using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; namespace Io.Github.Kerwinxu.LibShapes.Core.Shape { /// /// 枚举转换器 /// 用此类之前,必须保证在枚举项中定义了Description /// public class EnumConverter : ExpandableObjectConverter { /// /// 枚举项集合 /// Dictionary dic; /// /// 构造函数 /// public EnumConverter() { dic = new Dictionary(); } /// /// 加载枚举项集合 /// /// private void LoadDic(ITypeDescriptorContext context) { dic = GetEnumValueDesDic(context.PropertyDescriptor.PropertyType); } /// /// 是否可从来源转换 /// /// /// /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } /// /// 从来源转换 /// /// /// /// /// public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { //如果是枚举 if (context.PropertyDescriptor.PropertyType.IsEnum) { if (dic.Count <= 0) LoadDic(context); if (dic.Values.Contains(value.ToString())) { foreach (object obj in dic.Keys) { if (dic[obj] == value.ToString()) { return obj; } } } } } return base.ConvertFrom(context, culture, value); } /// /// 是否可转换 /// /// /// /// public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return true; } /// /// /// /// /// public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } /// /// /// /// /// public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; } /// /// /// /// /// public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { //ListAttribute listAttribute = (ListAttribute)context.PropertyDescriptor.Attributes[typeof(ListAttribute)]; //StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(listAttribute._lst); //Dictionary dic = GetEnumValueDesDic(typeof(PKGenerator)); //StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(dic.Keys); if (dic == null || dic.Count <= 0) LoadDic(context); StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(dic.Keys); return vals; } /// /// /// /// /// /// /// /// public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { //DescriptionAttribute.GetCustomAttribute( //EnumDescription //List> mList = UserCombox.ToListForBind(value.GetType()); //foreach (KeyValuePair mItem in mList) //{ // if (mItem.Key.Equals(value)) // { // return mItem.Value; // } //} //return "Error!"; //绑定控件 // FieldInfo fieldinfo = value.GetType().GetField(value.ToString()); //Object[] objs = fieldinfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); //if (objs == null || objs.Length == 0) //{ // return value.ToString(); //} //else //{ // System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0]; // return da.Description; //} if (dic.Count <= 0) LoadDic(context); foreach (object key in dic.Keys) { if (key.ToString() == value.ToString() || dic[key] == value.ToString()) { return dic[key].ToString(); } } return base.ConvertTo(context, culture, value, destinationType); } /// /// 记载枚举的值+描述 /// /// /// public Dictionary GetEnumValueDesDic(Type enumType) { Dictionary dic = new Dictionary(); FieldInfo[] fieldinfos = enumType.GetFields(); foreach (FieldInfo field in fieldinfos) { if (field.FieldType.IsEnum) { Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); if (objs.Length > 0) { dic.Add(Enum.Parse(enumType, field.Name), ((DescriptionAttribute)objs[0]).Description); } } } return dic; } } }