Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/DamMacroStabilityCommon/Assemblers/DtoHelperServices.cs =================================================================== diff -u -r1965 -r2126 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/DamMacroStabilityCommon/Assemblers/DtoHelperServices.cs (.../DtoHelperServices.cs) (revision 1965) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/DamMacroStabilityCommon/Assemblers/DtoHelperServices.cs (.../DtoHelperServices.cs) (revision 2126) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using Deltares.DamEngine.Data.Standard; using System; using System.Collections.Generic; using System.IO; Fisheye: Tag 2126 refers to a dead (removed) revision in file `DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/DamMacroStabilityCommon/Assemblers/ObjectExtensions.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ConversionException.cs =================================================================== diff -u -r2118 -r2126 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ConversionException.cs (.../ConversionException.cs) (revision 2118) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ConversionException.cs (.../ConversionException.cs) (revision 2126) @@ -34,6 +34,13 @@ /// /// Initializes a new instance of the class. /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ConversionException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the class. + /// /// The type. /// The value. /// The inner. Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/Deltares.DamEngine.Calculators.csproj =================================================================== diff -u -r2111 -r2126 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/Deltares.DamEngine.Calculators.csproj (.../Deltares.DamEngine.Calculators.csproj) (revision 2111) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/Deltares.DamEngine.Calculators.csproj (.../Deltares.DamEngine.Calculators.csproj) (revision 2126) @@ -165,7 +165,6 @@ - Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ObjectExtensions.cs =================================================================== diff -u -r1974 -r2126 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ObjectExtensions.cs (.../ObjectExtensions.cs) (revision 1974) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ObjectExtensions.cs (.../ObjectExtensions.cs) (revision 2126) @@ -231,6 +231,29 @@ return result; } + /// + /// Convert the value to the enum type + /// + /// The enum type to convert to + /// The value to convert. + /// A translation dictionary to handle items that do not match a given string representation + /// The enum + public static T ToEnumType(this object value, IDictionary lookup) + { + if (value == null) + { + throw new ArgumentNullException("value"); + } + + string stringVal = value.ToString(); + if (lookup != null && lookup.ContainsKey(stringVal)) + { + return lookup[stringVal]; + } + + return (T)ToEnumType(value, typeof(T)); + } + /// /// Clone properties from an original object to a destination object. /// @@ -350,5 +373,69 @@ throw new ConversionException(type, v, e); } - } + + /// + /// Sets the object value. + /// + /// + /// The property info. + /// The destination object + /// The value + /// The translator + public static void SetObjectValue(this PropertyInfo pInfo, T dest, object value, + Func translator) + { + if (pInfo != null && pInfo.CanWrite) + { + if (translator != null) + { + pInfo.SetValue(dest, translator(value), null); + } + else + { + if (pInfo.PropertyType.IsEnum) + { + try + { + object v = Enum.Parse(pInfo.PropertyType, value.ToString(), true); + pInfo.SetValue(dest, v, null); + } + catch (Exception e) + { + string msg = string.Format( + "Couldn't parse value '{1}' to type {0}. Try to set a translation rule for this conversion.", + pInfo.PropertyType, value); + + throw new ConversionException(msg, e); + } + } + else + { + pInfo.SetValue(dest, ToType(value, pInfo.PropertyType), null); + } + } + } + } + + /// + /// Converts an object to another type. + /// + /// The value. + /// The type. + /// + /// is null and is value type and not nullable. + /// When an error occurs during conversion. + public static object ToType(this object value, Type type) + { + if (value == null) + { + ThrowIfValueType(type); + + return null; + } + + return value.ToType(type, DefaultCulture); + } + + } } \ No newline at end of file