* System.ComponentModel/MaskedTextProvider.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 //  Ivan N. Zlatev <contact@i-nz.net>
8 //
9 // (C) Ximian, Inc.  http://www.ximian.com
10 // (C) 2003 Andreas Nahr
11 //
12
13 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.
32 //
33
34 using System;
35 using System.Collections;
36 using System.Reflection;
37 using System.Runtime.InteropServices;
38 using System.ComponentModel.Design;
39
40 namespace System.ComponentModel
41 {
42         [ComVisible (true)]
43         public abstract class MemberDescriptor
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                         get {
74                                 if (attrs == null) {
75                                         ArrayList list = new ArrayList ();
76                                         FillAttributes (list);
77
78                                         ArrayList filtered = new ArrayList ();
79                                         foreach (Attribute at in list) {
80                                                 bool found = false;
81                                                 for (int n = 0; n < filtered.Count && !found; n++)
82                                                         found = (filtered [n].GetType () == at.GetType ());
83                                                 if (!found)
84                                                         filtered.Add (at);
85                                         }
86                                         attrs = (Attribute[]) filtered.ToArray (typeof(Attribute));
87                                 }
88
89                                 return attrs;
90                         }
91                         set {
92                                 attrs = value;
93                         }
94                 }
95
96                 protected virtual void FillAttributes(System.Collections.IList attributeList)
97                 {
98                         // to be overriden
99                 }
100
101                 public virtual AttributeCollection Attributes {
102                         get {
103                                 if (attrCollection == null)
104                                         attrCollection = CreateAttributeCollection ();
105                                 return attrCollection;
106                         }
107                 }
108
109                 protected virtual AttributeCollection CreateAttributeCollection ()
110                 {
111                         return new AttributeCollection (AttributeArray);
112                 }
113
114                 public virtual string Category {
115                         get {
116                                 return ((CategoryAttribute) Attributes [typeof (CategoryAttribute)]).Category;
117                         }
118                 }
119
120                 public virtual string Description {
121                         get {
122                                 foreach (Attribute attr in AttributeArray)
123                                         if (attr is DescriptionAttribute)
124                                                 return ((DescriptionAttribute) attr).Description;
125                                 return string.Empty;
126                         }
127                 }
128
129                 public virtual bool DesignTimeOnly {
130                         get {
131                                 foreach (Attribute attr in AttributeArray)
132                                         if (attr is DesignOnlyAttribute)
133                                                 return ((DesignOnlyAttribute) attr).IsDesignOnly;
134                                 return false;
135                         }
136                 }
137
138                 public virtual string DisplayName {
139                         get {
140 #if NET_2_0
141                                 foreach (Attribute attr in AttributeArray)
142                                         if (attr is DisplayNameAttribute)
143                                                 return ((DisplayNameAttribute) attr).DisplayName;
144 #endif
145                                 return name;
146                         }
147                 }
148
149                 public virtual string Name {
150                         get {
151                                 return name;
152                         }
153                 }
154
155                 public virtual bool IsBrowsable {
156                         get {
157                                 foreach (Attribute attr in AttributeArray)
158                                         if (attr is BrowsableAttribute)
159                                                 return ((BrowsableAttribute) attr).Browsable;
160                                 return true;
161                         }
162                 }
163
164                 protected virtual int NameHashCode {
165                         get {
166                                 return name.GetHashCode ();
167                         }
168                 }
169
170                 public override int GetHashCode ()
171                 {
172                         return base.GetHashCode ();
173                 }
174
175                 public override bool Equals (object obj)
176                 {
177                         MemberDescriptor other = obj as MemberDescriptor;
178                         if (other == null)
179                                 return false;
180                         return other.name == name;
181                 }
182
183                 protected static ISite GetSite (object component)
184                 {
185                         if (component is Component)
186                                 return ((Component) component).Site;
187                         else
188                                 return null;
189                 }
190
191 #if NET_2_0
192                 [Obsolete ("Use GetInvocationTarget")]
193 #endif
194                 protected static object GetInvokee (Type componentClass, object component)
195                 {
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;
200                                         if (host != null) {
201                                                 IDesigner designer = host.GetDesigner ((IComponent) component);
202                                                 if (designer != null && componentClass.IsInstanceOfType (designer)) {
203                                                         component = designer;
204                                                 }
205                                         }
206                                 }
207                         }
208                         return component;
209                 }
210
211 #if NET_2_0
212                 protected virtual object GetInvocationTarget (Type type, object instance)
213                 {
214                         if (type == null)
215                                 throw new ArgumentNullException ("type");
216                         if (instance == null)
217                                 throw new ArgumentNullException ("instance");
218
219                         return GetInvokee (type, instance);
220                 }
221 #endif
222
223                 protected static MethodInfo FindMethod(Type componentClass, string name, Type[] args, Type returnType)
224                 {
225                         return FindMethod (componentClass, name, args, returnType, true);
226                 }
227
228                 protected static MethodInfo FindMethod(Type componentClass, string name, Type[] args, Type returnType, bool publicOnly)
229                 {
230                         BindingFlags bf;
231                         if (publicOnly == true)
232                                 bf = BindingFlags.Public;
233                         else
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);
238                 }
239         }
240 }