Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ObjectExtensions.cs =================================================================== diff -u -r3893 -r4000 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ObjectExtensions.cs (.../ObjectExtensions.cs) (revision 3893) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/ObjectExtensions.cs (.../ObjectExtensions.cs) (revision 4000) @@ -87,10 +87,10 @@ { ThrowIfValueType(); - return default(T); + return default; } - return (T)value.ToType(typeof(T), DefaultCulture); + return (T) value.ToType(typeof(T), DefaultCulture); } /// @@ -165,6 +165,7 @@ //TODO: how to handle 100,000 english notation? Seems not to occur in most/mabye all situations } + result = value.Equals("NaN") ? Double.NaN : Convert.ChangeType(value, type, CultureInfo.InvariantCulture); } else @@ -181,7 +182,7 @@ case "UInt32": case "Int64": case "UInt64": - var tmp = Convert.ChangeType(value, typeof(double), CultureInfo.InvariantCulture); + object tmp = Convert.ChangeType(value, typeof(double), CultureInfo.InvariantCulture); result = Convert.ChangeType(tmp, type); break; default: @@ -218,7 +219,7 @@ ThrowConversionException(value, type, null); } - string stringVal = value.ToString(); + var stringVal = value.ToString(); object result = null; try { @@ -228,6 +229,7 @@ { ThrowConversionException(value, type, e); } + return result; } @@ -245,13 +247,13 @@ throw new ArgumentNullException("value"); } - string stringVal = value.ToString(); + var stringVal = value.ToString(); if (lookup != null && lookup.ContainsKey(stringVal)) { return lookup[stringVal]; } - return (T)ToEnumType(value, typeof(T)); + return (T) ToEnumType(value, typeof(T)); } /// @@ -273,11 +275,12 @@ { throw new ArgumentNullException("destination", "Destination object must first be instantiated."); } + // Loop through each property in the destination - foreach (var destinationProperty in destination.GetType().GetProperties()) + foreach (PropertyInfo destinationProperty in destination.GetType().GetProperties()) { - bool excluded = false; - foreach (var excludedProperty in excludedProperties) + var excluded = false; + foreach (string excludedProperty in excludedProperties) { if (excludedProperty.ToLower().Equals(destinationProperty.Name.ToLower())) { @@ -290,18 +293,82 @@ { continue; } + // find and set val if we can find a matching property name and matching type in the origin with the origin's value if (!origin.Equals(default(T1)) && destinationProperty.CanWrite) { PropertyInfo propertyInfo = destinationProperty; origin.GetType().GetProperties().Where( - x => x.CanRead && (x.Name == propertyInfo.Name && x.PropertyType == propertyInfo.PropertyType)) + x => x.CanRead && (x.Name == propertyInfo.Name && x.PropertyType == propertyInfo.PropertyType)) .ToList() .ForEach(x => propertyInfo.SetValue(destination, x.GetValue(origin, null), null)); } } } + /// + /// 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); + } + private static bool IsGenericIDictionary(PropertyInfo destinationProperty) { return destinationProperty.PropertyType.IsGenericType && destinationProperty.PropertyType.GetGenericTypeDefinition() == typeof(IDictionary<,>); @@ -364,7 +431,7 @@ /// The exception. private static void ThrowConversionException(object value, Type type, Exception e) { - string v = ""; + var v = ""; try { v = value.ToString().Replace("\0", " "); @@ -376,69 +443,5 @@ 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