(SetCustomAttribute): Handle the presence of the
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
1 //
2 // System.Reflection.Emit/TypeBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Text;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
16 using System.Globalization;
17 using System.Collections;
18 using System.Security;
19 using System.Security.Permissions;
20
21 namespace System.Reflection.Emit {
22
23         public sealed class TypeBuilder : Type {
24         #region Sync with reflection.h
25         private string tname;
26         private string nspace;
27         private Type parent;
28         private Type nesting_type;
29         private Type[] interfaces;
30         private int num_methods;
31         private MethodBuilder[] methods;
32         private ConstructorBuilder[] ctors;
33         private PropertyBuilder[] properties;
34         private int num_fields;
35         private FieldBuilder[] fields;
36         private EventBuilder[] events;
37         private CustomAttributeBuilder[] cattrs;
38         internal TypeBuilder[] subtypes;
39         private TypeAttributes attrs;
40         private int table_idx;
41         private ModuleBuilder pmodule;
42         private int class_size;
43         private PackingSize packing_size;
44 #if NET_2_0 || BOOTSTRAP_NET_2_0
45         private GenericTypeParameterBuilder[] generic_params;
46 #else
47         private Object generic_params; /* so offsets don't change */
48 #endif
49         private RefEmitPermissionSet[] permissions;     
50         #endregion
51         private Type created;
52         string fullname;
53
54         public const int UnspecifiedTypeSize = 0;
55
56                 protected override TypeAttributes GetAttributeFlagsImpl () {
57                         return attrs;
58                 }
59                 
60                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
61                 private extern void setup_internal_class (TypeBuilder tb);
62                 
63                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
64                 private extern void create_internal_class (TypeBuilder tb);
65                 
66                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
67                 private extern void setup_generic_class (TypeBuilder tb);
68
69                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
70                 private extern EventInfo get_event_info (EventBuilder eb);
71
72                 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
73                         this.parent = null;
74                         this.attrs = attr;
75                         this.class_size = -1;
76                         fullname = this.tname = "<Module>";
77                         this.nspace = "";
78                         pmodule = mb;
79                         setup_internal_class (this);
80                 }
81
82                 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
83                         int sep_index;
84                         this.parent = parent;
85                         this.attrs = attr;
86                         this.class_size = type_size;
87                         this.packing_size = packing_size;
88                         this.nesting_type = nesting_type;
89                         sep_index = name.LastIndexOf('.');
90                         if (sep_index != -1) {
91                                 this.tname = name.Substring (sep_index + 1);
92                                 this.nspace = name.Substring (0, sep_index);
93                         } else {
94                                 this.tname = name;
95                                 this.nspace = "";
96                         }
97                         if (interfaces != null) {
98                                 this.interfaces = new Type[interfaces.Length];
99                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
100                         }
101                         pmodule = mb;
102                         // skip .<Module> ?
103                         table_idx = mb.get_next_table_index (this, 0x02, true);
104                         setup_internal_class (this);
105                         fullname = GetFullName ();
106                 }
107
108                 public override Assembly Assembly {
109                         get {return pmodule.Assembly;}
110                 }
111
112                 public override string AssemblyQualifiedName {
113                         get {
114                                 return fullname + ", " + Assembly.GetName().FullName;
115                         }
116                 }
117                 public override Type BaseType {
118                         get {
119                                 return parent;
120                         }
121                 }
122                 public override Type DeclaringType {get {return nesting_type;}}
123
124 /*              public override bool IsSubclassOf (Type c)
125                 {
126                         Type t;
127                         if (c == null)
128                                 return false;
129                         if (c == this)
130                                 return false;
131                         t = parent;
132                         while (t != null) {
133                                 if (c == t)
134                                         return true;
135                                 t = t.BaseType;
136                         }
137                         return false;
138                 }*/
139
140                 [MonoTODO]
141                 public override Type UnderlyingSystemType {
142                         get {
143                                 // This should return the type itself for non-enum types but 
144                                 // that breaks mcs.
145                                 if (fields != null) {
146                                         foreach (FieldBuilder f in fields) {
147                                                 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
148                                                         return f.FieldType;
149                                         }
150                                 }
151                                 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
152                         }
153                 }
154
155                 string GetFullName () {
156                         if (nesting_type != null)
157                                 return String.Concat (nesting_type.FullName, "+", tname);
158                         if ((nspace != null) && (nspace.Length > 0))
159                                 return String.Concat (nspace, ".", tname);
160                         return tname;
161                 }
162         
163                 public override string FullName {
164                         get {
165                                 return fullname;
166                         }
167                 }
168         
169                 public override Guid GUID {
170                         get {
171                             throw not_supported ();
172                         }
173                 }
174
175                 public override Module Module {
176                         get {return pmodule;}
177                 }
178                 public override string Name {
179                         get {return tname;}
180                 }
181                 public override string Namespace {
182                         get {return nspace;}
183                 }
184                 public PackingSize PackingSize {
185                         get {return packing_size;}
186                 }
187                 public int Size {
188                         get { return class_size; }
189                 }
190                 public override Type ReflectedType {get {return nesting_type;}}
191
192                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
193                         if (pset == null)
194                                 throw new ArgumentNullException ("pset");
195                         if ((action == SecurityAction.RequestMinimum) ||
196                                 (action == SecurityAction.RequestOptional) ||
197                                 (action == SecurityAction.RequestRefuse))
198                                 throw new ArgumentException ("Request* values are not permitted", "action");
199
200                         if (is_created)
201                                 throw not_after_created ();
202
203                         if (permissions != null) {
204                                 /* Check duplicate actions */
205                                 foreach (RefEmitPermissionSet set in permissions)
206                                         if (set.action == action)
207                                                 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
208
209                                 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
210                                 permissions.CopyTo (new_array, 0);
211                                 permissions = new_array;
212                         }
213                         else
214                                 permissions = new RefEmitPermissionSet [1];
215
216                         permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
217                         attrs |= TypeAttributes.HasSecurity;
218                 }
219
220                 public void AddInterfaceImplementation( Type interfaceType) {
221                         if (interfaceType == null)
222                                 throw new ArgumentNullException ("interfaceType");
223                         if (is_created)
224                                 throw not_after_created ();
225
226                         if (interfaces != null) {
227                                 // Check for duplicates
228                                 foreach (Type t in interfaces)
229                                         if (t == interfaceType)
230                                                 return;
231
232                                 Type[] ifnew = new Type [interfaces.Length + 1];
233                                 interfaces.CopyTo (ifnew, 0);
234                                 ifnew [interfaces.Length] = interfaceType;
235                                 interfaces = ifnew;
236                         } else {
237                                 interfaces = new Type [1];
238                                 interfaces [0] = interfaceType;
239                         }
240                 }
241
242                 [MonoTODO]
243                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
244                                                                        CallingConventions callConvention, Type[] types,
245                                                                        ParameterModifier[] modifiers)
246                 {
247                         if (ctors == null)
248                                 return null;
249
250                         ConstructorBuilder found = null;
251                         int count = 0;
252                         
253                         foreach (ConstructorBuilder cb in ctors){
254                                 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
255                                         continue;
256                                 found = cb;
257                                 count++;
258                         }
259
260                         if (count == 0)
261                                 return null;
262                         if (types == null){
263                                 if (count > 1)
264                                         throw new AmbiguousMatchException ();
265                                 return found;
266                         }
267                         MethodBase[] match = new MethodBase [count];
268                         if (count == 1)
269                                 match [0] = found;
270                         else {
271                                 count = 0;
272                                 foreach (ConstructorInfo m in ctors) {
273                                         if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
274                                                 continue;
275                                         match [count++] = m;
276                                 }
277                         }
278                         if (binder == null)
279                                 binder = Binder.DefaultBinder;
280                         return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
281                 }
282
283                 public override bool IsDefined( Type attributeType, bool inherit)
284                 {
285                         /*
286                          * MS throws NotSupported here, but we can't because some corlib
287                          * classes make calls to IsDefined.
288                          */
289                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
290                 }
291                 
292                 public override object[] GetCustomAttributes(bool inherit)
293                 {
294                         throw not_supported ();
295                 }
296                 
297                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
298                 {
299                         throw not_supported ();
300                 }
301
302                 public TypeBuilder DefineNestedType (string name) {
303                         return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
304                 }
305
306                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
307                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
308                 }
309
310                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
311                         return DefineNestedType (name, attr, parent, null);
312                 }
313
314                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
315                                                       PackingSize packsize, int typesize)
316                 {
317                         check_name ("name", name);
318                         // Visibility must be NestedXXX
319                         /* This breaks mcs
320                         if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
321                                 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
322                                 throw new ArgumentException ("attr", "Bad type flags for nested type.");
323                         */
324                         if (interfaces != null)
325                                 foreach (Type iface in interfaces)
326                                         if (iface == null)
327                                                 throw new ArgumentNullException ("interfaces");
328
329                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
330                         res.fullname = res.GetFullName ();
331                         pmodule.RegisterTypeName (res, res.fullname);
332                         if (subtypes != null) {
333                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
334                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
335                                 new_types [subtypes.Length] = res;
336                                 subtypes = new_types;
337                         } else {
338                                 subtypes = new TypeBuilder [1];
339                                 subtypes [0] = res;
340                         }
341                         return res;
342                 }
343
344                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
345                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
346                 }
347
348                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
349                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
350                 }
351
352                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
353                         return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
354                 }
355
356                 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
357                         return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
358                 }
359
360 #if NET_2_0 || BOOTSTRAP_NET_2_0
361                 public
362 #else
363                 internal
364 #endif
365                 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
366                 {
367                         if (is_created)
368                                 throw not_after_created ();
369                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
370                         if (ctors != null) {
371                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
372                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
373                                 new_ctors [ctors.Length] = cb;
374                                 ctors = new_ctors;
375                         } else {
376                                 ctors = new ConstructorBuilder [1];
377                                 ctors [0] = cb;
378                         }
379                         return cb;
380                 }
381
382                 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
383                 {
384                         ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
385
386                         Type parent_type;
387
388                         if (parent != null)
389                                 parent_type = parent;
390                         else
391                                 parent_type = pmodule.assemblyb.corlib_object_type;
392
393                         ConstructorInfo parent_constructor =
394                                 parent_type.GetConstructor (
395                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
396                                         null, Type.EmptyTypes, null);
397
398                         ILGenerator ig = cb.GetILGenerator ();
399                         if (parent_constructor != null){
400                                 ig.Emit (OpCodes.Ldarg_0);
401                                 ig.Emit (OpCodes.Call, parent_constructor);
402                         }
403                         ig.Emit (OpCodes.Ret);
404                         return cb;
405                 }
406
407                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
408                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
409                 }
410
411                 private void append_method (MethodBuilder mb) {
412                         if (methods != null) {
413                                 if (methods.Length == num_methods) {
414                                         MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
415                                         System.Array.Copy (methods, new_methods, num_methods);
416                                         methods = new_methods;
417                                 }
418                         } else {
419                                 methods = new MethodBuilder [1];
420                         }
421                         methods [num_methods] = mb;
422                         num_methods ++;
423                 }
424
425                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
426                         return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
427                 }
428
429 #if NET_2_0 || BOOTSTRAP_NET_2_0
430                 public
431 #else
432                 internal
433 #endif
434                 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
435                         check_name ("name", name);
436                         if (is_created)
437                                 throw not_after_created ();
438                         if (IsInterface && (
439                                 !((attributes & MethodAttributes.Abstract) != 0) || 
440                                 !((attributes & MethodAttributes.Virtual) != 0)))
441                                 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
442
443                         if (returnType == null)
444                                 returnType = pmodule.assemblyb.corlib_void_type;
445                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
446                         append_method (res);
447                         return res;
448                 }
449
450                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
451                         return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
452                 }
453
454 #if NET_2_0 || BOOTSTRAP_NET_2_0
455                 public
456 #else
457                 internal
458 #endif
459                 MethodBuilder DefinePInvokeMethod (
460                                                 string name, 
461                                                 string dllName, 
462                                                 string entryName, MethodAttributes attributes, 
463                                                 CallingConventions callingConvention, 
464                                                 Type returnType, 
465                                                 Type[] returnTypeRequiredCustomModifiers, 
466                                                 Type[] returnTypeOptionalCustomModifiers, 
467                                                 Type[] parameterTypes, 
468                                                 Type[][] parameterTypeRequiredCustomModifiers, 
469                                                 Type[][] parameterTypeOptionalCustomModifiers, 
470                                                 CallingConvention nativeCallConv, 
471                                                 CharSet nativeCharSet) {
472                         check_name ("name", name);
473                         check_name ("dllName", dllName);
474                         check_name ("entryName", entryName);
475                         if ((attributes & MethodAttributes.Abstract) != 0)
476                                 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
477                         if (IsInterface)
478                                 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");            
479                         if (is_created)
480                                 throw not_after_created ();
481
482                         MethodBuilder res 
483                                 = new MethodBuilder (
484                                                 this, 
485                                                 name, 
486                                                 attributes, 
487                                                 callingConvention,
488                                                 returnType, 
489                                                 returnTypeRequiredCustomModifiers, 
490                                                 returnTypeOptionalCustomModifiers, 
491                                                 parameterTypes, 
492                                                 parameterTypeRequiredCustomModifiers, 
493                                                 parameterTypeOptionalCustomModifiers,
494                                                 dllName, 
495                                                 entryName, 
496                                                 nativeCallConv, 
497                                                 nativeCharSet);
498                         append_method (res);
499                         return res;
500                 }
501
502                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
503                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
504                                 nativeCallConv, nativeCharSet);
505                 }
506
507                 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
508                         if (methodInfoBody == null)
509                                 throw new ArgumentNullException ("methodInfoBody");
510                         if (methodInfoDeclaration == null)
511                                 throw new ArgumentNullException ("methodInfoDeclaration");
512                         if (is_created)
513                                 throw not_after_created ();
514
515                         if (methodInfoBody is MethodBuilder) {
516                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
517                                 mb.set_override (methodInfoDeclaration);
518                         }
519                 }
520
521                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
522                         return DefineField (fieldName, type, null, null, attributes);
523                 }
524
525 #if NET_2_0 || BOOTSTRAP_NET_2_0
526                 public
527 #else
528                 internal
529 #endif
530             FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
531                         check_name ("fieldName", fieldName);
532                         if (type == typeof (void))
533                                 throw new ArgumentException ("type",  "Bad field type in defining field.");
534                         if (is_created)
535                                 throw not_after_created ();
536
537                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
538                         if (fields != null) {
539                                 if (fields.Length == num_fields) {
540                                         FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
541                                         System.Array.Copy (fields, new_fields, num_fields);
542                                         fields = new_fields;
543                                 }
544                                 fields [num_fields] = res;
545                                 num_fields ++;
546                         } else {
547                                 fields = new FieldBuilder [1];
548                                 fields [0] = res;
549                                 num_fields ++;
550                                 create_internal_class (this);
551                         }
552                         return res;
553                 }
554
555                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
556                         check_name ("name", name);
557                         if (parameterTypes != null)
558                                 foreach (Type param in parameterTypes)
559                                         if (param == null)
560                                                 throw new ArgumentNullException ("parameterTypes");
561                         if (is_created)
562                                 throw not_after_created ();
563
564                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
565
566                         if (properties != null) {
567                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
568                                 System.Array.Copy (properties, new_properties, properties.Length);
569                                 new_properties [properties.Length] = res;
570                                 properties = new_properties;
571                         } else {
572                                 properties = new PropertyBuilder [1];
573                                 properties [0] = res;
574                         }
575                         return res;
576                 }
577
578                 public ConstructorBuilder DefineTypeInitializer() {
579                         return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
580                 }
581
582                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
583                 private extern Type create_runtime_class (TypeBuilder tb);
584
585                 private bool is_nested_in (Type t) {
586                         while (t != null) {
587                                 if (t == this)
588                                         return true;
589                                 else
590                                         t = t.DeclaringType;
591                         }
592                         return false;
593                 }
594                 
595                 public Type CreateType() {
596                         /* handle nesting_type */
597                         if (is_created)
598                                 throw not_after_created ();
599
600                         // Fire TypeResolve events for fields whose type is an unfinished
601                         // value type.
602                         if (fields != null) {
603                                 foreach (FieldBuilder fb in fields) {
604                                         if (fb == null)
605                                                 continue;
606                                         Type ft = fb.FieldType;
607                                         if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
608                                                 TypeBuilder tb = (TypeBuilder)ft;
609                                                 if (!tb.is_created) {
610                                                         AppDomain.CurrentDomain.DoTypeResolve (tb);
611                                                         if (!tb.is_created) {
612                                                                 // FIXME: We should throw an exception here,
613                                                                 // but mcs expects that the type is created
614                                                                 // even if the exception is thrown
615                                                                 //throw new TypeLoadException ("Could not load type " + tb);
616                                                         }
617                                                 }
618                                         }
619                                 }
620                         }
621
622                         if (methods != null) {
623                                 for (int i = 0; i < num_methods; ++i)
624                                         ((MethodBuilder)(methods[i])).fixup ();
625                         }
626
627                         //
628                         // On classes, define a default constructor if not provided
629                         //
630                         if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
631                                 DefineDefaultConstructor (MethodAttributes.Public);
632
633                         if (ctors != null){
634                                 foreach (ConstructorBuilder ctor in ctors) 
635                                         ctor.fixup ();
636                         }
637
638                         created = create_runtime_class (this);
639                         if (created != null)
640                                 return created;
641                         return this;
642                 }
643
644                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
645                 {
646                         if (ctors == null)
647                                 return new ConstructorInfo [0];
648                         ArrayList l = new ArrayList ();
649                         bool match;
650                         MethodAttributes mattrs;
651                         
652                         foreach (ConstructorBuilder c in ctors) {
653                                 match = false;
654                                 mattrs = c.Attributes;
655                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
656                                         if ((bindingAttr & BindingFlags.Public) != 0)
657                                                 match = true;
658                                 } else {
659                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
660                                                 match = true;
661                                 }
662                                 if (!match)
663                                         continue;
664                                 match = false;
665                                 if ((mattrs & MethodAttributes.Static) != 0) {
666                                         if ((bindingAttr & BindingFlags.Static) != 0)
667                                                 match = true;
668                                 } else {
669                                         if ((bindingAttr & BindingFlags.Instance) != 0)
670                                                 match = true;
671                                 }
672                                 if (!match)
673                                         continue;
674                                 l.Add (c);
675                         }
676                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
677                         l.CopyTo (result);
678                         return result;
679                 }
680
681                 public override Type GetElementType () { 
682                         throw not_supported ();
683                 }
684
685                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
686                         throw not_supported ();
687                 }
688
689                 /* Needed to keep signature compatibility with MS.NET */
690                 public override EventInfo[] GetEvents ()
691                 {
692                         return GetEvents (DefaultBindingFlags);
693                 }
694
695                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
696                         // FIXME: Under MS.NET, this throws a NotImplementedException
697                         // But mcs calls this method. How can that be?
698                         return new EventInfo [0];
699                 }
700
701                 // This is only used from MonoGenericInst.initialize().
702                 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
703                 {
704                         if (events == null)
705                                 return new EventInfo [0];
706                         ArrayList l = new ArrayList ();
707                         bool match;
708                         MethodAttributes mattrs;
709                         MethodInfo accessor;
710
711                         foreach (EventBuilder eb in events) {
712                                 if (eb == null)
713                                         continue;
714                                 EventInfo c = get_event_info (eb);
715                                 match = false;
716                                 accessor = c.GetAddMethod (true);
717                                 if (accessor == null)
718                                         accessor = c.GetRemoveMethod (true);
719                                 if (accessor == null)
720                                         continue;
721                                 mattrs = accessor.Attributes;
722                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
723                                         if ((bindingAttr & BindingFlags.Public) != 0)
724                                                 match = true;
725                                 } else {
726                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
727                                                 match = true;
728                                 }
729                                 if (!match)
730                                         continue;
731                                 match = false;
732                                 if ((mattrs & MethodAttributes.Static) != 0) {
733                                         if ((bindingAttr & BindingFlags.Static) != 0)
734                                                 match = true;
735                                 } else {
736                                         if ((bindingAttr & BindingFlags.Instance) != 0)
737                                                 match = true;
738                                 }
739                                 if (!match)
740                                         continue;
741                                 l.Add (c);
742                         }
743                         EventInfo[] result = new EventInfo [l.Count];
744                         l.CopyTo (result);
745                         return result;
746                 }
747
748                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
749                         if (fields == null)
750                                 return null;
751
752                         bool match;
753                         FieldAttributes mattrs;
754                         
755                         foreach (FieldInfo c in fields) {
756                                 if (c == null)
757                                         continue;
758                                 if (c.Name != name)
759                                         continue;
760                                 match = false;
761                                 mattrs = c.Attributes;
762                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
763                                         if ((bindingAttr & BindingFlags.Public) != 0)
764                                                 match = true;
765                                 } else {
766                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
767                                                 match = true;
768                                 }
769                                 if (!match)
770                                         continue;
771                                 match = false;
772                                 if ((mattrs & FieldAttributes.Static) != 0) {
773                                         if ((bindingAttr & BindingFlags.Static) != 0)
774                                                 match = true;
775                                 } else {
776                                         if ((bindingAttr & BindingFlags.Instance) != 0)
777                                                 match = true;
778                                 }
779                                 if (!match)
780                                         continue;
781                                 return c;
782                         }
783                         return null;
784                 }
785
786                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
787                         if (fields == null)
788                                 return new FieldInfo [0];
789                         ArrayList l = new ArrayList ();
790                         bool match;
791                         FieldAttributes mattrs;
792                         
793                         foreach (FieldInfo c in fields) {
794                                 if (c == null)
795                                         continue;
796                                 match = false;
797                                 mattrs = c.Attributes;
798                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
799                                         if ((bindingAttr & BindingFlags.Public) != 0)
800                                                 match = true;
801                                 } else {
802                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
803                                                 match = true;
804                                 }
805                                 if (!match)
806                                         continue;
807                                 match = false;
808                                 if ((mattrs & FieldAttributes.Static) != 0) {
809                                         if ((bindingAttr & BindingFlags.Static) != 0)
810                                                 match = true;
811                                 } else {
812                                         if ((bindingAttr & BindingFlags.Instance) != 0)
813                                                 match = true;
814                                 }
815                                 if (!match)
816                                         continue;
817                                 l.Add (c);
818                         }
819                         FieldInfo[] result = new FieldInfo [l.Count];
820                         l.CopyTo (result);
821                         return result;
822                 }
823
824                 public override Type GetInterface (string name, bool ignoreCase) {
825                         throw not_supported ();
826                 }
827                 
828                 public override Type[] GetInterfaces () {
829                         if (interfaces != null) {
830                                 Type[] ret = new Type [interfaces.Length];
831                                 interfaces.CopyTo (ret, 0);
832                                 return ret;
833                         } else {
834                                 return Type.EmptyTypes;
835                         }
836                 }
837
838                 public override MemberInfo[] GetMember (string name, MemberTypes type,
839                                                                                                 BindingFlags bindingAttr) {
840                         throw not_supported ();
841                 }
842
843                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
844                         throw not_supported ();
845                 }
846
847                 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
848                         MethodInfo[] candidates;
849                         if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
850                                 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
851                                 if (methods == null)
852                                         candidates = parent_methods;
853                                 else {
854                                         candidates = new MethodInfo [methods.Length + parent_methods.Length];
855                                         parent_methods.CopyTo (candidates, 0);
856                                         methods.CopyTo (candidates, parent_methods.Length);
857                                 }
858                         }
859                         else
860                                 candidates = methods;
861                                         
862                         if (candidates == null)
863                                 return new MethodInfo [0];
864
865                         ArrayList l = new ArrayList ();
866                         bool match;
867                         MethodAttributes mattrs;
868
869                         foreach (MethodInfo c in candidates) {
870                                 if (c == null)
871                                         continue;
872                                 if (name != null) {
873                                         if (String.Compare (c.Name, name, ignoreCase) != 0)
874                                                 continue;
875                                 }
876                                 match = false;
877                                 mattrs = c.Attributes;
878                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
879                                         if ((bindingAttr & BindingFlags.Public) != 0)
880                                                 match = true;
881                                 } else {
882                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
883                                                 match = true;
884                                 }
885                                 if (!match)
886                                         continue;
887                                 match = false;
888                                 if ((mattrs & MethodAttributes.Static) != 0) {
889                                         if ((bindingAttr & BindingFlags.Static) != 0)
890                                                 match = true;
891                                 } else {
892                                         if ((bindingAttr & BindingFlags.Instance) != 0)
893                                                 match = true;
894                                 }
895                                 if (!match)
896                                         continue;
897                                 l.Add (c);
898                         }
899
900                         MethodInfo[] result = new MethodInfo [l.Count];
901                         l.CopyTo (result);
902                         return result;
903                 }
904
905                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
906                         return GetMethodsByName (null, bindingAttr, false, this);
907                 }
908
909                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
910                                                              Binder binder,
911                                                              CallingConventions callConvention,
912                                                              Type[] types, ParameterModifier[] modifiers)
913                 {
914                         if (!is_created)
915                                 /* MS.Net throws this exception if the type is unfinished... */
916                                 throw not_supported ();
917
918                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
919                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
920                         MethodInfo found = null;
921                         MethodBase[] match;
922                         int typesLen = (types != null) ? types.Length : 0;
923                         int count = 0;
924                         
925                         foreach (MethodInfo m in methods) {
926                                 // Under MS.NET, Standard|HasThis matches Standard...
927                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
928                                         continue;
929                                 found = m;
930                                 count++;
931                         }
932
933                         if (count == 0)
934                                 return null;
935                         
936                         if (count == 1 && typesLen == 0) 
937                                 return found;
938
939                         match = new MethodBase [count];
940                         if (count == 1)
941                                 match [0] = found;
942                         else {
943                                 count = 0;
944                                 foreach (MethodInfo m in methods) {
945                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
946                                                 continue;
947                                         match [count++] = m;
948                                 }
949                         }
950                         
951                         if (types == null) 
952                                 return (MethodInfo) Binder.FindMostDerivedMatch (match);
953
954                         if (binder == null)
955                                 binder = Binder.DefaultBinder;
956                         
957                         return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
958                 }
959
960                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
961                         throw not_supported ();
962                 }
963
964                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
965                         bool match;
966                         ArrayList result = new ArrayList ();
967                 
968                         if (subtypes == null)
969                                 return Type.EmptyTypes;
970                         foreach (TypeBuilder t in subtypes) {
971                                 match = false;
972                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
973                                         if ((bindingAttr & BindingFlags.Public) != 0)
974                                                 match = true;
975                                 } else {
976                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
977                                                 match = true;
978                                 }
979                                 if (!match)
980                                         continue;
981                                 result.Add (t);
982                         }
983                         Type[] r = new Type [result.Count];
984                         result.CopyTo (r);
985                         return r;
986                 }
987
988                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
989                         if (properties == null)
990                                 return new PropertyInfo [0];
991                         ArrayList l = new ArrayList ();
992                         bool match;
993                         MethodAttributes mattrs;
994                         MethodInfo accessor;
995                         
996                         foreach (PropertyInfo c in properties) {
997                                 match = false;
998                                 accessor = c.GetGetMethod (true);
999                                 if (accessor == null)
1000                                         accessor = c.GetSetMethod (true);
1001                                 if (accessor == null)
1002                                         continue;
1003                                 mattrs = accessor.Attributes;
1004                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1005                                         if ((bindingAttr & BindingFlags.Public) != 0)
1006                                                 match = true;
1007                                 } else {
1008                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1009                                                 match = true;
1010                                 }
1011                                 if (!match)
1012                                         continue;
1013                                 match = false;
1014                                 if ((mattrs & MethodAttributes.Static) != 0) {
1015                                         if ((bindingAttr & BindingFlags.Static) != 0)
1016                                                 match = true;
1017                                 } else {
1018                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1019                                                 match = true;
1020                                 }
1021                                 if (!match)
1022                                         continue;
1023                                 l.Add (c);
1024                         }
1025                         PropertyInfo[] result = new PropertyInfo [l.Count];
1026                         l.CopyTo (result);
1027                         return result;
1028                 }
1029                 
1030                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1031                         throw not_supported ();
1032                 }
1033
1034                 protected override bool HasElementTypeImpl () {
1035                         // According to the MSDN docs, this is supported for TypeBuilders,
1036                         // but in reality, it is not
1037                         throw not_supported ();
1038                         //                      return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1039                 }
1040
1041                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1042                         throw not_supported ();
1043                 }
1044
1045                 protected override bool IsArrayImpl ()
1046                 {
1047                         return Type.IsArrayImpl (this);
1048                 }
1049
1050                 protected override bool IsByRefImpl () {
1051                         // FIXME
1052                         return false;
1053                 }
1054                 protected override bool IsCOMObjectImpl () {
1055                         return false;
1056                 }
1057                 protected override bool IsPointerImpl () {
1058                         // FIXME
1059                         return false;
1060                 }
1061                 protected override bool IsPrimitiveImpl () {
1062                         // FIXME
1063                         return false;
1064                 }
1065                 protected override bool IsValueTypeImpl () {
1066                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1067                                 this != pmodule.assemblyb.corlib_value_type &&
1068                                 this != pmodule.assemblyb.corlib_enum_type);
1069                 }
1070                 
1071                 public override RuntimeTypeHandle TypeHandle { 
1072                         get { 
1073                                 throw not_supported (); 
1074                         } 
1075                 }
1076
1077                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1078                         if (customBuilder == null)
1079                                 throw new ArgumentNullException ("customBuilder");
1080
1081                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
1082                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1083                                 byte[] data = customBuilder.Data;
1084                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1085                                 layout_kind = (int)data [2];
1086                                 layout_kind |= ((int)data [3]) << 8;
1087                                 attrs &= ~TypeAttributes.LayoutMask;
1088                                 switch ((LayoutKind)layout_kind) {
1089                                 case LayoutKind.Auto:
1090                                         attrs |= TypeAttributes.AutoLayout;
1091                                         break;
1092                                 case LayoutKind.Explicit:
1093                                         attrs |= TypeAttributes.ExplicitLayout;
1094                                         break;
1095                                 case LayoutKind.Sequential:
1096                                         attrs |= TypeAttributes.SequentialLayout;
1097                                         break;
1098                                 default:
1099                                         // we should ignore it since it can be any value anyway...
1100                                         throw new Exception ("Error in customattr");
1101                                 }
1102                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1103                                 int pos = 6;
1104                                 if (first_type_name == "System.Int16")
1105                                         pos = 4;
1106                                 int nnamed = (int)data [pos++];
1107                                 nnamed |= ((int)data [pos++]) << 8;
1108                                 for (int i = 0; i < nnamed; ++i) {
1109                                         byte named_type = data [pos++];
1110                                         byte type = data [pos++];
1111                                         int len;
1112                                         string named_name;
1113
1114                                         if (type == 0x55) {
1115                                                 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1116                                                 string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1117                                                 pos += len;
1118                                                 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1119                                                 //        See related code/FIXME in mono/mono/metadata/reflection.c
1120                                         }
1121
1122                                         len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1123                                         named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1124                                         pos += len;
1125                                         /* all the fields are integers in StructLayout */
1126                                         int value = (int)data [pos++];
1127                                         value |= ((int)data [pos++]) << 8;
1128                                         value |= ((int)data [pos++]) << 16;
1129                                         value |= ((int)data [pos++]) << 24;
1130                                         switch (named_name) {
1131                                         case "CharSet":
1132                                                 switch ((CharSet)value) {
1133                                                 case CharSet.None:
1134                                                 case CharSet.Ansi:
1135                                                         break;
1136                                                 case CharSet.Unicode:
1137                                                         attrs |= TypeAttributes.UnicodeClass;
1138                                                         break;
1139                                                 case CharSet.Auto:
1140                                                         attrs |= TypeAttributes.AutoClass;
1141                                                         break;
1142                                                 default:
1143                                                         break; // error out...
1144                                                 }
1145                                                 break;
1146                                         case "Pack":
1147                                                 packing_size = (PackingSize)value;
1148                                                 break;
1149                                         case "Size":
1150                                                 class_size = value;
1151                                                 break;
1152                                         default:
1153                                                 break; // error out...
1154                                         }
1155                                 }
1156                                 return;
1157                         } else if (attrname == "System.SerializableAttribute") {
1158                                 attrs |= TypeAttributes.Serializable;
1159                                 return;
1160                         }
1161                         if (cattrs != null) {
1162                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1163                                 cattrs.CopyTo (new_array, 0);
1164                                 new_array [cattrs.Length] = customBuilder;
1165                                 cattrs = new_array;
1166                         } else {
1167                                 cattrs = new CustomAttributeBuilder [1];
1168                                 cattrs [0] = customBuilder;
1169                         }
1170                 }
1171                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1172                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1173                 }
1174
1175                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1176                         check_name ("name", name);
1177                         if (eventtype == null)
1178                                 throw new ArgumentNullException ("eventtype");
1179                         if (is_created)
1180                                 throw not_after_created ();
1181
1182                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1183                         if (events != null) {
1184                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1185                                 System.Array.Copy (events, new_events, events.Length);
1186                                 new_events [events.Length] = res;
1187                                 events = new_events;
1188                         } else {
1189                                 events = new EventBuilder [1];
1190                                 events [0] = res;
1191                         }
1192                         return res;
1193                 }
1194
1195                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1196                         if (data == null)
1197                                 throw new ArgumentNullException ("data");
1198                         if ((data.Length == 0) || (data.Length > 0x3f0000))
1199                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1200
1201                         FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1202                         res.SetRVAData (data);
1203
1204                         return res;
1205                 }
1206
1207                 static int UnmanagedDataCount = 0;
1208                 
1209                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1210                         check_name ("name", name);
1211                         if ((size <= 0) || (size > 0x3f0000))
1212                                 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1213                         if (is_created)
1214                                 throw not_after_created ();
1215
1216                         string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1217                         UnmanagedDataCount++;
1218                         TypeBuilder datablobtype = DefineNestedType (s,
1219                                 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1220                                 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1221                         datablobtype.CreateType ();
1222                         return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1223                 }
1224
1225                 public TypeToken TypeToken {
1226                         get {
1227                                 return new TypeToken (0x02000000 | table_idx);
1228                         }
1229                 }
1230                 public void SetParent (Type parentType) {
1231                         if (parentType == null)
1232                                 throw new ArgumentNullException ("parentType");
1233                         if (is_created)
1234                                 throw not_after_created ();
1235
1236                         parent = parentType;
1237                 }
1238                 internal int get_next_table_index (object obj, int table, bool inc) {
1239                         return pmodule.get_next_table_index (obj, table, inc);
1240                 }
1241
1242                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1243                 {
1244                         if (created == null)
1245                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1246
1247                         return created.GetInterfaceMap (interfaceType);
1248                 }
1249
1250                 internal bool is_created {
1251                         get {
1252                                 return created != null;
1253                         }
1254                 }
1255
1256                 private Exception not_supported ()
1257                 {
1258                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1259                 }
1260
1261                 private Exception not_after_created ()
1262                 {
1263                         return new InvalidOperationException ("Unable to change after type has been created.");
1264                 }
1265
1266                 private void check_name (string argName, string name)
1267                 {
1268                         if (name == null)
1269                                 throw new ArgumentNullException (argName);
1270                         if (name == "")
1271                                 throw new ArgumentException (argName, "Empty name is not legal.");
1272                         if (name.IndexOf ((char)0) != -1)
1273                                 throw new ArgumentException (argName, "Illegal name.");
1274                 }
1275
1276 #if NET_2_0 || BOOTSTRAP_NET_2_0
1277                 public override Type[] GetGenericArguments ()
1278                 {
1279                         if (generic_params != null)
1280                                 return generic_params;
1281
1282                         throw new InvalidOperationException ();
1283                 }
1284
1285                 public override Type GetGenericTypeDefinition ()
1286                 {
1287                         setup_generic_class (this);
1288
1289                         return base.GetGenericTypeDefinition ();
1290                 }
1291
1292                 public override bool HasGenericArguments {
1293                         get {
1294                                 throw new NotImplementedException ();
1295                         }
1296                 }
1297
1298                 public override bool ContainsGenericParameters {
1299                         get {
1300                                 return generic_params != null;
1301                         }
1302                 }
1303
1304                 public extern override bool IsGenericParameter {
1305                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1306                         get;
1307                 }
1308
1309                 public override int GenericParameterPosition {
1310                         get {
1311                                 throw new NotImplementedException ();
1312                         }
1313                 }
1314
1315                 public override MethodInfo DeclaringMethod {
1316                         get {
1317                                 throw new NotImplementedException ();
1318                         }
1319                 }
1320
1321                 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
1322                 {
1323                         generic_params = new GenericTypeParameterBuilder [names.Length];
1324                         for (int i = 0; i < names.Length; i++)
1325                                 generic_params [i] = new GenericTypeParameterBuilder (
1326                                         this, null, names [i], i);
1327
1328                         return generic_params;
1329                 }
1330
1331                 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1332                 {
1333                         return DefineMethod (name, attributes, CallingConventions.Standard, null, null);
1334                 }
1335 #endif
1336         }
1337 }