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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
//
// System.ComponentModel.PropertyDescriptor.cs
//
// Author:
// Lluis Sanchez Gual (lluis@ximian.com)
// Ivan N. Zlatev (contact i-nZ.net)
// (C) Novell, Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
namespace System.ComponentModel
{
internal class ReflectionPropertyDescriptor : PropertyDescriptor
{
PropertyInfo _member;
Type _componentType;
Type _propertyType;
PropertyInfo getter, setter;
bool accessors_inited;
public ReflectionPropertyDescriptor (Type componentType, PropertyDescriptor oldPropertyDescriptor, Attribute [] attributes)
: base (oldPropertyDescriptor, attributes)
{
_componentType = componentType;
_propertyType = oldPropertyDescriptor.PropertyType;
}
public ReflectionPropertyDescriptor (Type componentType, string name, Type type, Attribute [] attributes)
: base (name, attributes)
{
_componentType = componentType;
_propertyType = type;
}
public ReflectionPropertyDescriptor (PropertyInfo info)
: base (info.Name, null)
{
_member = info;
_componentType = _member.DeclaringType;
_propertyType = info.PropertyType;
}
PropertyInfo GetPropertyInfo ()
{
if (_member == null) {
_member = _componentType.GetProperty (Name, BindingFlags.GetProperty | BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance,
null, this.PropertyType,
new Type[0], new ParameterModifier[0]);
if (_member == null)
throw new ArgumentException ("Accessor methods for the " + Name + " property are missing");
}
return _member;
}
public override Type ComponentType {
get { return _componentType; }
}
public override bool IsReadOnly {
get {
ReadOnlyAttribute attrib = ((ReadOnlyAttribute) Attributes[typeof (ReadOnlyAttribute)]);
return !GetPropertyInfo ().CanWrite || attrib.IsReadOnly;
}
}
public override Type PropertyType {
get { return _propertyType; }
}
// The last added to the list attributes have higher precedence
//
protected override void FillAttributes (IList attributeList)
{
base.FillAttributes (attributeList);
if (!GetPropertyInfo ().CanWrite)
attributeList.Add (ReadOnlyAttribute.Yes);
// PropertyDescriptor merges the attributes of both virtual and also "new" properties
// in the the component type hierarchy.
//
int numberOfBaseTypes = 0;
Type baseType = this.ComponentType;
while (baseType != null && baseType != typeof (object)) {
numberOfBaseTypes++;
baseType = baseType.BaseType;
}
Attribute[][] hierarchyAttributes = new Attribute[numberOfBaseTypes][];
baseType = this.ComponentType;
while (baseType != null && baseType != typeof (object)) {
PropertyInfo property = baseType.GetProperty (Name, BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance |
BindingFlags.DeclaredOnly,
null, this.PropertyType,
new Type[0], new ParameterModifier[0]);
if (property != null) {
object[] attrObjects = property.GetCustomAttributes (false);
Attribute[] attrsArray = new Attribute[attrObjects.Length];
attrObjects.CopyTo (attrsArray, 0);
// add in reverse order so that the base types have lower precedence
hierarchyAttributes[--numberOfBaseTypes] = attrsArray;
}
baseType = baseType.BaseType;
}
foreach (Attribute[] attrArray in hierarchyAttributes) {
if (attrArray != null) {
foreach (Attribute attr in attrArray)
attributeList.Add (attr);
}
}
foreach (Attribute attribute in TypeDescriptor.GetAttributes (PropertyType))
attributeList.Add (attribute);
}
public override object GetValue (object component)
{
component = MemberDescriptor.GetInvokee (_componentType, component);
InitAccessors ();
return getter.GetValue (component, null);
}
DesignerTransaction CreateTransaction (object obj, string description)
{
IComponent com = obj as IComponent;
if (com == null || com.Site == null)
return null;
IDesignerHost dh = (IDesignerHost) com.Site.GetService (typeof(IDesignerHost));
if (dh == null)
return null;
DesignerTransaction tran = dh.CreateTransaction (description);
IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
if (ccs != null)
ccs.OnComponentChanging (com, this);
return tran;
}
void EndTransaction (object obj, DesignerTransaction tran, object oldValue, object newValue, bool commit)
{
if (tran == null) {
// FIXME: EventArgs might be differen type.
OnValueChanged (obj, new PropertyChangedEventArgs (Name));
return;
}
if (commit) {
IComponent com = obj as IComponent;
IComponentChangeService ccs = (IComponentChangeService) com.Site.GetService (typeof(IComponentChangeService));
if (ccs != null)
ccs.OnComponentChanged (com, this, oldValue, newValue);
tran.Commit ();
// FIXME: EventArgs might be differen type.
OnValueChanged (obj, new PropertyChangedEventArgs (Name));
} else
tran.Cancel ();
}
/*
This method exists because reflection is way too low level for what we need.
A given virtual property that is partially overriden by a child won't show the
non-overriden accessor in PropertyInfo. IOW:
class Parent {
public virtual string Prop { get; set; }
}
class Child : Parent {
public override string Prop {
get { return "child"; }
}
}
PropertyInfo pi = typeof (Child).GetProperty ("Prop");
pi.GetGetMethod (); //returns the MethodInfo for the overridden getter
pi.GetSetMethod (); //returns null as no override exists
*/
void InitAccessors () {
if (accessors_inited)
return;
PropertyInfo prop = GetPropertyInfo ();
MethodInfo setterMethod, getterMethod;
setterMethod = prop.GetSetMethod (true);
getterMethod = prop.GetGetMethod (true);
if (getterMethod != null)
getter = prop;
if (setterMethod != null)
setter = prop;
if (setterMethod != null && getterMethod != null) {//both exist
accessors_inited = true;
return;
}
if (setterMethod == null && getterMethod == null) {//neither exist, this is a broken property
accessors_inited = true;
return;
}
//In order to detect that this is a virtual property with override, we check the non null accessor
MethodInfo mi = getterMethod != null ? getterMethod : setterMethod;
if (mi == null || !mi.IsVirtual || (mi.Attributes & MethodAttributes.NewSlot) == MethodAttributes.NewSlot) {
accessors_inited = true;
return;
}
Type type = _componentType.BaseType;
while (type != null && type != typeof (object)) {
prop = type.GetProperty (Name, BindingFlags.GetProperty | BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance,
null, this.PropertyType,
new Type[0], new ParameterModifier[0]);
if (prop == null) //nothing left to search
break;
if (setterMethod == null)
setterMethod = mi = prop.GetSetMethod ();
else
getterMethod = mi = prop.GetGetMethod ();
if (getterMethod != null && getter == null)
getter = prop;
if (setterMethod != null && setter == null)
setter = prop;
if (mi != null)
break;
type = type.BaseType;
}
accessors_inited = true;
}
public override void SetValue (object component, object value)
{
DesignerTransaction tran = CreateTransaction (component, "Set Property '" + Name + "'");
object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
object old = GetValue (propertyHolder);
try {
InitAccessors ();
setter.SetValue (propertyHolder, value, null);
EndTransaction (component, tran, old, value, true);
} catch {
EndTransaction (component, tran, old, value, false);
throw;
}
}
MethodInfo FindPropertyMethod (object o, string method_name)
{
MethodInfo mi = null;
string name = method_name + Name;
foreach (MethodInfo m in o.GetType().GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) {
// XXX should we really not check the return type of the method?
if (m.Name == name && m.GetParameters().Length == 0) {
mi = m;
break;
}
}
return mi;
}
public override void ResetValue (object component)
{
object propertyHolder = MemberDescriptor.GetInvokee (_componentType, component);
DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
if (attrib != null)
SetValue (propertyHolder, attrib.Value);
DesignerTransaction tran = CreateTransaction (component, "Reset Property '" + Name + "'");
object old = GetValue (propertyHolder);
try {
MethodInfo mi = FindPropertyMethod (propertyHolder, "Reset");
if (mi != null)
mi.Invoke (propertyHolder, null);
EndTransaction (component, tran, old, GetValue (propertyHolder), true);
} catch {
EndTransaction (component, tran, old, GetValue (propertyHolder), false);
throw;
}
}
public override bool CanResetValue (object component)
{
component = MemberDescriptor.GetInvokee (_componentType, component);
DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
if (attrib != null) {
object current = GetValue (component);
if (attrib.Value == null || current == null){
if (attrib.Value != current)
return true;
if (attrib.Value == null && current == null)
return false;
}
return !attrib.Value.Equals (current);
} else {
if (!_member.CanWrite)
return false;
MethodInfo mi = FindPropertyMethod (component, "ShouldPersist");
if (mi != null)
return (bool) mi.Invoke (component, null);
mi = FindPropertyMethod (component, "ShouldSerialize");
if (mi != null && !((bool) mi.Invoke (component, null)))
return false;
mi = FindPropertyMethod (component, "Reset");
return mi != null;
}
}
public override bool ShouldSerializeValue (object component)
{
component = MemberDescriptor.GetInvokee (_componentType, component);
if (IsReadOnly) {
MethodInfo mi = FindPropertyMethod (component, "ShouldSerialize");
if (mi != null)
return (bool) mi.Invoke (component, null);
return Attributes.Contains (DesignerSerializationVisibilityAttribute.Content);
}
DefaultValueAttribute attrib = ((DefaultValueAttribute) Attributes[typeof (DefaultValueAttribute)]);
if (attrib != null) {
object current = GetValue (component);
if (attrib.Value == null || current == null)
return attrib.Value != current;
return !attrib.Value.Equals (current);
}
else {
MethodInfo mi = FindPropertyMethod (component, "ShouldSerialize");
if (mi != null)
return (bool) mi.Invoke (component, null);
// MSDN: If this method cannot find a DefaultValueAttribute or a ShouldSerializeMyProperty method,
// it cannot create optimizations and it returns true.
return true;
}
}
}
}
|