mscorlib(4.0.0.0) API with additions
PropertyDescriptor.cs
1 using System.Collections;
2 using System.Reflection;
5 
6 namespace System.ComponentModel
7 {
9  [ComVisible(true)]
10  [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
11  public abstract class PropertyDescriptor : MemberDescriptor
12  {
13  private TypeConverter converter;
14 
15  private Hashtable valueChangedHandlers;
16 
17  private object[] editors;
18 
19  private Type[] editorTypes;
20 
21  private int editorCount;
22 
25  public abstract Type ComponentType
26  {
27  get;
28  }
29 
32  public virtual TypeConverter Converter
33  {
34  get
35  {
36  AttributeCollection attributes = Attributes;
37  if (converter == null)
38  {
39  TypeConverterAttribute typeConverterAttribute = (TypeConverterAttribute)attributes[typeof(TypeConverterAttribute)];
40  if (typeConverterAttribute.ConverterTypeName != null && typeConverterAttribute.ConverterTypeName.Length > 0)
41  {
42  Type typeFromName = GetTypeFromName(typeConverterAttribute.ConverterTypeName);
43  if (typeFromName != null && typeof(TypeConverter).IsAssignableFrom(typeFromName))
44  {
45  converter = (TypeConverter)CreateInstance(typeFromName);
46  }
47  }
48  if (converter == null)
49  {
51  }
52  }
53  return converter;
54  }
55  }
56 
61 
65  public abstract bool IsReadOnly
66  {
67  get;
68  }
69 
73  {
74  get
75  {
77  return designerSerializationVisibilityAttribute.Visibility;
78  }
79  }
80 
83  public abstract Type PropertyType
84  {
85  get;
86  }
87 
91  public virtual bool SupportsChangeEvents => false;
92 
96  protected PropertyDescriptor(string name, Attribute[] attrs)
97  : base(name, attrs)
98  {
99  }
100 
104  : base(descr)
105  {
106  }
107 
112  : base(descr, attrs)
113  {
114  }
115 
121  public virtual void AddValueChanged(object component, EventHandler handler)
122  {
123  if (component == null)
124  {
125  throw new ArgumentNullException("component");
126  }
127  if (handler == null)
128  {
129  throw new ArgumentNullException("handler");
130  }
131  if (valueChangedHandlers == null)
132  {
133  valueChangedHandlers = new Hashtable();
134  }
135  EventHandler a = (EventHandler)valueChangedHandlers[component];
136  valueChangedHandlers[component] = Delegate.Combine(a, handler);
137  }
138 
143  public abstract bool CanResetValue(object component);
144 
149  public override bool Equals(object obj)
150  {
151  try
152  {
153  if (obj == this)
154  {
155  return true;
156  }
157  if (obj == null)
158  {
159  return false;
160  }
161  PropertyDescriptor propertyDescriptor = obj as PropertyDescriptor;
162  if (propertyDescriptor != null && propertyDescriptor.NameHashCode == NameHashCode && propertyDescriptor.PropertyType == PropertyType && propertyDescriptor.Name.Equals(Name))
163  {
164  return true;
165  }
166  }
167  catch
168  {
169  }
170  return false;
171  }
172 
176  protected object CreateInstance(Type type)
177  {
178  Type[] array = new Type[1]
179  {
180  typeof(Type)
181  };
182  ConstructorInfo constructor = type.GetConstructor(array);
183  if (constructor != null)
184  {
185  return TypeDescriptor.CreateInstance(null, type, array, new object[1]
186  {
188  });
189  }
190  return TypeDescriptor.CreateInstance(null, type, null, null);
191  }
192 
195  protected override void FillAttributes(IList attributeList)
196  {
197  converter = null;
198  editors = null;
199  editorTypes = null;
200  editorCount = 0;
201  base.FillAttributes(attributeList);
202  }
203 
207  {
208  return GetChildProperties(null, null);
209  }
210 
215  {
216  return GetChildProperties(null, filter);
217  }
218 
223  {
224  return GetChildProperties(instance, null);
225  }
226 
231  public virtual PropertyDescriptorCollection GetChildProperties(object instance, Attribute[] filter)
232  {
233  if (instance == null)
234  {
235  return TypeDescriptor.GetProperties(PropertyType, filter);
236  }
237  return TypeDescriptor.GetProperties(instance, filter);
238  }
239 
243  public virtual object GetEditor(Type editorBaseType)
244  {
245  object obj = null;
246  AttributeCollection attributes = Attributes;
247  if (editorTypes != null)
248  {
249  for (int i = 0; i < editorCount; i++)
250  {
251  if (editorTypes[i] == editorBaseType)
252  {
253  return editors[i];
254  }
255  }
256  }
257  if (obj == null)
258  {
259  for (int j = 0; j < attributes.Count; j++)
260  {
261  EditorAttribute editorAttribute = attributes[j] as EditorAttribute;
262  if (editorAttribute == null)
263  {
264  continue;
265  }
266  Type typeFromName = GetTypeFromName(editorAttribute.EditorBaseTypeName);
267  if (editorBaseType == typeFromName)
268  {
269  Type typeFromName2 = GetTypeFromName(editorAttribute.EditorTypeName);
270  if (typeFromName2 != null)
271  {
272  obj = CreateInstance(typeFromName2);
273  break;
274  }
275  }
276  }
277  if (obj == null)
278  {
279  obj = TypeDescriptor.GetEditor(PropertyType, editorBaseType);
280  }
281  if (editorTypes == null)
282  {
283  editorTypes = new Type[5];
284  editors = new object[5];
285  }
286  if (editorCount >= editorTypes.Length)
287  {
288  Type[] destinationArray = new Type[editorTypes.Length * 2];
289  object[] destinationArray2 = new object[editors.Length * 2];
290  Array.Copy(editorTypes, destinationArray, editorTypes.Length);
291  Array.Copy(editors, destinationArray2, editors.Length);
292  editorTypes = destinationArray;
293  editors = destinationArray2;
294  }
295  editorTypes[editorCount] = editorBaseType;
296  editors[editorCount++] = obj;
297  }
298  return obj;
299  }
300 
303  public override int GetHashCode()
304  {
306  }
307 
312  protected override object GetInvocationTarget(Type type, object instance)
313  {
314  object obj = base.GetInvocationTarget(type, instance);
315  ICustomTypeDescriptor customTypeDescriptor = obj as ICustomTypeDescriptor;
316  if (customTypeDescriptor != null)
317  {
318  obj = customTypeDescriptor.GetPropertyOwner(this);
319  }
320  return obj;
321  }
322 
326  protected Type GetTypeFromName(string typeName)
327  {
328  if (typeName == null || typeName.Length == 0)
329  {
330  return null;
331  }
332  Type type = Type.GetType(typeName);
333  Type type2 = null;
334  if (ComponentType != null && (type == null || ComponentType.Assembly.FullName.Equals(type.Assembly.FullName)))
335  {
336  int num = typeName.IndexOf(',');
337  if (num != -1)
338  {
339  typeName = typeName.Substring(0, num);
340  }
341  type2 = ComponentType.Assembly.GetType(typeName);
342  }
343  return type2 ?? type;
344  }
345 
349  public abstract object GetValue(object component);
350 
354  protected virtual void OnValueChanged(object component, EventArgs e)
355  {
356  if (component != null && valueChangedHandlers != null)
357  {
358  ((EventHandler)valueChangedHandlers[component])?.Invoke(component, e);
359  }
360  }
361 
367  public virtual void RemoveValueChanged(object component, EventHandler handler)
368  {
369  if (component == null)
370  {
371  throw new ArgumentNullException("component");
372  }
373  if (handler == null)
374  {
375  throw new ArgumentNullException("handler");
376  }
377  if (valueChangedHandlers != null)
378  {
379  EventHandler source = (EventHandler)valueChangedHandlers[component];
380  source = (EventHandler)Delegate.Remove(source, handler);
381  if (source != null)
382  {
383  valueChangedHandlers[component] = source;
384  }
385  else
386  {
387  valueChangedHandlers.Remove(component);
388  }
389  }
390  }
391 
395  protected internal EventHandler GetValueChangedHandler(object component)
396  {
397  if (component != null && valueChangedHandlers != null)
398  {
399  return (EventHandler)valueChangedHandlers[component];
400  }
401  return null;
402  }
403 
406  public abstract void ResetValue(object component);
407 
411  public abstract void SetValue(object component, object value);
412 
417  public abstract bool ShouldSerializeValue(object component);
418  }
419 }
int Count
Gets the number of attributes.
PropertyDescriptorCollection GetChildProperties(Attribute[] filter)
Returns a T:System.ComponentModel.PropertyDescriptorCollection using a specified array of attributes ...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
PropertyDescriptor(MemberDescriptor descr, Attribute[] attrs)
Initializes a new instance of the T:System.ComponentModel.PropertyDescriptor class with the name in t...
PropertyDescriptor(string name, Attribute[] attrs)
Initializes a new instance of the T:System.ComponentModel.PropertyDescriptor class with the specified...
abstract bool ShouldSerializeValue(object component)
When overridden in a derived class, determines a value indicating whether the value of this property ...
override int GetHashCode()
Returns the hash code for this instance.
Definition: Type.cs:2870
Provides an interface that supplies dynamic custom type information for an object.
static object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
Creates an object that can substitute for another data type.
Specifies the type of persistence to use when serializing a property on a component at design time.
virtual string FullName
Gets the display name of the assembly.
Definition: Assembly.cs:49
abstract Type PropertyType
When overridden in a derived class, gets the type of the property.
PropertyDescriptorCollection GetChildProperties(object instance)
Returns a T:System.ComponentModel.PropertyDescriptorCollection for a given object.
Represents a non-generic collection of objects that can be individually accessed by index.
Definition: IList.cs:8
static TypeConverter GetConverter(object component)
Returns a type converter for the type of the specified component.
static readonly LocalizableAttribute Yes
Specifies that a property should be localized. This static field is read-only.
Discovers the attributes of a class constructor and provides access to constructor metadata.
Represents the base class for custom attributes.
Definition: Attribute.cs:15
Definition: __Canon.cs:3
PropertyDescriptorCollection GetChildProperties()
Returns the default T:System.ComponentModel.PropertyDescriptorCollection.
virtual void RemoveValueChanged(object component, EventHandler handler)
Enables other objects to be notified when this property changes.
override bool Equals(object obj)
Returns whether the value of the given object is equal to the current T:System.ComponentModel....
ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
Searches for a constructor whose parameters match the specified argument types and modifiers,...
Definition: Type.cs:1378
string EditorTypeName
Gets the name of the editor class in the P:System.Type.AssemblyQualifiedName format.
virtual object GetEditor(Type editorBaseType)
Gets an editor of the specified type.
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: EventArgs.cs:9
static Delegate Remove(Delegate source, Delegate value)
Removes the last occurrence of the invocation list of a delegate from the invocation list of another ...
Definition: Delegate.cs:287
static Delegate Combine(Delegate a, Delegate b)
Concatenates the invocation lists of two delegates.
Definition: Delegate.cs:202
Provides an abstraction of a property on a class.
Specifies whether a property should be localized. This class cannot be inherited.
virtual string Name
Gets the name of the member.
abstract void SetValue(object component, object value)
When overridden in a derived class, sets the value of the component to a different value.
SecurityAction
Specifies the security actions that can be performed using declarative security.
string ConverterTypeName
Gets the fully qualified type name of the T:System.Type to use as a converter for the object this att...
Represents a class member, such as a property or event. This is an abstract base class.
static object GetEditor(object component, Type editorBaseType)
Gets an editor with the specified base type for the specified component.
string EditorBaseTypeName
Gets the name of the base class or interface serving as a lookup key for this editor.
delegate void EventHandler(object sender, EventArgs e)
Represents the method that will handle an event that has no event data.
Type GetTypeFromName(string typeName)
Returns a type using its name.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
Specifies the editor to use to change a property. This class cannot be inherited.
DesignerSerializationVisibility Visibility
Gets a value indicating the basic serialization mode a serializer should use when determining whether...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
abstract Assembly Assembly
Gets the T:System.Reflection.Assembly in which the type is declared. For generic types,...
Definition: Type.cs:131
virtual bool SupportsChangeEvents
Gets a value indicating whether value change notifications for this property may originate from outsi...
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
Provides information about the characteristics for a component, such as its attributes,...
virtual TypeConverter Converter
Gets the type converter for this property.
override void FillAttributes(IList attributeList)
Adds the attributes of the T:System.ComponentModel.PropertyDescriptor to the specified list of attrib...
object CreateInstance(Type type)
Creates an instance of the specified type.
Represents a collection of attributes.
abstract object GetValue(object component)
When overridden in a derived class, gets the current value of the property on a component.
virtual PropertyDescriptorCollection GetChildProperties(object instance, Attribute[] filter)
Returns a T:System.ComponentModel.PropertyDescriptorCollection for a given object using a specified a...
internal EventHandler GetValueChangedHandler(object component)
Retrieves the current set of ValueChanged event handlers for a specific component
DesignerSerializationVisibility SerializationVisibility
Gets a value indicating whether this property should be serialized, as specified in the T:System....
virtual void OnValueChanged(object component, EventArgs e)
Raises the ValueChanged event that you implemented.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
static PropertyDescriptorCollection GetProperties(Type componentType)
Returns the collection of properties for a specified type of component.
virtual AttributeCollection Attributes
Gets the collection of attributes for this member.
Specifies what type to use as a converter for the object this attribute is bound to.
abstract bool CanResetValue(object component)
When overridden in a derived class, returns whether resetting an object changes its value.
DesignerSerializationVisibility
Specifies the visibility a property has to the design-time serializer.
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
Represents a collection of T:System.ComponentModel.PropertyDescriptor objects.
abstract Type ComponentType
When overridden in a derived class, gets the type of the component this property is bound to.
PropertyDescriptor(MemberDescriptor descr)
Initializes a new instance of the T:System.ComponentModel.PropertyDescriptor class with the name and ...
override object GetInvocationTarget(Type type, object instance)
This method returns the object that should be used during invocation of members.
static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Gets the T:System.Type with the specified name, specifying whether to throw an exception if the type ...
Definition: Type.cs:853
override int GetHashCode()
Returns the hash code for this object.
object GetPropertyOwner(PropertyDescriptor pd)
Returns an object that contains the property described by the specified property descriptor.
abstract bool IsReadOnly
When overridden in a derived class, gets a value indicating whether this property is read-only.
virtual void AddValueChanged(object component, EventHandler handler)
Enables other objects to be notified when this property changes.
abstract void ResetValue(object component)
When overridden in a derived class, resets the value for this property of the component to the defaul...
virtual bool IsLocalizable
Gets a value indicating whether this property should be localized, as specified in the T:System....
override bool Equals(object obj)
Compares this to another object to see if they are equivalent.
Provides a unified way of converting types of values to other types, as well as for accessing standar...
virtual int NameHashCode
Gets the hash code for the name of the member, as specified in M:System.String.GetHashCode.