/// <summary> /// This type supports the Fluorine infrastructure and is not intended to be used directly from your code. /// </summary> /// <param name="context">An ITypeDescriptorContext that provides a format context.</param> /// <param name="culture">A CultureInfo object. If a null reference (Nothing in Visual Basic) is passed, the current culture is assumed.</param> /// <param name="value">The Object to convert.</param> /// <param name="destinationType">The Type to convert the value parameter to.</param> /// <returns>An Object that represents the converted value.</returns> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { ASObject aso = value as ASObject; if (!ReflectionUtils.IsInstantiatableType(destinationType)) { return(null); } object instance = TypeHelper.CreateInstance(destinationType); if (instance != null) { foreach (string memberName in aso.Keys) { object val = aso[memberName]; //MemberInfo mi = ReflectionUtils.GetMember(destinationType, key, MemberTypes.Field | MemberTypes.Property); //if (mi != null) // ReflectionUtils.SetMemberValue(mi, result, aso[key]); PropertyInfo propertyInfo = null; try { propertyInfo = destinationType.GetProperty(memberName); } catch (AmbiguousMatchException) { //To resolve the ambiguity, include BindingFlags.DeclaredOnly to restrict the search to members that are not inherited. propertyInfo = destinationType.GetProperty(memberName, BindingFlags.DeclaredOnly | BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance); } if (propertyInfo != null) { try { val = TypeHelper.ChangeType(val, propertyInfo.PropertyType); if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null) { if (propertyInfo.GetIndexParameters() == null || propertyInfo.GetIndexParameters().Length == 0) { propertyInfo.SetValue(instance, val, null); } else { string msg = __Res.GetString(__Res.Reflection_PropertyIndexFail, string.Format("{0}.{1}", destinationType.FullName, memberName)); throw new FluorineException(msg); } } else { //string msg = __Res.GetString(__Res.Reflection_PropertyReadOnly, string.Format("{0}.{1}", type.FullName, memberName)); } } catch (Exception ex) { string msg = __Res.GetString(__Res.Reflection_PropertySetFail, string.Format("{0}.{1}", destinationType.FullName, memberName), ex.Message); throw new FluorineException(msg); } } else { FieldInfo fi = destinationType.GetField(memberName, BindingFlags.Public | BindingFlags.Instance); try { if (fi != null) { val = TypeHelper.ChangeType(val, fi.FieldType); fi.SetValue(instance, val); } else { //string msg = __Res.GetString(__Res.Reflection_MemberNotFound, string.Format("{0}.{1}", destinationType.FullName, memberName)); } } catch (Exception ex) { string msg = __Res.GetString(__Res.Reflection_FieldSetFail, string.Format("{0}.{1}", destinationType.FullName, memberName), ex.Message); throw new FluorineException(msg); } } } } return(instance); }