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