/// <summary> /// 将value转换为目标类型 /// </summary> /// <param name="value">要转换的值</param> /// <param name="targetType">转换的目标类型</param> /// <returns></returns> public object Convert(object value, Type targetType) { var dic = value as IDictionary <string, object>; if (dic == null) { return(this.NextConvert.Convert(value, targetType)); } var instance = Activator.CreateInstance(targetType); var setters = PropertySetter.GetPropertySetters(targetType); foreach (var set in setters) { var key = dic.Keys.FirstOrDefault(k => string.Equals(k, set.Name, StringComparison.OrdinalIgnoreCase)); if (key != null) { var targetValue = this.Converter.Convert(dic[key], set.Type); set.SetValue(instance, targetValue); } } return(instance); }
/// <summary> /// 将value转换为目标类型 /// </summary> /// <param name="value">要转换的值</param> /// <param name="targetType">转换的目标类型</param> /// <returns></returns> public object Convert(object value, Type targetType) { var dynamicObject = value as DynamicObject; if (dynamicObject == null) { return(this.NextConvert.Convert(value, targetType)); } var instance = Activator.CreateInstance(targetType); var setters = PropertySetter.GetPropertySetters(targetType); foreach (var set in setters) { object targetValue; if (this.TryGetValue(dynamicObject, set.Name, out targetValue) == true) { targetValue = this.Converter.Convert(targetValue, set.Type); set.SetValue(instance, targetValue); } } return(instance); }