PropertyDescriptor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // System.ComponentModel.PropertyDescriptor.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Reflection;
  34. using System.Runtime.InteropServices;
  35. namespace System.ComponentModel
  36. {
  37. [ComVisible (true)]
  38. public abstract class PropertyDescriptor : MemberDescriptor
  39. {
  40. TypeConverter converter;
  41. protected PropertyDescriptor (MemberDescriptor reference)
  42. : base (reference)
  43. {
  44. }
  45. protected PropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
  46. : base (reference, attrs)
  47. {
  48. }
  49. protected PropertyDescriptor (string name, Attribute [] attrs)
  50. : base (name, attrs)
  51. {
  52. }
  53. public abstract Type ComponentType { get; }
  54. public virtual TypeConverter Converter {
  55. get {
  56. if (converter == null) {
  57. TypeConverterAttribute at = (TypeConverterAttribute) Attributes [typeof(TypeConverterAttribute)];
  58. if (at == null || at == TypeConverterAttribute.Default)
  59. converter = TypeDescriptor.GetConverter (PropertyType);
  60. else {
  61. Type t = Type.GetType (at.ConverterTypeName);
  62. ConstructorInfo ci = t.GetConstructor (new Type[] { typeof(Type) });
  63. if (ci != null)
  64. converter = (TypeConverter) ci.Invoke (new object[] { PropertyType });
  65. else
  66. converter = (TypeConverter) Activator.CreateInstance (t);
  67. }
  68. }
  69. return converter;
  70. }
  71. }
  72. public virtual bool IsLocalizable {
  73. get {
  74. foreach (Attribute attr in AttributeArray){
  75. if (attr is LocalizableAttribute)
  76. return ((LocalizableAttribute) attr).IsLocalizable;
  77. }
  78. return false;
  79. }
  80. }
  81. public abstract bool IsReadOnly { get; }
  82. public abstract Type PropertyType { get; }
  83. public DesignerSerializationVisibility SerializationVisibility {
  84. get {
  85. foreach (Attribute attr in AttributeArray) {
  86. if (attr is DesignerSerializationVisibilityAttribute){
  87. DesignerSerializationVisibilityAttribute a;
  88. a = (DesignerSerializationVisibilityAttribute) attr;
  89. return a.Visibility;
  90. }
  91. }
  92. return DesignerSerializationVisibility.Visible;
  93. }
  94. }
  95. Hashtable notifiers;
  96. public virtual void AddValueChanged (object component, EventHandler handler)
  97. {
  98. EventHandler component_notifiers;
  99. if (component == null)
  100. throw new ArgumentNullException ("component");
  101. if (handler == null)
  102. throw new ArgumentNullException ("handler");
  103. if (notifiers == null)
  104. notifiers = new Hashtable ();
  105. component_notifiers = (EventHandler) notifiers [component];
  106. if (component_notifiers != null)
  107. component_notifiers += handler;
  108. else
  109. notifiers [component] = handler;
  110. }
  111. public virtual void RemoveValueChanged (object component, System.EventHandler handler)
  112. {
  113. EventHandler component_notifiers;
  114. if (component == null)
  115. throw new ArgumentNullException ("component");
  116. if (handler == null)
  117. throw new ArgumentNullException ("handler");
  118. if (notifiers == null) return;
  119. component_notifiers = (EventHandler) notifiers [component];
  120. component_notifiers -= handler;
  121. if (component_notifiers == null)
  122. notifiers.Remove (component);
  123. else
  124. notifiers [component] = handler;
  125. }
  126. protected virtual void OnValueChanged (object component, EventArgs e)
  127. {
  128. if (notifiers == null)
  129. return;
  130. EventHandler component_notifiers = (EventHandler) notifiers [component];
  131. if (component_notifiers == null)
  132. return;
  133. component_notifiers (component, e);
  134. }
  135. public abstract object GetValue (object component);
  136. public abstract void SetValue (object component, object value);
  137. public abstract void ResetValue (object component);
  138. public abstract bool CanResetValue (object component);
  139. public abstract bool ShouldSerializeValue (object component);
  140. protected object CreateInstance(System.Type type)
  141. {
  142. return Assembly.GetExecutingAssembly ().CreateInstance (type.Name);
  143. }
  144. public override bool Equals(object obj)
  145. {
  146. if (!base.Equals (obj)) return false;
  147. PropertyDescriptor other = obj as PropertyDescriptor;
  148. if (other == null) return false;
  149. return other.PropertyType == PropertyType;
  150. }
  151. public PropertyDescriptorCollection GetChildProperties()
  152. {
  153. return GetChildProperties (null, null);
  154. }
  155. public PropertyDescriptorCollection GetChildProperties(object instance)
  156. {
  157. return GetChildProperties (instance, null);
  158. }
  159. public PropertyDescriptorCollection GetChildProperties(Attribute[] filter)
  160. {
  161. return GetChildProperties (null, filter);
  162. }
  163. public override int GetHashCode()
  164. {
  165. return base.GetHashCode ();
  166. }
  167. public virtual PropertyDescriptorCollection GetChildProperties (object instance, Attribute[] filter)
  168. {
  169. return TypeDescriptor.GetProperties (instance, filter);
  170. }
  171. public virtual object GetEditor(Type editorBaseType)
  172. {
  173. return TypeDescriptor.GetEditor (PropertyType, editorBaseType);
  174. }
  175. protected Type GetTypeFromName(string typeName)
  176. {
  177. return Type.GetType (typeName);
  178. }
  179. }
  180. }