MemberDescriptor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // System.ComponentModel.MemberDescriptor.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. using System;
  12. using System.Reflection;
  13. using System.Runtime.InteropServices;
  14. namespace System.ComponentModel
  15. {
  16. [ComVisible (true)]
  17. public abstract class MemberDescriptor
  18. {
  19. private string name;
  20. private string displayName;
  21. private Attribute [] attrs;
  22. private AttributeCollection attrCollection;
  23. protected MemberDescriptor (string name, Attribute [] attrs)
  24. {
  25. this.name = name;
  26. this.displayName = name;
  27. this.attrs = attrs;
  28. }
  29. protected MemberDescriptor (MemberDescriptor reference, Attribute [] attrs)
  30. {
  31. name = reference.name;
  32. this.displayName = name;
  33. this.attrs = attrs;
  34. }
  35. protected MemberDescriptor (string name)
  36. {
  37. this.name = name;
  38. this.displayName = name;
  39. }
  40. protected MemberDescriptor (MemberDescriptor reference)
  41. {
  42. name = reference.name;
  43. this.displayName = name;
  44. attrs = reference.attrs;
  45. }
  46. protected virtual Attribute [] AttributeArray
  47. {
  48. get
  49. {
  50. return attrs;
  51. }
  52. set
  53. {
  54. attrs = value;
  55. }
  56. }
  57. [MonoTODO]
  58. protected virtual void FillAttributes(System.Collections.IList attributeList)
  59. {
  60. // LAMESPEC/FIXME - I don't think this is correct, but didn't really understand
  61. // what this sub is good for
  62. attributeList = this.attrs;
  63. return;
  64. }
  65. public virtual AttributeCollection Attributes
  66. {
  67. get
  68. {
  69. if (attrCollection == null)
  70. attrCollection = CreateAttributeCollection ();
  71. return attrCollection;
  72. }
  73. }
  74. protected virtual AttributeCollection CreateAttributeCollection()
  75. {
  76. return new AttributeCollection (attrs);
  77. }
  78. public virtual string Category
  79. {
  80. get
  81. {
  82. return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
  83. }
  84. }
  85. public virtual string Description
  86. {
  87. get
  88. {
  89. foreach (Attribute attr in attrs)
  90. {
  91. if (attr is DescriptionAttribute)
  92. return ((DescriptionAttribute) attr).Description;
  93. }
  94. return "";
  95. }
  96. }
  97. public virtual bool DesignTimeOnly
  98. {
  99. get
  100. {
  101. foreach (Attribute attr in attrs)
  102. {
  103. if (attr is DesignOnlyAttribute)
  104. return ((DesignOnlyAttribute) attr).IsDesignOnly;
  105. }
  106. return false;
  107. }
  108. }
  109. public virtual string DisplayName
  110. {
  111. get
  112. {
  113. return displayName;
  114. }
  115. }
  116. public virtual string Name
  117. {
  118. get
  119. {
  120. return name;
  121. }
  122. }
  123. public virtual bool IsBrowsable
  124. {
  125. get
  126. {
  127. foreach (Attribute attr in attrs)
  128. {
  129. if (attr is BrowsableAttribute)
  130. return ((BrowsableAttribute) attr).Browsable;
  131. }
  132. return false;
  133. }
  134. }
  135. protected virtual int NameHashCode
  136. {
  137. get
  138. {
  139. return name.GetHashCode ();
  140. }
  141. }
  142. public override int GetHashCode()
  143. {
  144. return name.GetHashCode ();
  145. }
  146. [MonoTODO ("Probably not correctly implemented (too harsh?)")]
  147. public override bool Equals(object obj)
  148. {
  149. if (!(obj is MemberDescriptor))
  150. return false;
  151. if (obj == this)
  152. return true;
  153. return (((MemberDescriptor) obj).AttributeArray == attrs) &&
  154. (((MemberDescriptor) obj).Attributes == attrCollection) &&
  155. (((MemberDescriptor) obj).DisplayName == displayName) &&
  156. (((MemberDescriptor) obj).Name == name);
  157. }
  158. protected static ISite GetSite(object component)
  159. {
  160. if (component is Component)
  161. return ((Component) component).Site;
  162. else
  163. return null;
  164. }
  165. [MonoTODO]
  166. protected static object GetInvokee(Type componentClass, object component)
  167. {
  168. // FIXME WHAT should that do???
  169. throw new NotImplementedException ();
  170. }
  171. protected static MethodInfo FindMethod(Type componentClass, string name,
  172. Type[ ] args, Type returnType)
  173. {
  174. return FindMethod (componentClass, name, args, returnType, true);
  175. }
  176. protected static MethodInfo FindMethod(Type componentClass, string name,
  177. Type[ ] args, Type returnType, bool publicOnly)
  178. {
  179. BindingFlags bf;
  180. if (publicOnly == true)
  181. bf = BindingFlags.Public;
  182. else
  183. bf = BindingFlags.NonPublic | BindingFlags.Public;
  184. // FIXME returnType is not taken into account. AFAIK methods are not allowed to only
  185. // differ by return type anyway
  186. return componentClass.GetMethod (name, bf, null, CallingConventions.Any, args, null);
  187. }
  188. }
  189. }