merge -r 61110:61111
[mono.git] / mcs / class / corlib / System.Reflection.Emit / GenericTypeParameterBuilder.cs
1 //
2 // System.Reflection.Emit.GenericTypeParameterBuilder
3 //
4 // Martin Baulig (martin@ximian.com)
5 //
6 // (C) 2004 Novell, Inc.
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Collections;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Globalization;
38 using System.Runtime.Serialization;
39
40 #if NET_2_0 || BOOTSTRAP_NET_2_0
41 namespace System.Reflection.Emit
42 {
43         [ComVisible (true)]
44         public sealed class GenericTypeParameterBuilder : Type
45         {
46         #region Sync with reflection.h
47                 private TypeBuilder tbuilder;
48                 private MethodBuilder mbuilder;
49                 private string name;
50                 private int index;
51                 private Type base_type;
52                 private Type[] iface_constraints;
53                 private CustomAttributeBuilder[] cattrs;
54                 private GenericParameterAttributes attrs;
55         #endregion
56
57                 public void SetBaseTypeConstraint (Type base_type_constraint)
58                 {
59                         this.base_type = base_type_constraint;
60                 }
61
62                 [ComVisible (true)]
63                 public void SetInterfaceConstraints (Type[] iface_constraints)
64                 {
65                         this.iface_constraints = iface_constraints;
66                 }
67
68                 public void SetGenericParameterAttributes (GenericParameterAttributes attrs)
69                 {
70                         this.attrs = attrs;
71                 }
72
73                 internal GenericTypeParameterBuilder (TypeBuilder tbuilder,
74                                                       MethodBuilder mbuilder,
75                                                       string name, int index)
76                 {
77                         this.tbuilder = tbuilder;
78                         this.mbuilder = mbuilder;
79                         this.name = name;
80                         this.index = index;
81
82                         initialize ();
83                 }
84
85                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86                 private extern void initialize ();
87
88                 public override bool IsSubclassOf (Type c)
89                 {
90                         if (BaseType == null)
91                                 return false;
92                         else
93                                 return BaseType == c || BaseType.IsSubclassOf (c);
94                 }
95
96                 protected override TypeAttributes GetAttributeFlagsImpl ()
97                 {
98                         return TypeAttributes.Public;
99                 }
100
101                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
102                                                                        Binder binder,
103                                                                        CallingConventions callConvention,
104                                                                        Type[] types,
105                                                                        ParameterModifier[] modifiers)
106                 {
107                         throw not_supported ();
108                 }
109
110                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
111                 {
112                         throw not_supported ();
113                 }
114
115                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
116                 {
117                         throw not_supported ();
118                 }
119
120                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
121                 {
122                         throw not_supported ();
123                 }
124
125                 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
126                 {
127                         throw not_supported ();
128                 }
129
130                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
131                 {
132                         throw not_supported ();
133                 }
134                 
135                 public override Type GetInterface (string name, bool ignoreCase)
136                 {
137                         throw not_supported ();
138                 }
139
140                 public override Type[] GetInterfaces ()
141                 {
142                         throw not_supported ();
143                 }
144                 
145                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
146                 {
147                         throw not_supported ();
148                 }
149
150                 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
151                 {
152                         throw not_supported ();
153                 }
154
155                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
156                                                              Binder binder,
157                                                              CallingConventions callConvention,
158                                                              Type[] types, ParameterModifier[] modifiers)
159                 {
160                         throw not_supported ();
161                 }
162
163                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
164                 {
165                         throw not_supported ();
166                 }
167
168                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
169                 {
170                         throw not_supported ();
171                 }
172
173                 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
174                 {
175                         throw not_supported ();
176                 }
177
178                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
179                                                                  Binder binder, Type returnType,
180                                                                  Type[] types,
181                                                                  ParameterModifier[] modifiers)
182                 {
183                         throw not_supported ();
184                 }
185
186                 protected override bool HasElementTypeImpl ()
187                 {
188                         return false;
189                 }
190
191                 protected override bool IsArrayImpl ()
192                 {
193                         return false;
194                 }
195
196                 protected override bool IsByRefImpl ()
197                 {
198                         return false;
199                 }
200
201                 protected override bool IsCOMObjectImpl ()
202                 {
203                         return false;
204                 }
205
206                 protected override bool IsPointerImpl ()
207                 {
208                         return false;
209                 }
210
211                 protected override bool IsPrimitiveImpl ()
212                 {
213                         return false;
214                 }
215
216                 protected override bool IsValueTypeImpl ()
217                 {
218                         return base_type != null ? base_type.IsValueType : false;
219                 }
220                 
221                 public override object InvokeMember (string name, BindingFlags invokeAttr,
222                                                      Binder binder, object target, object[] args,
223                                                      ParameterModifier[] modifiers,
224                                                      CultureInfo culture, string[] namedParameters)
225                 {
226                         throw not_supported ();
227                 }
228
229                 public override Type GetElementType ()
230                 {
231                         throw not_supported ();
232                 }
233
234                 public override Type UnderlyingSystemType {
235                         get {
236                                 return this;
237                         }
238                 }
239
240                 public override Assembly Assembly {
241                         get { return tbuilder.Assembly; }
242                 }
243
244                 public override string AssemblyQualifiedName {
245                         get { return null; }
246                 }
247
248                 public override Type BaseType {
249                         get { return base_type; }
250                 }
251
252                 public override string FullName {
253                         get { return null; }
254                 }
255
256                 public override Guid GUID {
257                         get { return Guid.Empty; }
258                 }
259
260                 public override bool IsDefined (Type attributeType, bool inherit)
261                 {
262                         throw not_supported ();
263                 }
264
265                 public override object[] GetCustomAttributes (bool inherit)
266                 {
267                         throw not_supported ();
268                 }
269
270                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
271                 {
272                         throw not_supported ();
273                 }
274
275                 public override MemberTypes MemberType {
276                         get { return MemberTypes.TypeInfo; }
277                 }
278
279                 public override string Name {
280                         get { return name; }
281                 }
282
283                 public override string Namespace {
284                         get { return null; }
285                 }
286
287                 public override Module Module {
288                         get { return tbuilder.Module; }
289                 }
290
291                 public override Type DeclaringType {
292                         get { return mbuilder != null ? mbuilder.DeclaringType : tbuilder; }
293                 }
294
295                 public override Type ReflectedType {
296                         get { return DeclaringType; }
297                 }
298
299                 public override RuntimeTypeHandle TypeHandle {
300                         get { throw not_supported (); }
301                 }
302
303                 public override int GetArrayRank ()
304                 {
305                         throw not_supported ();
306                 }
307
308                 public override Type[] GetGenericArguments ()
309                 {
310                         throw not_supported ();
311                 }
312
313                 public override Type GetGenericTypeDefinition ()
314                 {
315                         throw not_supported ();
316                 }
317
318                 public override bool ContainsGenericParameters {
319                         get { return true; }
320                 }
321
322                 public override bool IsGenericParameter {
323                         get { return true; }
324                 }
325
326                 public override int GenericParameterPosition {
327                         get { return index; }
328                 }
329
330                 public override GenericParameterAttributes GenericParameterAttributes {
331                         get {
332                                 return attrs;
333                         }
334                 }
335
336                 public override Type[] GetGenericParameterConstraints ()
337                 {
338                         if (base_type == null) {
339                                 if (iface_constraints != null)
340                                         return iface_constraints;
341
342                                 return Type.EmptyTypes;
343                         }
344
345                         if (iface_constraints == null)
346                                 return new Type[] { base_type };
347
348                         Type[] ret = new Type [iface_constraints.Length + 1];
349                         ret [0] = base_type;
350                         iface_constraints.CopyTo (ret, 1);
351                         return ret;
352                 }
353
354                 public override MethodBase DeclaringMethod {
355                         get { return mbuilder; }
356                 }
357
358                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
359                 {
360                         if (customBuilder == null)
361                                 throw new ArgumentNullException ("customBuilder");
362
363                         if (cattrs != null) {
364                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
365                                 cattrs.CopyTo (new_array, 0);
366                                 new_array [cattrs.Length] = customBuilder;
367                                 cattrs = new_array;
368                         } else {
369                                 cattrs = new CustomAttributeBuilder [1];
370                                 cattrs [0] = customBuilder;
371                         }
372                 }
373
374                 private Exception not_supported ()
375                 {
376                         return new NotSupportedException ();
377                 }
378
379                 public override string ToString ()
380                 {
381                         return name;
382                 }
383         }
384 }
385 #endif