Add MIT license to System.dll
[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 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37
38 namespace System.ComponentModel
39 {
40
41     [ComVisible (true)]
42     public abstract class MemberDescriptor 
43     {
44
45         private string name;
46         private Attribute [] attrs;
47         private AttributeCollection attrCollection;
48                 
49         protected MemberDescriptor (string name, Attribute [] attrs)
50         {
51             this.name = name;
52             this.attrs = attrs;
53         }
54
55         protected MemberDescriptor (MemberDescriptor reference, Attribute [] attrs)
56         {
57             name = reference.name;
58             this.attrs = attrs;
59         }
60
61         protected MemberDescriptor (string name)
62         {
63             this.name = name;
64         }
65
66         protected MemberDescriptor (MemberDescriptor reference)
67         {
68             name = reference.name;
69             attrs = reference.AttributeArray;
70         }
71
72         protected virtual Attribute [] AttributeArray 
73         {
74             get 
75             {
76                                 if (attrs == null) 
77                                 {
78                                         ArrayList list = new ArrayList ();
79                                         FillAttributes (list);
80                                         
81                                         ArrayList filtered = new ArrayList ();
82                                         foreach (Attribute at in list) {
83                                                 bool found = false;
84                                                 for (int n=0; n<filtered.Count && !found; n++)
85                                                         found = (filtered[n].GetType() == at.GetType ());
86                                                 if (!found)
87                                                         filtered.Add (at);
88                                         }
89                                         attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
90                                 }
91                                 
92                 return attrs;
93             }
94
95             set 
96             {
97                 attrs = value;
98             }
99         }
100
101         protected virtual void FillAttributes(System.Collections.IList attributeList)
102         {
103                         // to be overriden
104         }
105
106         public virtual AttributeCollection Attributes
107         {
108             get 
109             {
110                 if (attrCollection == null)
111                     attrCollection = CreateAttributeCollection ();
112                 return attrCollection;
113             }
114         }
115
116         protected virtual AttributeCollection CreateAttributeCollection()
117         {
118             return new AttributeCollection (AttributeArray);
119         }
120                         
121         public virtual string Category 
122         {
123             get 
124             {
125                 return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
126             }
127         }
128
129         public virtual string Description 
130         {
131             get 
132             {
133                 foreach (Attribute attr in AttributeArray)
134                 {
135                     if (attr is DescriptionAttribute)
136                         return ((DescriptionAttribute) attr).Description;
137                 }
138                 return "";
139             }
140         }
141
142         public virtual bool DesignTimeOnly 
143         {
144             get 
145             {
146                 foreach (Attribute attr in AttributeArray)
147                 {
148                     if (attr is DesignOnlyAttribute)
149                         return ((DesignOnlyAttribute) attr).IsDesignOnly;
150                 }
151
152                 return false;
153             }
154         }
155
156         public virtual string DisplayName 
157         {
158             get 
159             {
160                 return name;
161             }
162         }
163
164         public virtual string Name 
165         {
166             get 
167             {
168                 return name;
169             }
170         }
171
172         public virtual bool IsBrowsable 
173         {
174             get 
175             {
176                 foreach (Attribute attr in AttributeArray)
177                 {
178                     if (attr is BrowsableAttribute)
179                         return ((BrowsableAttribute) attr).Browsable;
180                 }
181
182                 return false;
183             }
184         }
185
186         protected virtual int NameHashCode 
187         {
188             get 
189             {
190                 return name.GetHashCode ();
191             }
192         }
193
194         public override int GetHashCode() 
195         {
196             return base.GetHashCode ();
197         }
198
199         public override bool Equals(object obj)
200         {
201                         MemberDescriptor other = obj as MemberDescriptor;
202             if (other == null) return false;
203                         
204             return other.name == name;
205         }
206
207         protected static ISite GetSite(object component)
208         {
209             if (component is Component)
210                 return ((Component) component).Site;
211             else
212                 return null;
213         }
214
215         [MonoTODO]
216         protected static object GetInvokee(Type componentClass, object component)
217         {
218             // FIXME WHAT should that do???
219                         
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.
223             return component;
224         }
225
226         protected static MethodInfo FindMethod(Type componentClass, string name, 
227             Type[ ] args, Type returnType)
228         {
229             return FindMethod (componentClass, name, args, returnType, true);
230         }
231
232         protected static MethodInfo FindMethod(Type componentClass, string name, 
233             Type[ ] args, Type returnType, bool publicOnly)
234         {
235             BindingFlags bf;
236             if (publicOnly == true)
237                 bf = BindingFlags.Public;
238             else
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);
243         }
244     }
245 }