* AttributeCollection.cs: Added new internal constructor.
[mono.git] / mcs / class / System / System.ComponentModel / MemberDescriptor.cs
1 //
2 // System.ComponentModel.MemberDescriptor.cs
3 //
4 // Author:
5 //  Miguel de Icaza (miguel@ximian.com)
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) 2003 Andreas Nahr
10 //
11
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Runtime.InteropServices;
16
17 namespace System.ComponentModel
18 {
19
20     [ComVisible (true)]
21     public abstract class MemberDescriptor 
22     {
23
24         private string name;
25         private Attribute [] attrs;
26         private AttributeCollection attrCollection;
27                 
28         protected MemberDescriptor (string name, Attribute [] attrs)
29         {
30             this.name = name;
31             this.attrs = attrs;
32         }
33
34         protected MemberDescriptor (MemberDescriptor reference, Attribute [] attrs)
35         {
36             name = reference.name;
37             this.attrs = attrs;
38         }
39
40         protected MemberDescriptor (string name)
41         {
42             this.name = name;
43         }
44
45         protected MemberDescriptor (MemberDescriptor reference)
46         {
47             name = reference.name;
48             attrs = reference.AttributeArray;
49         }
50
51         protected virtual Attribute [] AttributeArray 
52         {
53             get 
54             {
55                                 if (attrs == null) 
56                                 {
57                                         ArrayList list = new ArrayList ();
58                                         FillAttributes (list);
59                                         
60                                         ArrayList filtered = new ArrayList ();
61                                         foreach (Attribute at in list) {
62                                                 bool found = false;
63                                                 for (int n=0; n<filtered.Count && !found; n++)
64                                                         found = (filtered[n].GetType() == at.GetType ());
65                                                 if (!found)
66                                                         filtered.Add (at);
67                                         }
68                                         attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
69                                 }
70                                 
71                 return attrs;
72             }
73
74             set 
75             {
76                 attrs = value;
77             }
78         }
79
80         protected virtual void FillAttributes(System.Collections.IList attributeList)
81         {
82                         // to be overriden
83         }
84
85         public virtual AttributeCollection Attributes
86         {
87             get 
88             {
89                 if (attrCollection == null)
90                     attrCollection = CreateAttributeCollection ();
91                 return attrCollection;
92             }
93         }
94
95         protected virtual AttributeCollection CreateAttributeCollection()
96         {
97             return new AttributeCollection (AttributeArray);
98         }
99                         
100         public virtual string Category 
101         {
102             get 
103             {
104                 return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
105             }
106         }
107
108         public virtual string Description 
109         {
110             get 
111             {
112                 foreach (Attribute attr in AttributeArray)
113                 {
114                     if (attr is DescriptionAttribute)
115                         return ((DescriptionAttribute) attr).Description;
116                 }
117                 return "";
118             }
119         }
120
121         public virtual bool DesignTimeOnly 
122         {
123             get 
124             {
125                 foreach (Attribute attr in AttributeArray)
126                 {
127                     if (attr is DesignOnlyAttribute)
128                         return ((DesignOnlyAttribute) attr).IsDesignOnly;
129                 }
130
131                 return false;
132             }
133         }
134
135         public virtual string DisplayName 
136         {
137             get 
138             {
139                 return name;
140             }
141         }
142
143         public virtual string Name 
144         {
145             get 
146             {
147                 return name;
148             }
149         }
150
151         public virtual bool IsBrowsable 
152         {
153             get 
154             {
155                 foreach (Attribute attr in AttributeArray)
156                 {
157                     if (attr is BrowsableAttribute)
158                         return ((BrowsableAttribute) attr).Browsable;
159                 }
160
161                 return false;
162             }
163         }
164
165         protected virtual int NameHashCode 
166         {
167             get 
168             {
169                 return name.GetHashCode ();
170             }
171         }
172
173         public override int GetHashCode() 
174         {
175             return base.GetHashCode ();
176         }
177
178         public override bool Equals(object obj)
179         {
180                         MemberDescriptor other = obj as MemberDescriptor;
181             if (other == null) return false;
182                         
183             return other.name == name;
184         }
185
186         protected static ISite GetSite(object component)
187         {
188             if (component is Component)
189                 return ((Component) component).Site;
190             else
191                 return null;
192         }
193
194         [MonoTODO]
195         protected static object GetInvokee(Type componentClass, object component)
196         {
197             // FIXME WHAT should that do???
198                         
199                         // Lluis: Checked with VS.NET and it always return the component, even if
200                         // it has its own designer set with DesignerAttribute. So, no idea
201                         // what this should do.
202             return component;
203         }
204
205         protected static MethodInfo FindMethod(Type componentClass, string name, 
206             Type[ ] args, Type returnType)
207         {
208             return FindMethod (componentClass, name, args, returnType, true);
209         }
210
211         protected static MethodInfo FindMethod(Type componentClass, string name, 
212             Type[ ] args, Type returnType, bool publicOnly)
213         {
214             BindingFlags bf;
215             if (publicOnly == true)
216                 bf = BindingFlags.Public;
217             else
218                 bf = BindingFlags.NonPublic | BindingFlags.Public;
219             // FIXME returnType is not taken into account. AFAIK methods are not allowed to only
220             // differ by return type anyway
221             return componentClass.GetMethod (name, bf, null, CallingConventions.Any, args, null);
222         }
223     }
224 }