/// <summary> /// 使用指定的上下文和区域性信息将给定的值对象转换为指定的类型[转换为显示对象] /// </summary> /// <param name="context">提供格式上下文</param> /// <param name="culture">如果传递 null,则采用当前区域性</param> /// <param name="value">要转换的 Object</param> /// <param name="destinationType">value 参数要转换成的 Type</param> /// <returns>表示转换的 value 的 Object</returns> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { try { if (context == null) { return(base.ConvertTo(context, culture, value, destinationType)); } if (value == null) { return(value); } if (context.PropertyDescriptor.PropertyType.IsEnum) { return(EnumEx.GetEnumItemDisplayName(value)); } else { if (context.Instance.GetType().GetInterface(_ipropertyGridDropDownListType.FullName) == null) { return(value); } Type valueType = value.GetType(); if (valueType.IsPrimitive) { return(value); } IPropertyGridDropDown ipropertyGridDropDownList = (IPropertyGridDropDown)context.Instance; string displayPropertyName = ipropertyGridDropDownList.GetPropertyGridDisplayName(context.PropertyDescriptor.Name); //如果显示属性名称为空或null则调用父类方法 if (string.IsNullOrEmpty(displayPropertyName)) { return(value.ToString()); } object retValue = null; System.Reflection.PropertyInfo propertyInfo = valueType.GetProperty(displayPropertyName); if (propertyInfo == null) { System.Reflection.FieldInfo fieldInfo = valueType.GetField(displayPropertyName); if (fieldInfo == null) { //如果属性名或字段名不正确,则也调用父类方法 retValue = value; } else { //通过字段反射获取值 retValue = fieldInfo.GetValue(value); } } else { //通过属性反射获取值 retValue = propertyInfo.GetValue(value, null); } return(retValue); } } catch (Exception ex) { Loger.Error(ex); return(null); } }
/// <summary> /// 使用指定的上下文和区域性信息将给定的对象转换为此转换器的类型[从显示文本转换为真实对象] /// </summary> /// <param name="context">提供格式上下文</param> /// <param name="culture">用作当前区域性的 CultureInfo</param> /// <param name="value">要转换的 Object</param> /// <returns>表示转换的 value 的 Object</returns> public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { try { if (context == null) { return(base.ConvertFrom(context, culture, value)); } if (value == null) { return(value); } string valueStr = value.ToString(); if (context.PropertyDescriptor.PropertyType.IsEnum) { if (string.IsNullOrWhiteSpace(valueStr)) { return(base.ConvertFrom(context, culture, value)); } else { return(EnumEx.GetEnumByDisplayNameExAttributeDisplayName(context.PropertyDescriptor.PropertyType, valueStr)); } } else { if (context.Instance.GetType().GetInterface(_ipropertyGridDropDownListType.FullName) == null) { return(value); } IPropertyGridDropDown ipropertyGridDropDownList = (IPropertyGridDropDown)context.Instance; System.Collections.ICollection collection = ipropertyGridDropDownList.GetPropertyGridDropDownItems(context.PropertyDescriptor.Name); if (collection == null || collection.Count == 0) { return(value); } Type instanceType = null; foreach (var item in collection) { instanceType = item.GetType(); break; } if (instanceType.IsPrimitive) { if (value.GetType() != instanceType) { value = Convert.ChangeType(value, instanceType); } return(value); } else { string displayPropertyName = ipropertyGridDropDownList.GetPropertyGridDisplayName(context.PropertyDescriptor.Name); if (string.IsNullOrWhiteSpace(displayPropertyName)) { //如果显示属性名称为空或null则直接用原始数据作比较,比如:字符串集合 foreach (var item in collection) { if (object.Equals(valueStr, item.ToString())) { return(item); } } return(null); } else { System.Reflection.PropertyInfo propertyInfo = instanceType.GetProperty(displayPropertyName); if (propertyInfo == null) { System.Reflection.FieldInfo fieldInfo = instanceType.GetField(displayPropertyName); if (fieldInfo == null) { //如果属性名或字段名不正确,则也调用父类方法 foreach (var item in collection) { if (object.Equals(value, item)) { return(item); } } return(null); } else { //通过字段反射获取值 foreach (var item in collection) { if (object.Equals(fieldInfo.GetValue(item), value)) { return(item); } } return(null); } } else { //通过属性反射获取值 foreach (var item in collection) { if (object.Equals(propertyInfo.GetValue(item, null), value)) { return(item); } } return(null); } } } } } catch (Exception ex) { Loger.Error(ex); return(null); } }