comparison Source/ComponentModel/CustomTypeDescriptorImpl.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Reflection;
5
6 using BLToolkit.Reflection;
7
8 namespace BLToolkit.ComponentModel
9 {
10 public class CustomTypeDescriptorImpl : ICustomTypeDescriptor
11 {
12 #region Constructors
13
14 public CustomTypeDescriptorImpl()
15 {
16 _typeDescriptionProvider = CreateTypeDescriptionProvider(GetType());
17 }
18
19 public CustomTypeDescriptorImpl(Type type)
20 {
21 _typeDescriptionProvider = CreateTypeDescriptionProvider(type);
22 }
23
24 public CustomTypeDescriptorImpl(ITypeDescriptionProvider typeDescriptionProvider)
25 {
26 _typeDescriptionProvider = typeDescriptionProvider;
27 }
28
29 private readonly ITypeDescriptionProvider _typeDescriptionProvider;
30
31 protected virtual ITypeDescriptionProvider CreateTypeDescriptionProvider(Type type)
32 {
33 return TypeAccessor.GetAccessor(type);
34 }
35
36 #endregion
37
38 #region ICustomTypeDescriptor Members
39
40 AttributeCollection _attributes;
41 AttributeCollection ICustomTypeDescriptor.GetAttributes()
42 {
43 if (_attributes == null)
44 _attributes = _typeDescriptionProvider.GetAttributes();
45
46 return _attributes;
47 }
48
49 string ICustomTypeDescriptor.GetClassName()
50 {
51 return _typeDescriptionProvider.ClassName;
52 }
53
54 string ICustomTypeDescriptor.GetComponentName()
55 {
56 return _typeDescriptionProvider.ComponentName;
57 }
58
59 TypeConverter _converter;
60 TypeConverter ICustomTypeDescriptor.GetConverter()
61 {
62 if (_converter == null)
63 {
64 TypeConverterAttribute attr =
65 _td.GetAttributes()[typeof(TypeConverterAttribute)] as TypeConverterAttribute;
66
67 if (attr != null && !string.IsNullOrEmpty(attr.ConverterTypeName))
68 {
69 Type type = GetTypeByName(attr.ConverterTypeName);
70
71 if (type != null && typeof(TypeConverter).IsAssignableFrom(type))
72 _converter = (TypeConverter)CreateInstance(type);
73 }
74
75 if (_converter == null)
76 _converter = new TypeConverter();
77 }
78
79 return _converter;
80 }
81
82 bool _readDefaultEvent;
83 EventDescriptor _defaultEvent;
84 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
85 {
86 if (_readDefaultEvent == false)
87 {
88 _readDefaultEvent = true;
89
90 DefaultEventAttribute attr =
91 _td.GetAttributes()[typeof(DefaultEventAttribute)] as DefaultEventAttribute;
92
93 if (attr != null && !string.IsNullOrEmpty(attr.Name))
94 _defaultEvent = _typeDescriptionProvider.GetEvent(attr.Name);
95 }
96
97 return _defaultEvent;
98 }
99
100 bool _readDefaultProperty;
101 PropertyDescriptor _defaultProperty;
102 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
103 {
104 if (_readDefaultProperty == false)
105 {
106 _readDefaultProperty = true;
107
108 DefaultPropertyAttribute attr =
109 _td.GetAttributes()[typeof(DefaultPropertyAttribute)] as DefaultPropertyAttribute;
110
111 if (attr != null && !string.IsNullOrEmpty(attr.Name))
112 _defaultProperty = _typeDescriptionProvider.GetProperty(attr.Name);
113 }
114
115 return _defaultProperty;
116 }
117
118 Hashtable _editors;
119 object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
120 {
121 if (_editors == null)
122 _editors = new Hashtable();
123
124 object editor = _editors[editorBaseType];
125
126 if (editor == null)
127 {
128 if (_editors.Contains(editorBaseType))
129 return null;
130
131 foreach (Attribute attr in _td.GetAttributes())
132 {
133 if (attr is EditorAttribute)
134 {
135 EditorAttribute ea = (EditorAttribute)attr;
136
137 if (ea.EditorBaseTypeName != null &&
138 ea.EditorTypeName != null &&
139 editorBaseType == GetTypeByName(ea.EditorBaseTypeName))
140 {
141 Type type = GetTypeByName(ea.EditorTypeName);
142
143 if (type != null)
144 {
145 editor = CreateInstance(type);
146 break;
147 }
148 }
149 }
150 }
151
152 _editors[editorBaseType] = editor;
153 }
154
155 return editor;
156 }
157
158 EventDescriptorCollection _events;
159 EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
160 {
161 if (_events == null)
162 _events = _typeDescriptionProvider.GetEvents();
163
164 return _events;
165 }
166
167 EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
168 {
169 return _td.GetEvents(null);
170 }
171
172 PropertyDescriptorCollection _properties;
173 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
174 {
175 if (_properties == null)
176 _properties = _typeDescriptionProvider.GetProperties();
177
178 return _properties;
179 }
180
181 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
182 {
183 return _td.GetProperties(null);
184 }
185
186 object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
187 {
188 return this;
189 }
190
191 #endregion
192
193 #region Protected Members
194
195 private Type GetTypeByName(string typeName)
196 {
197 if (string.IsNullOrEmpty(typeName))
198 return null;
199
200 Type type = Type.GetType(typeName);
201
202 if (type != null)
203 return type;
204
205 int idx = typeName.IndexOf(',');
206
207 if (idx != -1)
208 typeName = typeName.Substring(0, idx);
209
210 return _typeDescriptionProvider.OriginalType.Assembly.GetType(typeName);
211 }
212
213 private object CreateInstance(Type type)
214 {
215 ConstructorInfo ci = type.GetConstructor(new Type[]{ typeof(Type) });
216
217 return ci != null?
218 Activator.CreateInstance(type, new object[] { _typeDescriptionProvider.OriginalType }):
219 Activator.CreateInstance(type);
220 }
221
222 private ICustomTypeDescriptor _td
223 {
224 get { return this; }
225 }
226
227 #endregion
228 }
229 }