public static object ConvertObject(PropertyInfo prop, object val, Type objectType) { // Should i return default(T) if property.PropertyType is a valuetype? if (val == null) { return(val); } if (prop.PropertyType.IsAssignableFrom(val.GetType())) { return(val); } if (prop.PropertyType == typeof(string)) { return(val.ToString()); } TypeConverter tc = Helper.GetConverterFor(prop, prop.PropertyType); if (tc == null) { tc = new MoonlightTypeConverter(prop.Name, prop.PropertyType); } return(tc.ConvertFrom(null, Helper.DefaultCulture, val)); }
public static object ConvertObject(DependencyProperty dp, object val, Type objectType, bool doToStringConversion) { // Should i return default(T) if property.PropertyType is a valuetype? if (val == null) { return(val); } if (dp.PropertyType.IsAssignableFrom(val.GetType())) { return(val); } if (dp.PropertyType == typeof(string)) { return(doToStringConversion ? val.ToString() : ""); } TypeConverter tc = null; if (dp.IsAttached) { tc = Helper.GetConverterFor(GetGetterMethodForAttachedDP(dp, val), dp.PropertyType); } else { PropertyInfo pi = dp.DeclaringType.GetProperty(dp.Name); if (pi != null) { tc = Helper.GetConverterFor(pi, pi.PropertyType); if (tc == null) { tc = new MoonlightTypeConverter(pi.Name, pi.PropertyType); } } } if (tc == null) { tc = new MoonlightTypeConverter(dp.Name, dp.PropertyType); } return(tc.ConvertFrom(val)); }