ningshuxia
2025-03-24 7b8ae93d47186c442ff890a1a83d108f115924c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace Yw.WinFrmUI
{
    /// <summary>
    /// 枚举在propertyGrid中的显示
    /// </summary>
    public class EnumPropertyConverter : ExpandableObjectConverter
    {
        /// <summary>  
        /// 构造函数  
        /// </summary>  
        public EnumPropertyConverter()
        {
            _dict = new Dictionary<object, string>();
        }
 
        private Dictionary<object, string> _dict;//枚举项集合
 
        /// <summary>  
        /// 加载枚举项集合  
        /// </summary>  
        private void LoadDict(ITypeDescriptorContext context)
        {
            _dict = GetEnumValueDispDict(context.PropertyDescriptor.PropertyType);
        }
 
        /// <summary>  
        /// 是否可从来源转换  
        /// </summary>  
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
 
            return base.CanConvertFrom(context, sourceType);
        }
 
        /// <summary>  
        /// 从来源转换  
        /// </summary>  
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                //如果是枚举  
                if (context.PropertyDescriptor.PropertyType.IsEnum)
                {
                    if (_dict.Count <= 0)
                        LoadDict(context);
                    if (_dict.Values.Contains(value.ToString()))
                    {
                        foreach (object obj in _dict.Keys)
                        {
                            if (_dict[obj] == value.ToString())
                            {
                                return obj;
                            }
                        }
                    }
                }
            }
 
            return base.ConvertFrom(context, culture, value);
        }
 
        /// <summary>  
        /// 是否可转换  
        /// </summary>  
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return true;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (_dict == null || _dict.Count <= 0)
                LoadDict(context);
 
            StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(_dict.Keys);
 
            return vals;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (_dict.Count <= 0)
                LoadDict(context);
 
            foreach (object key in _dict.Keys)
            {
                if (key.ToString() == value.ToString() || _dict[key] == value.ToString())
                {
                    return _dict[key].ToString();
                }
            }
 
            return base.ConvertTo(context, culture, value, destinationType);
        }
 
        /// <summary>  
        /// 记载枚举的值+Display  
        /// </summary>  
        /// <param name="enumType"></param>  
        /// <returns></returns>  
        public Dictionary<object, string> GetEnumValueDispDict(Type enumType)
        {
            Dictionary<object, string> dict = new Dictionary<object, string>();
            FieldInfo[] fieldinfos = enumType.GetFields();
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    Object[] objs = field.GetCustomAttributes(typeof(DisplayAttribute), false);
                    if (objs.Length > 0)
                    {
                        dict.Add(Enum.Parse(enumType, field.Name), ((DisplayAttribute)objs[0]).Name);
                    }
                }
 
            }
 
            return dict;
        }
 
    }
}