New tests.
[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 namespace System.Reflection.Emit
41 {
42         [ComVisible (true)]
43         public sealed class GenericTypeParameterBuilder : Type
44         {
45         #region Sync with reflection.h
46                 private TypeBuilder tbuilder;
47                 private MethodBuilder mbuilder;
48                 private string name;
49                 private int index;
50                 private Type base_type;
51                 private Type[] iface_constraints;
52                 private CustomAttributeBuilder[] cattrs;
53                 private GenericParameterAttributes attrs;
54         #endregion
55
56                 public void SetBaseTypeConstraint (Type baseTypeConstraint)
57                 {
58                         this.base_type = baseTypeConstraint ?? typeof (object);
59                 }
60
61                 [ComVisible (true)]
62                 public void SetInterfaceConstraints (params Type[] interfaceConstraints)
63                 {
64                         this.iface_constraints = interfaceConstraints;
65                 }
66
67                 public void SetGenericParameterAttributes (GenericParameterAttributes genericParameterAttributes)
68                 {
69                         this.attrs = genericParameterAttributes;
70                 }
71
72                 internal GenericTypeParameterBuilder (TypeBuilder tbuilder,
73                                                       MethodBuilder mbuilder,
74                                                       string name, int index)
75                 {
76                         this.tbuilder = tbuilder;
77                         this.mbuilder = mbuilder;
78                         this.name = name;
79                         this.index = index;
80
81                         initialize ();
82                 }
83
84
85                 internal override bool IsCompilerContext {
86                         get {
87                                 return tbuilder.IsCompilerContext;
88                         }
89                 }
90
91                 internal override Type InternalResolve ()
92                 {
93                         return tbuilder.InternalResolve ().GetGenericArguments () [index]; 
94                 }
95
96                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97                 private extern void initialize ();
98
99                 [ComVisible (true)]
100                 public override bool IsSubclassOf (Type c)
101                 {
102                         if (!IsCompilerContext)
103                                 throw not_supported ();
104                         if (BaseType == null)
105                                 return false;
106                         else
107                                 return BaseType == c || BaseType.IsSubclassOf (c);
108                 }
109
110                 protected override TypeAttributes GetAttributeFlagsImpl ()
111                 {
112 #if NET_4_0
113                         return TypeAttributes.Public;
114 #else
115                         if (IsCompilerContext)
116                                 return TypeAttributes.Public;
117                         throw not_supported ();
118 #endif
119                 }
120
121                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
122                                                                        Binder binder,
123                                                                        CallingConventions callConvention,
124                                                                        Type[] types,
125                                                                        ParameterModifier[] modifiers)
126                 {
127                         throw not_supported ();
128                 }
129
130                 [ComVisible (true)]
131                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
132                 {
133                         throw not_supported ();
134                 }
135
136                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
137                 {
138                         throw not_supported ();
139                 }
140
141                 public override EventInfo[] GetEvents ()
142                 {
143                         throw not_supported ();
144                 }
145
146                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
147                 {
148                         throw not_supported ();
149                 }
150
151                 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
152                 {
153                         throw not_supported ();
154                 }
155
156                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
157                 {
158                         throw not_supported ();
159                 }
160                 
161                 public override Type GetInterface (string name, bool ignoreCase)
162                 {
163                         throw not_supported ();
164                 }
165
166                 public override Type[] GetInterfaces ()
167                 {
168                         throw not_supported ();
169                 }
170                 
171                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
172                 {
173                         throw not_supported ();
174                 }
175
176                 public override MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
177                 {
178                         throw not_supported ();
179                 }
180
181                 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
182                 {
183                         throw not_supported ();
184                 }
185
186                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
187                                                              Binder binder,
188                                                              CallingConventions callConvention,
189                                                              Type[] types, ParameterModifier[] modifiers)
190                 {
191                         throw not_supported ();
192                 }
193
194                 public override Type GetNestedType (string name, BindingFlags bindingAttr)
195                 {
196                         throw not_supported ();
197                 }
198
199                 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
200                 {
201                         throw not_supported ();
202                 }
203
204                 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
205                 {
206                         throw not_supported ();
207                 }
208
209                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
210                                                                  Binder binder, Type returnType,
211                                                                  Type[] types,
212                                                                  ParameterModifier[] modifiers)
213                 {
214                         throw not_supported ();
215                 }
216
217                 protected override bool HasElementTypeImpl ()
218                 {
219                         return false;
220                 }
221
222                 public override bool IsAssignableFrom (Type c)
223                 {
224                         throw not_supported ();
225                 }
226
227                 public override bool IsInstanceOfType (object o)
228                 {
229                         throw not_supported ();
230                 }
231
232                 protected override bool IsArrayImpl ()
233                 {
234                         return false;
235                 }
236
237                 protected override bool IsByRefImpl ()
238                 {
239                         return false;
240                 }
241
242                 protected override bool IsCOMObjectImpl ()
243                 {
244                         return false;
245                 }
246
247                 protected override bool IsPointerImpl ()
248                 {
249                         return false;
250                 }
251
252                 protected override bool IsPrimitiveImpl ()
253                 {
254                         return false;
255                 }
256
257                 protected override bool IsValueTypeImpl ()
258                 {
259                         return base_type != null ? base_type.IsValueType : false;
260                 }
261                 
262                 public override object InvokeMember (string name, BindingFlags invokeAttr,
263                                                      Binder binder, object target, object[] args,
264                                                      ParameterModifier[] modifiers,
265                                                      CultureInfo culture, string[] namedParameters)
266                 {
267                         throw not_supported ();
268                 }
269
270                 public override Type GetElementType ()
271                 {
272                         throw not_supported ();
273                 }
274
275                 public override Type UnderlyingSystemType {
276                         get {
277                                 return this;
278                         }
279                 }
280
281                 public override Assembly Assembly {
282                         get { return tbuilder.Assembly; }
283                 }
284
285                 public override string AssemblyQualifiedName {
286                         get { return null; }
287                 }
288
289                 public override Type BaseType {
290                         get { return base_type; }
291                 }
292
293                 public override string FullName {
294                         get { return null; }
295                 }
296
297                 public override Guid GUID {
298                         get { throw not_supported (); }
299                 }
300
301                 public override bool IsDefined (Type attributeType, bool inherit)
302                 {
303                         throw not_supported ();
304                 }
305
306                 public override object[] GetCustomAttributes (bool inherit)
307                 {
308                         throw not_supported ();
309                 }
310
311                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
312                 {
313                         throw not_supported ();
314                 }
315
316                 [ComVisible (true)]
317                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
318                 {
319                         throw not_supported ();
320                 }
321
322                 public override string Name {
323                         get { return name; }
324                 }
325
326                 public override string Namespace {
327                         get { return null; }
328                 }
329
330                 public override Module Module {
331                         get { return tbuilder.Module; }
332                 }
333
334                 public override Type DeclaringType {
335                         get { return mbuilder != null ? mbuilder.DeclaringType : tbuilder; }
336                 }
337
338                 public override Type ReflectedType {
339                         get { return DeclaringType; }
340                 }
341
342                 public override RuntimeTypeHandle TypeHandle {
343                         get { throw not_supported (); }
344                 }
345
346                 public override Type[] GetGenericArguments ()
347                 {
348                         throw new InvalidOperationException ();
349                 }
350
351                 public override Type GetGenericTypeDefinition ()
352                 {
353                         throw new InvalidOperationException ();
354                 }
355
356                 public override bool ContainsGenericParameters {
357                         get { return true; }
358                 }
359
360                 public override bool IsGenericParameter {
361                         get { return true; }
362                 }
363
364                 public override bool IsGenericType {
365                         get { return false; }
366                 }
367
368                 public override bool IsGenericTypeDefinition {
369                         get { return false; }
370                 }
371
372                 public override GenericParameterAttributes GenericParameterAttributes {
373                         get {
374                                 if (IsCompilerContext)
375                                         return attrs;
376                                 throw new NotSupportedException ();
377                         }
378                 }
379
380                 public override int GenericParameterPosition {
381                         get { return index; }
382                 }
383
384                 public override Type[] GetGenericParameterConstraints ()
385                 {
386                         if (!IsCompilerContext)
387                                 throw new InvalidOperationException ();
388                         if (base_type == null) {
389                                 if (iface_constraints != null)
390                                         return iface_constraints;
391
392                                 return Type.EmptyTypes;
393                         }
394
395                         if (iface_constraints == null)
396                                 return new Type[] { base_type };
397
398                         Type[] ret = new Type [iface_constraints.Length + 1];
399                         ret [0] = base_type;
400                         iface_constraints.CopyTo (ret, 1);
401                         return ret;
402                 }
403
404                 public override MethodBase DeclaringMethod {
405                         get { return mbuilder; }
406                 }
407
408                 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
409                 {
410                         if (customBuilder == null)
411                                 throw new ArgumentNullException ("customBuilder");
412
413                         if (cattrs != null) {
414                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
415                                 cattrs.CopyTo (new_array, 0);
416                                 new_array [cattrs.Length] = customBuilder;
417                                 cattrs = new_array;
418                         } else {
419                                 cattrs = new CustomAttributeBuilder [1];
420                                 cattrs [0] = customBuilder;
421                         }
422                 }
423
424                 [MonoTODO ("unverified implementation")]
425                 public void SetCustomAttribute (ConstructorInfo con, byte [] binaryAttribute)
426                 {
427                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
428                 }
429
430                 private Exception not_supported ()
431                 {
432                         return new NotSupportedException ();
433                 }
434
435                 public override string ToString ()
436                 {
437                         return name;
438                 }
439
440                 [MonoTODO]
441                 public override bool Equals (object o)
442                 {
443                         return base.Equals (o);
444                 }
445
446                 [MonoTODO]
447                 public override int GetHashCode ()
448                 {
449                         return base.GetHashCode ();
450                 }
451
452                 public override Type MakeArrayType ()
453                 {
454                         return  new ArrayType (this, 0);
455                 }
456
457                 public override Type MakeArrayType (int rank)
458                 {
459                         if (rank < 1)
460                                 throw new IndexOutOfRangeException ();
461                         return new ArrayType (this, rank);
462                 }
463
464                 public override Type MakeByRefType ()
465                 {
466                         return new ByRefType (this);
467                 }
468
469                 [MonoTODO]
470                 public override Type MakeGenericType (params Type [] typeArguments)
471                 {
472                         return base.MakeGenericType (typeArguments);
473                 }
474
475                 public override Type MakePointerType ()
476                 {
477                         return new PointerType (this);
478                 }
479         }
480 }