2 // System.ComponentModel.PropertyDescriptor.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System
.Collections
;
35 using System
.Reflection
;
36 using System
.Runtime
.InteropServices
;
38 namespace System
.ComponentModel
41 public abstract class PropertyDescriptor
: MemberDescriptor
43 protected PropertyDescriptor (MemberDescriptor reference
)
48 protected PropertyDescriptor (MemberDescriptor reference
, Attribute
[] attrs
)
49 : base (reference
, attrs
)
53 protected PropertyDescriptor (string name
, Attribute
[] attrs
)
58 public abstract Type ComponentType { get; }
60 public virtual TypeConverter Converter
{
61 get { return TypeDescriptor.GetConverter (PropertyType); }
64 public virtual bool IsLocalizable
{
66 foreach (Attribute attr
in AttributeArray
){
67 if (attr
is LocalizableAttribute
)
68 return ((LocalizableAttribute
) attr
).IsLocalizable
;
75 public abstract bool IsReadOnly { get; }
77 public abstract Type PropertyType { get; }
79 public DesignerSerializationVisibility SerializationVisibility
{
81 foreach (Attribute attr
in AttributeArray
) {
82 if (attr
is DesignerSerializationVisibilityAttribute
){
83 DesignerSerializationVisibilityAttribute a
;
85 a
= (DesignerSerializationVisibilityAttribute
) attr
;
92 // Is this a good default if we cant find the property?
94 return DesignerSerializationVisibility
.Hidden
;
100 public virtual void AddValueChanged (object component
, EventHandler handler
)
102 EventHandler component_notifiers
;
104 if (component
== null)
105 throw new ArgumentNullException ("component");
108 throw new ArgumentNullException ("handler");
110 if (notifiers
== null)
111 notifiers
= new Hashtable ();
113 component_notifiers
= (EventHandler
) notifiers
[component
];
115 if (component_notifiers
!= null)
116 component_notifiers
+= handler
;
118 notifiers
[component
] = handler
;
121 public virtual void RemoveValueChanged (object component
, System
.EventHandler handler
)
123 EventHandler component_notifiers
;
125 if (component
== null)
126 throw new ArgumentNullException ("component");
129 throw new ArgumentNullException ("handler");
131 if (notifiers
== null) return;
133 component_notifiers
= (EventHandler
) notifiers
[component
];
134 component_notifiers
-= handler
;
136 if (component_notifiers
== null)
137 notifiers
.Remove (component
);
139 notifiers
[component
] = handler
;
142 protected virtual void OnValueChanged (object component
, EventArgs e
)
144 if (notifiers
== null)
147 EventHandler component_notifiers
= (EventHandler
) notifiers
[component
];
149 if (component_notifiers
== null)
152 component_notifiers (component
, e
);
155 public abstract object GetValue (object component
);
157 public abstract void SetValue (object component
, object value);
159 public abstract void ResetValue (object component
);
161 public abstract bool CanResetValue (object component
);
163 public abstract bool ShouldSerializeValue (object component
);
165 protected object CreateInstance(System
.Type type
)
167 return Assembly
.GetExecutingAssembly ().CreateInstance (type
.Name
);
170 public override bool Equals(object obj
)
172 if (!base.Equals (obj
)) return false;
173 PropertyDescriptor other
= obj
as PropertyDescriptor
;
174 if (other
== null) return false;
175 return other
.PropertyType
== PropertyType
;
178 public PropertyDescriptorCollection
GetChildProperties()
180 return GetChildProperties (null, null);
183 public PropertyDescriptorCollection
GetChildProperties(object instance
)
185 return GetChildProperties (instance
, null);
188 public PropertyDescriptorCollection
GetChildProperties(Attribute
[] filter
)
190 return GetChildProperties (null, filter
);
193 public override int GetHashCode()
195 return base.GetHashCode ();
198 public virtual PropertyDescriptorCollection
GetChildProperties (object instance
, Attribute
[] filter
)
200 return TypeDescriptor
.GetProperties (instance
, filter
);
203 public virtual object GetEditor(Type editorBaseType
)
205 return TypeDescriptor
.GetEditor (PropertyType
, editorBaseType
);
208 protected Type
GetTypeFromName(string typeName
)
210 return Type
.GetType (typeName
);