2 // System.ComponentModel.MemberDescriptor.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Ivan N. Zlatev <contact@i-nz.net>
9 // (C) Ximian, Inc. http://www.ximian.com
10 // (C) 2003 Andreas Nahr
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Reflection;
37 using System.Runtime.InteropServices;
38 using System.ComponentModel.Design;
40 namespace System.ComponentModel
43 public abstract class MemberDescriptor
46 private Attribute [] attrs;
47 private AttributeCollection attrCollection;
49 protected MemberDescriptor (string name, Attribute [] attrs)
55 protected MemberDescriptor (MemberDescriptor reference, Attribute [] attrs)
57 name = reference.name;
61 protected MemberDescriptor (string name)
66 protected MemberDescriptor (MemberDescriptor reference)
68 name = reference.name;
69 attrs = reference.AttributeArray;
72 protected virtual Attribute [] AttributeArray {
75 ArrayList list = new ArrayList ();
76 FillAttributes (list);
78 ArrayList filtered = new ArrayList ();
79 foreach (Attribute at in list) {
81 for (int n = 0; n < filtered.Count && !found; n++)
82 found = (filtered [n].GetType () == at.GetType ());
86 attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
96 protected virtual void FillAttributes(System.Collections.IList attributeList)
101 public virtual AttributeCollection Attributes {
103 if (attrCollection == null)
104 attrCollection = CreateAttributeCollection ();
105 return attrCollection;
109 protected virtual AttributeCollection CreateAttributeCollection ()
111 return new AttributeCollection (AttributeArray);
114 public virtual string Category {
116 return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
120 public virtual string Description {
122 foreach (Attribute attr in AttributeArray)
123 if (attr is DescriptionAttribute)
124 return ((DescriptionAttribute) attr).Description;
129 public virtual bool DesignTimeOnly {
131 foreach (Attribute attr in AttributeArray)
132 if (attr is DesignOnlyAttribute)
133 return ((DesignOnlyAttribute) attr).IsDesignOnly;
138 public virtual string DisplayName {
141 foreach (Attribute attr in AttributeArray)
142 if (attr is DisplayNameAttribute)
143 return ((DisplayNameAttribute) attr).DisplayName;
149 public virtual string Name {
155 public virtual bool IsBrowsable {
157 foreach (Attribute attr in AttributeArray)
158 if (attr is BrowsableAttribute)
159 return ((BrowsableAttribute) attr).Browsable;
164 protected virtual int NameHashCode {
166 return name.GetHashCode ();
170 public override int GetHashCode ()
172 return base.GetHashCode ();
175 public override bool Equals (object obj)
177 MemberDescriptor other = obj as MemberDescriptor;
180 return other.name == name;
183 protected static ISite GetSite (object component)
185 if (component is Component)
186 return ((Component) component).Site;
192 [Obsolete ("Use GetInvocationTarget")]
194 protected static object GetInvokee (Type componentClass, object component)
196 if (component is IComponent) {
197 ISite site = ((IComponent) component).Site;
198 if (site != null && site.DesignMode) {
199 IDesignerHost host = site.GetService (typeof (IDesignerHost)) as IDesignerHost;
201 IDesigner designer = host.GetDesigner ((IComponent) component);
202 if (designer != null && componentClass.IsInstanceOfType (designer)) {
203 component = designer;
212 protected virtual object GetInvocationTarget (Type type, object instance)
215 throw new ArgumentNullException ("type");
216 if (instance == null)
217 throw new ArgumentNullException ("instance");
219 return GetInvokee (type, instance);
223 protected static MethodInfo FindMethod(Type componentClass, string name, Type[] args, Type returnType)
225 return FindMethod (componentClass, name, args, returnType, true);
228 protected static MethodInfo FindMethod(Type componentClass, string name, Type[] args, Type returnType, bool publicOnly)
231 if (publicOnly == true)
232 bf = BindingFlags.Public;
234 bf = BindingFlags.NonPublic | BindingFlags.Public;
235 // FIXME returnType is not taken into account. AFAIK methods are not allowed to only
236 // differ by return type anyway
237 return componentClass.GetMethod (name, bf, null, CallingConventions.Any, args, null);