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