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 TypeConverter converter;
45 protected PropertyDescriptor (MemberDescriptor reference)
50 protected PropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
51 : base (reference, attrs)
55 protected PropertyDescriptor (string name, Attribute [] attrs)
60 public abstract Type ComponentType { get; }
62 public virtual TypeConverter Converter {
64 if (converter == null) {
65 TypeConverterAttribute at = (TypeConverterAttribute) Attributes [typeof(TypeConverterAttribute)];
66 if (at == null || at == TypeConverterAttribute.Default)
67 converter = TypeDescriptor.GetConverter (PropertyType);
69 Type t = Type.GetType (at.ConverterTypeName);
70 ConstructorInfo ci = t.GetConstructor (new Type[] { typeof(Type) });
72 converter = (TypeConverter) ci.Invoke (new object[] { PropertyType });
74 converter = (TypeConverter) Activator.CreateInstance (t);
81 public virtual bool IsLocalizable {
83 foreach (Attribute attr in AttributeArray){
84 if (attr is LocalizableAttribute)
85 return ((LocalizableAttribute) attr).IsLocalizable;
92 public abstract bool IsReadOnly { get; }
94 public abstract Type PropertyType { get; }
96 public DesignerSerializationVisibility SerializationVisibility {
98 foreach (Attribute attr in AttributeArray) {
99 if (attr is DesignerSerializationVisibilityAttribute){
100 DesignerSerializationVisibilityAttribute a;
102 a = (DesignerSerializationVisibilityAttribute) attr;
108 return DesignerSerializationVisibility.Visible;
114 public virtual void AddValueChanged (object component, EventHandler handler)
116 EventHandler component_notifiers;
118 if (component == null)
119 throw new ArgumentNullException ("component");
122 throw new ArgumentNullException ("handler");
124 if (notifiers == null)
125 notifiers = new Hashtable ();
127 component_notifiers = (EventHandler) notifiers [component];
129 if (component_notifiers != null)
130 component_notifiers += handler;
132 notifiers [component] = handler;
135 public virtual void RemoveValueChanged (object component, System.EventHandler handler)
137 EventHandler component_notifiers;
139 if (component == null)
140 throw new ArgumentNullException ("component");
143 throw new ArgumentNullException ("handler");
145 if (notifiers == null) return;
147 component_notifiers = (EventHandler) notifiers [component];
148 component_notifiers -= handler;
150 if (component_notifiers == null)
151 notifiers.Remove (component);
153 notifiers [component] = handler;
156 protected virtual void OnValueChanged (object component, EventArgs e)
158 if (notifiers == null)
161 EventHandler component_notifiers = (EventHandler) notifiers [component];
163 if (component_notifiers == null)
166 component_notifiers (component, e);
169 public abstract object GetValue (object component);
171 public abstract void SetValue (object component, object value);
173 public abstract void ResetValue (object component);
175 public abstract bool CanResetValue (object component);
177 public abstract bool ShouldSerializeValue (object component);
179 protected object CreateInstance(System.Type type)
181 return Assembly.GetExecutingAssembly ().CreateInstance (type.Name);
184 public override bool Equals(object obj)
186 if (!base.Equals (obj)) return false;
187 PropertyDescriptor other = obj as PropertyDescriptor;
188 if (other == null) return false;
189 return other.PropertyType == PropertyType;
192 public PropertyDescriptorCollection GetChildProperties()
194 return GetChildProperties (null, null);
197 public PropertyDescriptorCollection GetChildProperties(object instance)
199 return GetChildProperties (instance, null);
202 public PropertyDescriptorCollection GetChildProperties(Attribute[] filter)
204 return GetChildProperties (null, filter);
207 public override int GetHashCode()
209 return base.GetHashCode ();
212 public virtual PropertyDescriptorCollection GetChildProperties (object instance, Attribute[] filter)
214 return TypeDescriptor.GetProperties (instance, filter);
217 public virtual object GetEditor(Type editorBaseType)
219 return TypeDescriptor.GetEditor (PropertyType, editorBaseType);
222 protected Type GetTypeFromName(string typeName)
224 return Type.GetType (typeName);