MemberDescriptor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. //
  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 MemberDescriptor
  39. {
  40. private string name;
  41. private Attribute [] attrs;
  42. private AttributeCollection attrCollection;
  43. protected MemberDescriptor (string name, Attribute [] attrs)
  44. {
  45. this.name = name;
  46. this.attrs = attrs;
  47. }
  48. protected MemberDescriptor (MemberDescriptor reference, Attribute [] attrs)
  49. {
  50. name = reference.name;
  51. this.attrs = attrs;
  52. }
  53. protected MemberDescriptor (string name)
  54. {
  55. this.name = name;
  56. }
  57. protected MemberDescriptor (MemberDescriptor reference)
  58. {
  59. name = reference.name;
  60. attrs = reference.AttributeArray;
  61. }
  62. protected virtual Attribute [] AttributeArray
  63. {
  64. get
  65. {
  66. if (attrs == null)
  67. {
  68. ArrayList list = new ArrayList ();
  69. FillAttributes (list);
  70. ArrayList filtered = new ArrayList ();
  71. foreach (Attribute at in list) {
  72. bool found = false;
  73. for (int n=0; n<filtered.Count && !found; n++)
  74. found = (filtered[n].GetType() == at.GetType ());
  75. if (!found)
  76. filtered.Add (at);
  77. }
  78. attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
  79. }
  80. return attrs;
  81. }
  82. set
  83. {
  84. attrs = value;
  85. }
  86. }
  87. protected virtual void FillAttributes(System.Collections.IList attributeList)
  88. {
  89. // to be overriden
  90. }
  91. public virtual AttributeCollection Attributes
  92. {
  93. get
  94. {
  95. if (attrCollection == null)
  96. attrCollection = CreateAttributeCollection ();
  97. return attrCollection;
  98. }
  99. }
  100. protected virtual AttributeCollection CreateAttributeCollection()
  101. {
  102. return new AttributeCollection (AttributeArray);
  103. }
  104. public virtual string Category
  105. {
  106. get
  107. {
  108. return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
  109. }
  110. }
  111. public virtual string Description
  112. {
  113. get
  114. {
  115. foreach (Attribute attr in AttributeArray)
  116. {
  117. if (attr is DescriptionAttribute)
  118. return ((DescriptionAttribute) attr).Description;
  119. }
  120. return "";
  121. }
  122. }
  123. public virtual bool DesignTimeOnly
  124. {
  125. get
  126. {
  127. foreach (Attribute attr in AttributeArray)
  128. {
  129. if (attr is DesignOnlyAttribute)
  130. return ((DesignOnlyAttribute) attr).IsDesignOnly;
  131. }
  132. return false;
  133. }
  134. }
  135. public virtual string DisplayName
  136. {
  137. get
  138. {
  139. return name;
  140. }
  141. }
  142. public virtual string Name
  143. {
  144. get
  145. {
  146. return name;
  147. }
  148. }
  149. public virtual bool IsBrowsable
  150. {
  151. get
  152. {
  153. foreach (Attribute attr in AttributeArray)
  154. {
  155. if (attr is BrowsableAttribute)
  156. return ((BrowsableAttribute) attr).Browsable;
  157. }
  158. return true;
  159. }
  160. }
  161. protected virtual int NameHashCode
  162. {
  163. get
  164. {
  165. return name.GetHashCode ();
  166. }
  167. }
  168. public override int GetHashCode()
  169. {
  170. return base.GetHashCode ();
  171. }
  172. public override bool Equals(object obj)
  173. {
  174. MemberDescriptor other = obj as MemberDescriptor;
  175. if (other == null) return false;
  176. return other.name == name;
  177. }
  178. protected static ISite GetSite(object component)
  179. {
  180. if (component is Component)
  181. return ((Component) component).Site;
  182. else
  183. return null;
  184. }
  185. [MonoTODO]
  186. protected static object GetInvokee(Type componentClass, object component)
  187. {
  188. // FIXME WHAT should that do???
  189. // Lluis: Checked with VS.NET and it always return the component, even if
  190. // it has its own designer set with DesignerAttribute. So, no idea
  191. // what this should do.
  192. return component;
  193. }
  194. protected static MethodInfo FindMethod(Type componentClass, string name,
  195. Type[ ] args, Type returnType)
  196. {
  197. return FindMethod (componentClass, name, args, returnType, true);
  198. }
  199. protected static MethodInfo FindMethod(Type componentClass, string name,
  200. Type[ ] args, Type returnType, bool publicOnly)
  201. {
  202. BindingFlags bf;
  203. if (publicOnly == true)
  204. bf = BindingFlags.Public;
  205. else
  206. bf = BindingFlags.NonPublic | BindingFlags.Public;
  207. // FIXME returnType is not taken into account. AFAIK methods are not allowed to only
  208. // differ by return type anyway
  209. return componentClass.GetMethod (name, bf, null, CallingConventions.Any, args, null);
  210. }
  211. }
  212. }