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