comparison Source/ComponentModel/TypeDescriptorExtender.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 abstract class TypeDescriptorExtender : ICustomTypeDescriptor, ITypeDescriptionProvider
11 {
12 #region Constructors
13
14 protected TypeDescriptorExtender(Type baseType)
15 {
16 if (baseType == null)
17 throw new ArgumentNullException("baseType");
18
19 _baseObject = TypeAccessor.CreateInstanceEx(baseType);
20 }
21
22 protected TypeDescriptorExtender(object baseObject)
23 {
24 if (baseObject == null)
25 throw new ArgumentNullException("baseObject");
26
27 _baseObject = baseObject;
28 }
29
30 #endregion
31
32 #region Public Members
33
34 private readonly object _baseObject;
35 public object BaseObject
36 {
37 get { return _baseObject; }
38 }
39
40 #endregion
41
42 #region Protected Members
43
44 private static readonly Hashtable _hashDescriptors = new Hashtable();
45
46 [NonSerialized]
47 private ICustomTypeDescriptor _typeDescriptor;
48 private ICustomTypeDescriptor TypeDescriptor
49 {
50 get
51 {
52 if (_typeDescriptor == null)
53 {
54 Type key1 = GetType();
55 Type key2 = _baseObject.GetType();
56
57 Hashtable tbl = (Hashtable)_hashDescriptors[key1];
58
59 if (tbl == null)
60 _hashDescriptors[key1] = tbl = new Hashtable();
61
62 _typeDescriptor = (ICustomTypeDescriptor)tbl[key2];
63
64 if (_typeDescriptor == null)
65 tbl[key2] = _typeDescriptor = CreateTypeDescriptor();
66 }
67
68 return _typeDescriptor;
69 }
70 }
71
72 private ICustomTypeDescriptor CreateTypeDescriptor()
73 {
74 return new CustomTypeDescriptorImpl(this);
75 }
76
77 #endregion
78
79 #region ICustomTypeDescriptor Members
80
81 AttributeCollection ICustomTypeDescriptor.GetAttributes()
82 {
83 return TypeDescriptor.GetAttributes();
84 }
85
86 string ICustomTypeDescriptor.GetClassName()
87 {
88 return TypeDescriptor.GetClassName();
89 }
90
91 string ICustomTypeDescriptor.GetComponentName()
92 {
93 return TypeDescriptor.GetComponentName();
94 }
95
96 TypeConverter ICustomTypeDescriptor.GetConverter()
97 {
98 return TypeDescriptor.GetConverter();
99 }
100
101 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
102 {
103 return TypeDescriptor.GetDefaultEvent();
104 }
105
106 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
107 {
108 return TypeDescriptor.GetDefaultProperty();
109 }
110
111 object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
112 {
113 return TypeDescriptor.GetEditor(editorBaseType);
114 }
115
116 EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
117 {
118 return TypeDescriptor.GetEvents(attributes);
119 }
120
121 EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
122 {
123 return TypeDescriptor.GetEvents();
124 }
125
126 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
127 {
128 return TypeDescriptor.GetProperties(attributes);
129 }
130
131 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
132 {
133 return TypeDescriptor.GetProperties();
134 }
135
136 object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
137 {
138 return TypeDescriptor.GetPropertyOwner(pd);
139 }
140
141 #endregion
142
143 #region ITypeDescriptionProvider Members
144
145 [NonSerialized]
146 private ICustomTypeDescriptor _baseTypeDescriptor;
147 private ICustomTypeDescriptor BaseTypeDescriptor
148 {
149 get
150 {
151 if (_baseTypeDescriptor == null)
152 {
153 _baseTypeDescriptor = _baseObject as ICustomTypeDescriptor ??
154 new CustomTypeDescriptorImpl(_baseObject.GetType());
155 }
156
157 return _baseTypeDescriptor;
158 }
159 }
160
161 [NonSerialized]
162 private ITypeDescriptionProvider _provider;
163 private ITypeDescriptionProvider Provider
164 {
165 get
166 {
167 if (_provider == null)
168 _provider = TypeAccessor.GetAccessor(GetType());
169
170 return _provider;
171 }
172 }
173
174 Type ITypeDescriptionProvider.OriginalType { get { return GetType(); } }
175 string ITypeDescriptionProvider.ClassName { get { return GetType().Name; } }
176 string ITypeDescriptionProvider.ComponentName { get { return GetType().Name; } }
177
178 EventDescriptor ITypeDescriptionProvider.GetEvent(string name)
179 {
180 EventInfo ei = GetType().GetEvent(name);
181
182 return ei != null ? Provider.GetEvent(name) : BaseTypeDescriptor.GetEvents()[name];
183 }
184
185 PropertyDescriptor ITypeDescriptionProvider.GetProperty(string name)
186 {
187 PropertyDescriptor pd = Provider.GetProperty(name);
188
189 return pd ?? BaseTypeDescriptor.GetProperties()[name];
190 }
191
192 AttributeCollection ITypeDescriptionProvider.GetAttributes()
193 {
194 AttributeCollection col1 = Provider.GetAttributes();
195 AttributeCollection col2 = BaseTypeDescriptor.GetAttributes();
196
197 Attribute[] attrs = new Attribute[col1.Count + col2.Count];
198
199 for (int i = 0; i < col1.Count; i++)
200 attrs[i] = col1[i];
201
202 for (int i = 0; i < col2.Count; i++)
203 attrs[col1.Count + i] = col2[i];
204
205 return new AttributeCollection(attrs);
206 }
207
208 EventDescriptorCollection ITypeDescriptionProvider.GetEvents()
209 {
210 EventDescriptorCollection col1 = Provider.GetEvents();
211 EventDescriptorCollection col2 = BaseTypeDescriptor.GetEvents();
212
213 EventDescriptorCollection col = new EventDescriptorCollection(new EventDescriptor[0]);
214
215 foreach (EventDescriptor ed in col1)
216 col.Add(ed);
217
218 foreach (EventDescriptor ed in col2)
219 if (col.Find(ed.Name, false) == null)
220 col.Add(ed);
221
222 return col;
223 }
224
225 PropertyDescriptorCollection ITypeDescriptionProvider.GetProperties()
226 {
227 throw new Exception("The method or operation is not implemented.");
228 }
229
230 #endregion
231 }
232 }