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