2 // System.ComponentModel.MemberDescriptor.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
38 namespace System.ComponentModel
42 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
78 ArrayList list = new ArrayList ();
79 FillAttributes (list);
81 ArrayList filtered = new ArrayList ();
82 foreach (Attribute at in list) {
84 for (int n=0; n<filtered.Count && !found; n++)
85 found = (filtered[n].GetType() == at.GetType ());
89 attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
101 protected virtual void FillAttributes(System.Collections.IList attributeList)
106 public virtual AttributeCollection Attributes
110 if (attrCollection == null)
111 attrCollection = CreateAttributeCollection ();
112 return attrCollection;
116 protected virtual AttributeCollection CreateAttributeCollection()
118 return new AttributeCollection (AttributeArray);
121 public virtual string Category
125 return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
129 public virtual string Description
133 foreach (Attribute attr in AttributeArray)
135 if (attr is DescriptionAttribute)
136 return ((DescriptionAttribute) attr).Description;
142 public virtual bool DesignTimeOnly
146 foreach (Attribute attr in AttributeArray)
148 if (attr is DesignOnlyAttribute)
149 return ((DesignOnlyAttribute) attr).IsDesignOnly;
156 public virtual string DisplayName
164 public virtual string Name
172 public virtual bool IsBrowsable
176 foreach (Attribute attr in AttributeArray)
178 if (attr is BrowsableAttribute)
179 return ((BrowsableAttribute) attr).Browsable;
186 protected virtual int NameHashCode
190 return name.GetHashCode ();
194 public override int GetHashCode()
196 return base.GetHashCode ();
199 public override bool Equals(object obj)
201 MemberDescriptor other = obj as MemberDescriptor;
202 if (other == null) return false;
204 return other.name == name;
207 protected static ISite GetSite(object component)
209 if (component is Component)
210 return ((Component) component).Site;
216 protected static object GetInvokee(Type componentClass, object component)
218 // FIXME WHAT should that do???
220 // Lluis: Checked with VS.NET and it always return the component, even if
221 // it has its own designer set with DesignerAttribute. So, no idea
222 // what this should do.
226 protected static MethodInfo FindMethod(Type componentClass, string name,
227 Type[ ] args, Type returnType)
229 return FindMethod (componentClass, name, args, returnType, true);
232 protected static MethodInfo FindMethod(Type componentClass, string name,
233 Type[ ] args, Type returnType, bool publicOnly)
236 if (publicOnly == true)
237 bf = BindingFlags.Public;
239 bf = BindingFlags.NonPublic | BindingFlags.Public;
240 // FIXME returnType is not taken into account. AFAIK methods are not allowed to only
241 // differ by return type anyway
242 return componentClass.GetMethod (name, bf, null, CallingConventions.Any, args, null);