PropertyDescriptor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if (t == null) {
  63. converter = TypeDescriptor.GetConverter (PropertyType);
  64. }
  65. else {
  66. ConstructorInfo ci = t.GetConstructor (new Type[] { typeof(Type) });
  67. if (ci != null)
  68. converter = (TypeConverter) ci.Invoke (new object[] { PropertyType });
  69. else
  70. converter = (TypeConverter) Activator.CreateInstance (t);
  71. }
  72. }
  73. }
  74. return converter;
  75. }
  76. }
  77. public virtual bool IsLocalizable {
  78. get {
  79. foreach (Attribute attr in AttributeArray){
  80. if (attr is LocalizableAttribute)
  81. return ((LocalizableAttribute) attr).IsLocalizable;
  82. }
  83. return false;
  84. }
  85. }
  86. public abstract bool IsReadOnly { get; }
  87. public abstract Type PropertyType { get; }
  88. #if NET_2_0
  89. public virtual bool SupportsChangeEvents {
  90. get { return false; }
  91. }
  92. #endif
  93. public DesignerSerializationVisibility SerializationVisibility {
  94. get {
  95. foreach (Attribute attr in AttributeArray) {
  96. if (attr is DesignerSerializationVisibilityAttribute){
  97. DesignerSerializationVisibilityAttribute a;
  98. a = (DesignerSerializationVisibilityAttribute) attr;
  99. return a.Visibility;
  100. }
  101. }
  102. return DesignerSerializationVisibility.Visible;
  103. }
  104. }
  105. Hashtable notifiers;
  106. public virtual void AddValueChanged (object component, EventHandler handler)
  107. {
  108. EventHandler component_notifiers;
  109. if (component == null)
  110. throw new ArgumentNullException ("component");
  111. if (handler == null)
  112. throw new ArgumentNullException ("handler");
  113. if (notifiers == null)
  114. notifiers = new Hashtable ();
  115. component_notifiers = (EventHandler) notifiers [component];
  116. if (component_notifiers != null)
  117. component_notifiers += handler;
  118. else
  119. notifiers [component] = handler;
  120. }
  121. public virtual void RemoveValueChanged (object component, System.EventHandler handler)
  122. {
  123. EventHandler component_notifiers;
  124. if (component == null)
  125. throw new ArgumentNullException ("component");
  126. if (handler == null)
  127. throw new ArgumentNullException ("handler");
  128. if (notifiers == null) return;
  129. component_notifiers = (EventHandler) notifiers [component];
  130. component_notifiers -= handler;
  131. if (component_notifiers == null)
  132. notifiers.Remove (component);
  133. else
  134. notifiers [component] = handler;
  135. }
  136. #if NET_2_0
  137. [MonoNotSupported ("")]
  138. protected override void FillAttributes (IList attributeList)
  139. {
  140. base.FillAttributes (attributeList);
  141. }
  142. [MonoNotSupported ("")]
  143. protected override Object GetInvocationTarget (Type type, object instance)
  144. {
  145. return base.GetInvocationTarget (type, instance);
  146. }
  147. [MonoNotSupported("")]
  148. protected internal EventHandler GetValueChangedHandler (object component)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. #endif
  153. protected virtual void OnValueChanged (object component, EventArgs e)
  154. {
  155. if (notifiers == null)
  156. return;
  157. EventHandler component_notifiers = (EventHandler) notifiers [component];
  158. if (component_notifiers == null)
  159. return;
  160. component_notifiers (component, e);
  161. }
  162. public abstract object GetValue (object component);
  163. public abstract void SetValue (object component, object value);
  164. public abstract void ResetValue (object component);
  165. public abstract bool CanResetValue (object component);
  166. public abstract bool ShouldSerializeValue (object component);
  167. protected object CreateInstance(System.Type type)
  168. {
  169. return Assembly.GetExecutingAssembly ().CreateInstance (type.Name);
  170. }
  171. public override bool Equals(object obj)
  172. {
  173. if (!base.Equals (obj)) return false;
  174. PropertyDescriptor other = obj as PropertyDescriptor;
  175. if (other == null) return false;
  176. return other.PropertyType == PropertyType;
  177. }
  178. public PropertyDescriptorCollection GetChildProperties()
  179. {
  180. return GetChildProperties (null, null);
  181. }
  182. public PropertyDescriptorCollection GetChildProperties(object instance)
  183. {
  184. return GetChildProperties (instance, null);
  185. }
  186. public PropertyDescriptorCollection GetChildProperties(Attribute[] filter)
  187. {
  188. return GetChildProperties (null, filter);
  189. }
  190. public override int GetHashCode()
  191. {
  192. return base.GetHashCode ();
  193. }
  194. public virtual PropertyDescriptorCollection GetChildProperties (object instance, Attribute[] filter)
  195. {
  196. return TypeDescriptor.GetProperties (instance, filter);
  197. }
  198. public virtual object GetEditor(Type editorBaseType)
  199. {
  200. return TypeDescriptor.GetEditor (PropertyType, editorBaseType);
  201. }
  202. protected Type GetTypeFromName(string typeName)
  203. {
  204. return Type.GetType (typeName);
  205. }
  206. }
  207. }