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.table_idx = 1;
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                                 DefineDefaultConstructor (MethodAttributes.Public);
656
657                         if (ctors != null){
658                                 foreach (ConstructorBuilder ctor in ctors) 
659                                         ctor.fixup ();
660                         }
661
662                         created = create_runtime_class (this);
663                         if (created != null)
664                                 return created;
665                         return this;
666                 }
667
668                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
669                 {
670                         if (ctors == null)
671                                 return new ConstructorInfo [0];
672                         ArrayList l = new ArrayList ();
673                         bool match;
674                         MethodAttributes mattrs;
675                         
676                         foreach (ConstructorBuilder c in ctors) {
677                                 match = false;
678                                 mattrs = c.Attributes;
679                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
680                                         if ((bindingAttr & BindingFlags.Public) != 0)
681                                                 match = true;
682                                 } else {
683                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
684                                                 match = true;
685                                 }
686                                 if (!match)
687                                         continue;
688                                 match = false;
689                                 if ((mattrs & MethodAttributes.Static) != 0) {
690                                         if ((bindingAttr & BindingFlags.Static) != 0)
691                                                 match = true;
692                                 } else {
693                                         if ((bindingAttr & BindingFlags.Instance) != 0)
694                                                 match = true;
695                                 }
696                                 if (!match)
697                                         continue;
698                                 l.Add (c);
699                         }
700                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
701                         l.CopyTo (result);
702                         return result;
703                 }
704
705                 public override Type GetElementType () { 
706                         throw not_supported ();
707                 }
708
709                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
710                         throw not_supported ();
711                 }
712
713                 /* Needed to keep signature compatibility with MS.NET */
714                 public override EventInfo[] GetEvents ()
715                 {
716                         return GetEvents (DefaultBindingFlags);
717                 }
718
719                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
720                         // FIXME: Under MS.NET, this throws a NotImplementedException
721                         // But mcs calls this method. How can that be?
722                         return new EventInfo [0];
723                 }
724
725                 // This is only used from MonoGenericInst.initialize().
726                 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
727                 {
728                         if (events == null)
729                                 return new EventInfo [0];
730                         ArrayList l = new ArrayList ();
731                         bool match;
732                         MethodAttributes mattrs;
733                         MethodInfo accessor;
734
735                         foreach (EventBuilder eb in events) {
736                                 if (eb == null)
737                                         continue;
738                                 EventInfo c = get_event_info (eb);
739                                 match = false;
740                                 accessor = c.GetAddMethod (true);
741                                 if (accessor == null)
742                                         accessor = c.GetRemoveMethod (true);
743                                 if (accessor == null)
744                                         continue;
745                                 mattrs = accessor.Attributes;
746                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
747                                         if ((bindingAttr & BindingFlags.Public) != 0)
748                                                 match = true;
749                                 } else {
750                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
751                                                 match = true;
752                                 }
753                                 if (!match)
754                                         continue;
755                                 match = false;
756                                 if ((mattrs & MethodAttributes.Static) != 0) {
757                                         if ((bindingAttr & BindingFlags.Static) != 0)
758                                                 match = true;
759                                 } else {
760                                         if ((bindingAttr & BindingFlags.Instance) != 0)
761                                                 match = true;
762                                 }
763                                 if (!match)
764                                         continue;
765                                 l.Add (c);
766                         }
767                         EventInfo[] result = new EventInfo [l.Count];
768                         l.CopyTo (result);
769                         return result;
770                 }
771
772                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
773                         if (fields == null)
774                                 return null;
775
776                         bool match;
777                         FieldAttributes mattrs;
778                         
779                         foreach (FieldInfo c in fields) {
780                                 if (c == null)
781                                         continue;
782                                 if (c.Name != name)
783                                         continue;
784                                 match = false;
785                                 mattrs = c.Attributes;
786                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
787                                         if ((bindingAttr & BindingFlags.Public) != 0)
788                                                 match = true;
789                                 } else {
790                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
791                                                 match = true;
792                                 }
793                                 if (!match)
794                                         continue;
795                                 match = false;
796                                 if ((mattrs & FieldAttributes.Static) != 0) {
797                                         if ((bindingAttr & BindingFlags.Static) != 0)
798                                                 match = true;
799                                 } else {
800                                         if ((bindingAttr & BindingFlags.Instance) != 0)
801                                                 match = true;
802                                 }
803                                 if (!match)
804                                         continue;
805                                 return c;
806                         }
807                         return null;
808                 }
809
810                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
811                         if (fields == null)
812                                 return new FieldInfo [0];
813                         ArrayList l = new ArrayList ();
814                         bool match;
815                         FieldAttributes mattrs;
816                         
817                         foreach (FieldInfo c in fields) {
818                                 if (c == null)
819                                         continue;
820                                 match = false;
821                                 mattrs = c.Attributes;
822                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
823                                         if ((bindingAttr & BindingFlags.Public) != 0)
824                                                 match = true;
825                                 } else {
826                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
827                                                 match = true;
828                                 }
829                                 if (!match)
830                                         continue;
831                                 match = false;
832                                 if ((mattrs & FieldAttributes.Static) != 0) {
833                                         if ((bindingAttr & BindingFlags.Static) != 0)
834                                                 match = true;
835                                 } else {
836                                         if ((bindingAttr & BindingFlags.Instance) != 0)
837                                                 match = true;
838                                 }
839                                 if (!match)
840                                         continue;
841                                 l.Add (c);
842                         }
843                         FieldInfo[] result = new FieldInfo [l.Count];
844                         l.CopyTo (result);
845                         return result;
846                 }
847
848                 public override Type GetInterface (string name, bool ignoreCase) {
849                         throw not_supported ();
850                 }
851                 
852                 public override Type[] GetInterfaces () {
853                         if (interfaces != null) {
854                                 Type[] ret = new Type [interfaces.Length];
855                                 interfaces.CopyTo (ret, 0);
856                                 return ret;
857                         } else {
858                                 return Type.EmptyTypes;
859                         }
860                 }
861
862                 public override MemberInfo[] GetMember (string name, MemberTypes type,
863                                                                                                 BindingFlags bindingAttr) {
864                         throw not_supported ();
865                 }
866
867                 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
868                         throw not_supported ();
869                 }
870
871                 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
872                         MethodInfo[] candidates;
873                         if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
874                                 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
875                                 if (methods == null)
876                                         candidates = parent_methods;
877                                 else {
878                                         candidates = new MethodInfo [methods.Length + parent_methods.Length];
879                                         parent_methods.CopyTo (candidates, 0);
880                                         methods.CopyTo (candidates, parent_methods.Length);
881                                 }
882                         }
883                         else
884                                 candidates = methods;
885                                         
886                         if (candidates == null)
887                                 return new MethodInfo [0];
888
889                         ArrayList l = new ArrayList ();
890                         bool match;
891                         MethodAttributes mattrs;
892
893                         foreach (MethodInfo c in candidates) {
894                                 if (c == null)
895                                         continue;
896                                 if (name != null) {
897                                         if (String.Compare (c.Name, name, ignoreCase) != 0)
898                                                 continue;
899                                 }
900                                 match = false;
901                                 mattrs = c.Attributes;
902                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
903                                         if ((bindingAttr & BindingFlags.Public) != 0)
904                                                 match = true;
905                                 } else {
906                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
907                                                 match = true;
908                                 }
909                                 if (!match)
910                                         continue;
911                                 match = false;
912                                 if ((mattrs & MethodAttributes.Static) != 0) {
913                                         if ((bindingAttr & BindingFlags.Static) != 0)
914                                                 match = true;
915                                 } else {
916                                         if ((bindingAttr & BindingFlags.Instance) != 0)
917                                                 match = true;
918                                 }
919                                 if (!match)
920                                         continue;
921                                 l.Add (c);
922                         }
923
924                         MethodInfo[] result = new MethodInfo [l.Count];
925                         l.CopyTo (result);
926                         return result;
927                 }
928
929                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
930                         return GetMethodsByName (null, bindingAttr, false, this);
931                 }
932
933                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
934                                                              Binder binder,
935                                                              CallingConventions callConvention,
936                                                              Type[] types, ParameterModifier[] modifiers)
937                 {
938                         if (!is_created)
939                                 /* MS.Net throws this exception if the type is unfinished... */
940                                 throw not_supported ();
941
942                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
943                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
944                         MethodInfo found = null;
945                         MethodBase[] match;
946                         int typesLen = (types != null) ? types.Length : 0;
947                         int count = 0;
948                         
949                         foreach (MethodInfo m in methods) {
950                                 // Under MS.NET, Standard|HasThis matches Standard...
951                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
952                                         continue;
953                                 found = m;
954                                 count++;
955                         }
956
957                         if (count == 0)
958                                 return null;
959                         
960                         if (count == 1 && typesLen == 0) 
961                                 return found;
962
963                         match = new MethodBase [count];
964                         if (count == 1)
965                                 match [0] = found;
966                         else {
967                                 count = 0;
968                                 foreach (MethodInfo m in methods) {
969                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
970                                                 continue;
971                                         match [count++] = m;
972                                 }
973                         }
974                         
975                         if (types == null) 
976                                 return (MethodInfo) Binder.FindMostDerivedMatch (match);
977
978                         if (binder == null)
979                                 binder = Binder.DefaultBinder;
980                         
981                         return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
982                 }
983
984                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
985                         throw not_supported ();
986                 }
987
988                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
989                         bool match;
990                         ArrayList result = new ArrayList ();
991
992                         if (subtypes == null)
993                                 return Type.EmptyTypes;
994                         foreach (TypeBuilder t in subtypes) {
995                                 match = false;
996                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
997                                         if ((bindingAttr & BindingFlags.Public) != 0)
998                                                 match = true;
999                                 } else {
1000                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1001                                                 match = true;
1002                                 }
1003                                 if (!match)
1004                                         continue;
1005                                 result.Add (t);
1006                         }
1007                         Type[] r = new Type [result.Count];
1008                         result.CopyTo (r);
1009                         return r;
1010                 }
1011
1012                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
1013                         if (properties == null)
1014                                 return new PropertyInfo [0];
1015                         ArrayList l = new ArrayList ();
1016                         bool match;
1017                         MethodAttributes mattrs;
1018                         MethodInfo accessor;
1019                         
1020                         foreach (PropertyInfo c in properties) {
1021                                 match = false;
1022                                 accessor = c.GetGetMethod (true);
1023                                 if (accessor == null)
1024                                         accessor = c.GetSetMethod (true);
1025                                 if (accessor == null)
1026                                         continue;
1027                                 mattrs = accessor.Attributes;
1028                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1029                                         if ((bindingAttr & BindingFlags.Public) != 0)
1030                                                 match = true;
1031                                 } else {
1032                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
1033                                                 match = true;
1034                                 }
1035                                 if (!match)
1036                                         continue;
1037                                 match = false;
1038                                 if ((mattrs & MethodAttributes.Static) != 0) {
1039                                         if ((bindingAttr & BindingFlags.Static) != 0)
1040                                                 match = true;
1041                                 } else {
1042                                         if ((bindingAttr & BindingFlags.Instance) != 0)
1043                                                 match = true;
1044                                 }
1045                                 if (!match)
1046                                         continue;
1047                                 l.Add (c);
1048                         }
1049                         PropertyInfo[] result = new PropertyInfo [l.Count];
1050                         l.CopyTo (result);
1051                         return result;
1052                 }
1053                 
1054                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1055                         throw not_supported ();
1056                 }
1057
1058                 protected override bool HasElementTypeImpl () {
1059                         // According to the MSDN docs, this is supported for TypeBuilders,
1060                         // but in reality, it is not
1061                         throw not_supported ();
1062                         //                      return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1063                 }
1064
1065                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1066                         throw not_supported ();
1067                 }
1068
1069                 protected override bool IsArrayImpl ()
1070                 {
1071                         return Type.IsArrayImpl (this);
1072                 }
1073
1074                 protected override bool IsByRefImpl () {
1075                         // FIXME
1076                         return false;
1077                 }
1078                 protected override bool IsCOMObjectImpl () {
1079                         return false;
1080                 }
1081                 protected override bool IsPointerImpl () {
1082                         // FIXME
1083                         return false;
1084                 }
1085                 protected override bool IsPrimitiveImpl () {
1086                         // FIXME
1087                         return false;
1088                 }
1089                 protected override bool IsValueTypeImpl () {
1090                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1091                                 this != pmodule.assemblyb.corlib_value_type &&
1092                                 this != pmodule.assemblyb.corlib_enum_type);
1093                 }
1094                 
1095                 public override RuntimeTypeHandle TypeHandle { 
1096                         get { 
1097                                 throw not_supported (); 
1098                         } 
1099                 }
1100
1101                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1102                         if (customBuilder == null)
1103                                 throw new ArgumentNullException ("customBuilder");
1104
1105                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
1106                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1107                                 byte[] data = customBuilder.Data;
1108                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1109                                 layout_kind = (int)data [2];
1110                                 layout_kind |= ((int)data [3]) << 8;
1111                                 attrs &= ~TypeAttributes.LayoutMask;
1112                                 switch ((LayoutKind)layout_kind) {
1113                                 case LayoutKind.Auto:
1114                                         attrs |= TypeAttributes.AutoLayout;
1115                                         break;
1116                                 case LayoutKind.Explicit:
1117                                         attrs |= TypeAttributes.ExplicitLayout;
1118                                         break;
1119                                 case LayoutKind.Sequential:
1120                                         attrs |= TypeAttributes.SequentialLayout;
1121                                         break;
1122                                 default:
1123                                         // we should ignore it since it can be any value anyway...
1124                                         throw new Exception ("Error in customattr");
1125                                 }
1126                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1127                                 int pos = 6;
1128                                 if (first_type_name == "System.Int16")
1129                                         pos = 4;
1130                                 int nnamed = (int)data [pos++];
1131                                 nnamed |= ((int)data [pos++]) << 8;
1132                                 for (int i = 0; i < nnamed; ++i) {
1133                                         byte named_type = data [pos++];
1134                                         byte type = data [pos++];
1135                                         int len;
1136                                         string named_name;
1137
1138                                         if (type == 0x55) {
1139                                                 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1140                                                 string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1141                                                 pos += len;
1142                                                 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1143                                                 //        See related code/FIXME in mono/mono/metadata/reflection.c
1144                                         }
1145
1146                                         len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1147                                         named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1148                                         pos += len;
1149                                         /* all the fields are integers in StructLayout */
1150                                         int value = (int)data [pos++];
1151                                         value |= ((int)data [pos++]) << 8;
1152                                         value |= ((int)data [pos++]) << 16;
1153                                         value |= ((int)data [pos++]) << 24;
1154                                         switch (named_name) {
1155                                         case "CharSet":
1156                                                 switch ((CharSet)value) {
1157                                                 case CharSet.None:
1158                                                 case CharSet.Ansi:
1159                                                         break;
1160                                                 case CharSet.Unicode:
1161                                                         attrs |= TypeAttributes.UnicodeClass;
1162                                                         break;
1163                                                 case CharSet.Auto:
1164                                                         attrs |= TypeAttributes.AutoClass;
1165                                                         break;
1166                                                 default:
1167                                                         break; // error out...
1168                                                 }
1169                                                 break;
1170                                         case "Pack":
1171                                                 packing_size = (PackingSize)value;
1172                                                 break;
1173                                         case "Size":
1174                                                 class_size = value;
1175                                                 break;
1176                                         default:
1177                                                 break; // error out...
1178                                         }
1179                                 }
1180                                 return;
1181                         } else if (attrname == "System.SerializableAttribute") {
1182                                 attrs |= TypeAttributes.Serializable;
1183                                 return;
1184                         }
1185                         if (cattrs != null) {
1186                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1187                                 cattrs.CopyTo (new_array, 0);
1188                                 new_array [cattrs.Length] = customBuilder;
1189                                 cattrs = new_array;
1190                         } else {
1191                                 cattrs = new CustomAttributeBuilder [1];
1192                                 cattrs [0] = customBuilder;
1193                         }
1194                 }
1195                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1196                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1197                 }
1198
1199                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1200                         check_name ("name", name);
1201                         if (eventtype == null)
1202                                 throw new ArgumentNullException ("eventtype");
1203                         if (is_created)
1204                                 throw not_after_created ();
1205
1206                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1207                         if (events != null) {
1208                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1209                                 System.Array.Copy (events, new_events, events.Length);
1210                                 new_events [events.Length] = res;
1211                                 events = new_events;
1212                         } else {
1213                                 events = new EventBuilder [1];
1214                                 events [0] = res;
1215                         }
1216                         return res;
1217                 }
1218
1219                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1220                         if (data == null)
1221                                 throw new ArgumentNullException ("data");
1222                         if ((data.Length == 0) || (data.Length > 0x3f0000))
1223                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1224
1225                         FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1226                         res.SetRVAData (data);
1227
1228                         return res;
1229                 }
1230
1231                 static int UnmanagedDataCount = 0;
1232                 
1233                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1234                         check_name ("name", name);
1235                         if ((size <= 0) || (size > 0x3f0000))
1236                                 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1237                         if (is_created)
1238                                 throw not_after_created ();
1239
1240                         string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1241                         UnmanagedDataCount++;
1242                         TypeBuilder datablobtype = DefineNestedType (s,
1243                                 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1244                                 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1245                         datablobtype.CreateType ();
1246                         return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1247                 }
1248
1249                 public TypeToken TypeToken {
1250                         get {
1251                                 return new TypeToken (0x02000000 | table_idx);
1252                         }
1253                 }
1254                 public void SetParent (Type parentType) {
1255                         if (parentType == null)
1256                                 throw new ArgumentNullException ("parentType");
1257                         if (is_created)
1258                                 throw not_after_created ();
1259
1260                         parent = parentType;
1261                         // will just set the parent-related bits if called a second time
1262                         setup_internal_class (this);
1263                 }
1264                 internal int get_next_table_index (object obj, int table, bool inc) {
1265                         return pmodule.get_next_table_index (obj, table, inc);
1266                 }
1267
1268                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1269                 {
1270                         if (created == null)
1271                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1272
1273                         return created.GetInterfaceMap (interfaceType);
1274                 }
1275
1276                 internal bool is_created {
1277                         get {
1278                                 return created != null;
1279                         }
1280                 }
1281
1282                 private Exception not_supported ()
1283                 {
1284                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1285                 }
1286
1287                 private Exception not_after_created ()
1288                 {
1289                         return new InvalidOperationException ("Unable to change after type has been created.");
1290                 }
1291
1292                 private void check_name (string argName, string name)
1293                 {
1294                         if (name == null)
1295                                 throw new ArgumentNullException (argName);
1296                         if (name == "")
1297                                 throw new ArgumentException (argName, "Empty name is not legal.");
1298                         if (name.IndexOf ((char)0) != -1)
1299                                 throw new ArgumentException (argName, "Illegal name.");
1300                 }
1301
1302                 public override String ToString ()
1303                 {
1304                         return FullName;
1305                 }
1306
1307                 [MonoTODO]
1308                 public override bool IsAssignableFrom (Type c)
1309                 {
1310                         return base.IsAssignableFrom (c);
1311                 }
1312
1313                 [MonoTODO]
1314                 public override bool IsSubclassOf (Type c)
1315                 {
1316                         return base.IsSubclassOf (c);
1317                 }
1318
1319 #if NET_2_0 || BOOTSTRAP_NET_2_0
1320                 public override Type[] GetGenericArguments ()
1321                 {
1322                         if (generic_params != null)
1323                                 return generic_params;
1324
1325                         throw new InvalidOperationException ();
1326                 }
1327
1328                 public override Type GetGenericTypeDefinition ()
1329                 {
1330                         setup_generic_class (this);
1331
1332                         return base.GetGenericTypeDefinition ();
1333                 }
1334
1335                 public override bool HasGenericArguments {
1336                         get {
1337                                 throw new NotImplementedException ();
1338                         }
1339                 }
1340
1341                 public override bool ContainsGenericParameters {
1342                         get {
1343                                 return generic_params != null;
1344                         }
1345                 }
1346
1347                 public extern override bool IsGenericParameter {
1348                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1349                         get;
1350                 }
1351
1352                 public override int GenericParameterPosition {
1353                         get {
1354                                 throw new NotImplementedException ();
1355                         }
1356                 }
1357
1358                 public override MethodInfo DeclaringMethod {
1359                         get {
1360                                 throw new NotImplementedException ();
1361                         }
1362                 }
1363
1364                 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
1365                 {
1366                         generic_params = new GenericTypeParameterBuilder [names.Length];
1367                         for (int i = 0; i < names.Length; i++)
1368                                 generic_params [i] = new GenericTypeParameterBuilder (
1369                                         this, null, names [i], i);
1370
1371                         return generic_params;
1372                 }
1373
1374                 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1375                 {
1376                         return DefineMethod (name, attributes, CallingConventions.Standard, null, null);
1377                 }
1378 #endif
1379         }
1380 }