PropertyDescriptor.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. protected PropertyDescriptor (MemberDescriptor reference)
  41. : base (reference)
  42. {
  43. }
  44. protected PropertyDescriptor (MemberDescriptor reference, Attribute [] attrs)
  45. : base (reference, attrs)
  46. {
  47. }
  48. protected PropertyDescriptor (string name, Attribute [] attrs)
  49. : base (name, attrs)
  50. {
  51. }
  52. public abstract Type ComponentType { get; }
  53. public virtual TypeConverter Converter {
  54. get { return TypeDescriptor.GetConverter (PropertyType); }
  55. }
  56. public virtual bool IsLocalizable {
  57. get {
  58. foreach (Attribute attr in AttributeArray){
  59. if (attr is LocalizableAttribute)
  60. return ((LocalizableAttribute) attr).IsLocalizable;
  61. }
  62. return false;
  63. }
  64. }
  65. public abstract bool IsReadOnly { get; }
  66. public abstract Type PropertyType { get; }
  67. public DesignerSerializationVisibility SerializationVisibility {
  68. get {
  69. foreach (Attribute attr in AttributeArray) {
  70. if (attr is DesignerSerializationVisibilityAttribute){
  71. DesignerSerializationVisibilityAttribute a;
  72. a = (DesignerSerializationVisibilityAttribute) attr;
  73. return a.Visibility;
  74. }
  75. }
  76. //
  77. // Is this a good default if we cant find the property?
  78. //
  79. return DesignerSerializationVisibility.Hidden;
  80. }
  81. }
  82. Hashtable notifiers;
  83. public virtual void AddValueChanged (object component, EventHandler handler)
  84. {
  85. EventHandler component_notifiers;
  86. if (component == null)
  87. throw new ArgumentNullException ("component");
  88. if (handler == null)
  89. throw new ArgumentNullException ("handler");
  90. if (notifiers == null)
  91. notifiers = new Hashtable ();
  92. component_notifiers = (EventHandler) notifiers [component];
  93. if (component_notifiers != null)
  94. component_notifiers += handler;
  95. else
  96. notifiers [component] = handler;
  97. }
  98. public virtual void RemoveValueChanged (object component, System.EventHandler handler)
  99. {
  100. EventHandler component_notifiers;
  101. if (component == null)
  102. throw new ArgumentNullException ("component");
  103. if (handler == null)
  104. throw new ArgumentNullException ("handler");
  105. if (notifiers == null) return;
  106. component_notifiers = (EventHandler) notifiers [component];
  107. component_notifiers -= handler;
  108. if (component_notifiers == null)
  109. notifiers.Remove (component);
  110. else
  111. notifiers [component] = handler;
  112. }
  113. protected virtual void OnValueChanged (object component, EventArgs e)
  114. {
  115. if (notifiers == null)
  116. return;
  117. EventHandler component_notifiers = (EventHandler) notifiers [component];
  118. if (component_notifiers == null)
  119. return;
  120. component_notifiers (component, e);
  121. }
  122. public abstract object GetValue (object component);
  123. public abstract void SetValue (object component, object value);
  124. public abstract void ResetValue (object component);
  125. public abstract bool CanResetValue (object component);
  126. public abstract bool ShouldSerializeValue (object component);
  127. protected object CreateInstance(System.Type type)
  128. {
  129. return Assembly.GetExecutingAssembly ().CreateInstance (type.Name);
  130. }
  131. public override bool Equals(object obj)
  132. {
  133. if (!base.Equals (obj)) return false;
  134. PropertyDescriptor other = obj as PropertyDescriptor;
  135. if (other == null) return false;
  136. return other.PropertyType == PropertyType;
  137. }
  138. public PropertyDescriptorCollection GetChildProperties()
  139. {
  140. return GetChildProperties (null, null);
  141. }
  142. public PropertyDescriptorCollection GetChildProperties(object instance)
  143. {
  144. return GetChildProperties (instance, null);
  145. }
  146. public PropertyDescriptorCollection GetChildProperties(Attribute[] filter)
  147. {
  148. return GetChildProperties (null, filter);
  149. }
  150. public override int GetHashCode()
  151. {
  152. return base.GetHashCode ();
  153. }
  154. public virtual PropertyDescriptorCollection GetChildProperties (object instance, Attribute[] filter)
  155. {
  156. return TypeDescriptor.GetProperties (instance, filter);
  157. }
  158. public virtual object GetEditor(Type editorBaseType)
  159. {
  160. return TypeDescriptor.GetEditor (PropertyType, editorBaseType);
  161. }
  162. protected Type GetTypeFromName(string typeName)
  163. {
  164. return Type.GetType (typeName);
  165. }
  166. }
  167. }