2002-12-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
1 //
2 // System.Reflection.Emit/TypeBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 using System.Globalization;
16 using System.Collections;
17 using System.Security;
18 using System.Security.Permissions;
19
20 namespace System.Reflection.Emit {
21         public sealed class TypeBuilder : Type {
22         private string tname;
23         private string nspace;
24         private Type parent;
25         private Type nesting_type;
26         private Type[] interfaces;
27         private MethodBuilder[] methods;
28         private ConstructorBuilder[] ctors;
29         private PropertyBuilder[] properties;
30         private FieldBuilder[] fields;
31         private EventBuilder[] events;
32         private CustomAttributeBuilder[] cattrs;
33         internal TypeBuilder[] subtypes;
34         private TypeAttributes attrs;
35         private int table_idx;
36         private ModuleBuilder pmodule;
37         private int class_size;
38         private PackingSize packing_size;
39         private Type created;
40
41         public const int UnspecifiedTypeSize = 0;
42
43                 protected override TypeAttributes GetAttributeFlagsImpl () {
44                         return attrs;
45                 }
46                 
47                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48                 private extern void setup_internal_class (TypeBuilder tb);
49                 
50                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51                 private extern void create_internal_class (TypeBuilder tb);
52                 
53                 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size) {
54                         int sep_index;
55                         this.parent = parent;
56                         this.attrs = attr;
57                         this.class_size = type_size;
58                         this.packing_size = packing_size;
59                         sep_index = name.LastIndexOf('.');
60                         if (sep_index != -1) {
61                                 this.tname = name.Substring (sep_index + 1);
62                                 this.nspace = name.Substring (0, sep_index);
63                         } else {
64                                 this.tname = name;
65                                 this.nspace = "";
66                         }
67                         if (interfaces != null) {
68                                 this.interfaces = new Type[interfaces.Length];
69                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
70                         }
71                         pmodule = mb;
72                         // skip .<Module> ?
73                         table_idx = mb.get_next_table_index (this, 0x02, true);
74                         setup_internal_class (this);
75                 }
76
77                 public override Assembly Assembly {
78                         get {return pmodule.Assembly;}
79                 }
80                 public override string AssemblyQualifiedName {
81                         get {
82                                 return FullName + ", " + Assembly.ToString();
83                         }
84                 }
85                 public override Type BaseType {
86                         get {
87                                 return parent;
88                         }
89                 }
90                 public override Type DeclaringType {get {return nesting_type;}}
91                 public override Type UnderlyingSystemType {
92                         get {
93                                 if (fields != null) {
94                                         foreach (FieldBuilder f in fields) {
95                                                 if ((f.Attributes & FieldAttributes.Static) == 0)
96                                                         return f.FieldType;
97                                         }
98                                 }
99                                 throw new InvalidOperationException (String.Format ("typebuilder: {0}", this));
100                         }
101                 }
102
103                 public override string FullName {
104                         get {
105                                 if (nesting_type != null)
106                                         return String.Concat (nesting_type.FullName, "+", tname);
107                                 if ((nspace != null) && (nspace.Length > 0))
108                                         return String.Concat (nspace, ".", tname);
109                                 return tname;
110                         }
111                 }
112         
113                 public override Guid GUID {
114                         get {return Guid.Empty;}
115                 }
116
117                 public override Module Module {
118                         get {return pmodule;}
119                 }
120                 public override string Name {
121                         get {return tname;}
122                 }
123                 public override string Namespace {
124                         get {return nspace;}
125                 }
126                 public PackingSize PackingSize {
127                         get {return packing_size;}
128                 }
129                 public override Type ReflectedType {get {return nesting_type;}}
130                 public override MemberTypes MemberType { 
131                         get {return MemberTypes.TypeInfo;}
132                 }
133
134                 [MonoTODO]
135                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
136                         throw new NotImplementedException ();
137                 }
138
139                 public void AddInterfaceImplementation( Type interfaceType) {
140                         if (interfaces != null) {
141                                 Type[] ifnew = new Type [interfaces.Length + 1];
142                                 interfaces.CopyTo (ifnew, 0);
143                                 ifnew [interfaces.Length] = interfaceType;
144                                 interfaces = ifnew;
145                         } else {
146                                 interfaces = new Type [1];
147                                 interfaces [0] = interfaceType;
148                         }
149                 }
150
151                 [MonoTODO]
152                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
153                         throw new NotImplementedException ();
154                 }
155
156                 public override bool IsDefined( Type attributeType, bool inherit) {
157                         return false;
158                 }
159                 public override object[] GetCustomAttributes(bool inherit) {
160                         return null;
161                 }
162                 public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
163                         return null;
164                 }
165
166                 [MonoTODO]
167                 public TypeBuilder DefineNestedType (string name) {
168                         // FIXME: LAMESPEC: what other attributes should we use here as default?
169                         return DefineNestedType (name, TypeAttributes.Public, pmodule.assemblyb.corlib_object_type, null);
170                 }
171
172                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
173                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
174                 }
175
176                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
177                         return DefineNestedType (name, attr, parent, null);
178                 }
179
180                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packsize, int typesize) {
181                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize);
182                         res.nesting_type = this;
183                         if (subtypes != null) {
184                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
185                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
186                                 new_types [subtypes.Length] = res;
187                                 subtypes = new_types;
188                         } else {
189                                 subtypes = new TypeBuilder [1];
190                                 subtypes [0] = res;
191                         }
192                         return res;
193                 }
194
195                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
196                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
197                 }
198
199                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
200                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
201                 }
202
203                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
204                         return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
205                 }
206
207                 public ConstructorBuilder DefineConstructor( MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
208                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
209                         if (ctors != null) {
210                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
211                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
212                                 new_ctors [ctors.Length] = cb;
213                                 ctors = new_ctors;
214                         } else {
215                                 ctors = new ConstructorBuilder [1];
216                                 ctors [0] = cb;
217                         }
218                         return cb;
219                 }
220
221                 public ConstructorBuilder DefineDefaultConstructor( MethodAttributes attributes) {
222                         return DefineConstructor (attributes, CallingConventions.Standard, null);
223                 }
224
225                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
226                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
227                 }
228
229                 private void append_method (MethodBuilder mb) {
230                         if (methods != null) {
231                                 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
232                                 System.Array.Copy (methods, new_methods, methods.Length);
233                                 new_methods [methods.Length] = mb;
234                                 methods = new_methods;
235                         } else {
236                                 methods = new MethodBuilder [1];
237                                 methods [0] = mb;
238                         }
239                 }
240
241                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
242                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
243                         append_method (res);
244                         return res;
245                 }
246
247                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
248                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
249                                 dllName, entryName, nativeCallConv, nativeCharSet);
250                         append_method (res);
251                         return res;
252                 }
253
254                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
255                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
256                                 nativeCallConv, nativeCharSet);
257                 }
258
259                 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
260                         if (methodInfoBody is MethodBuilder) {
261                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
262                                 mb.set_override (methodInfoDeclaration);
263                         }
264                 }
265
266                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
267                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
268                         if (fields != null) {
269                                 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
270                                 System.Array.Copy (fields, new_fields, fields.Length);
271                                 new_fields [fields.Length] = res;
272                                 fields = new_fields;
273                         } else {
274                                 fields = new FieldBuilder [1];
275                                 fields [0] = res;
276                                 create_internal_class (this);
277                         }
278                         return res;
279                 }
280
281                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
282                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
283
284                         if (properties != null) {
285                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
286                                 System.Array.Copy (properties, new_properties, properties.Length);
287                                 new_properties [properties.Length] = res;
288                                 properties = new_properties;
289                         } else {
290                                 properties = new PropertyBuilder [1];
291                                 properties [0] = res;
292                         }
293                         return res;
294                 }
295
296                 [MonoTODO]
297                 public ConstructorBuilder DefineTypeInitializer() {
298                         throw new NotImplementedException ();
299                 }
300
301                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
302                 private extern Type create_runtime_class (TypeBuilder tb);
303                 
304                 public Type CreateType() {
305                         /* handle nesting_type */
306                         if (created != null) {
307                                 string err="type already created: " + created.ToString();
308                                 throw new InvalidOperationException (err);
309                         }
310                         if (methods != null) {
311                                 foreach (MethodBuilder method in methods) {
312                                         method.fixup ();
313                                 }
314                         }
315                         if (ctors != null) {
316                                 foreach (ConstructorBuilder ctor in ctors) {
317                                         ctor.fixup ();
318                                 }
319                         }
320                         created = create_runtime_class (this);
321                         if (created != null)
322                                 return created;
323                         return this;
324                 }
325
326                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr) {
327                         if (ctors == null)
328                                 return new ConstructorInfo [0];
329                         ArrayList l = new ArrayList ();
330                         bool match;
331                         MethodAttributes mattrs;
332                         
333                         foreach (ConstructorBuilder c in ctors) {
334                                 match = false;
335                                 mattrs = c.Attributes;
336                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
337                                         if ((bindingAttr & BindingFlags.Public) != 0)
338                                                 match = true;
339                                 } else {
340                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
341                                                 match = true;
342                                 }
343                                 if (!match)
344                                         continue;
345                                 match = false;
346                                 if ((mattrs & MethodAttributes.Static) != 0) {
347                                         if ((bindingAttr & BindingFlags.Static) != 0)
348                                                 match = true;
349                                 } else {
350                                         if ((bindingAttr & BindingFlags.Instance) != 0)
351                                                 match = true;
352                                 }
353                                 if (!match)
354                                         continue;
355                                 l.Add (c);
356                         }
357                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
358                         l.CopyTo (result);
359                         return result;
360                 }
361
362                 public override Type GetElementType () { return null; }
363
364                 [MonoTODO]
365                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
366                         throw new NotImplementedException ();
367                 }
368
369                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
370                         return new EventInfo [0];
371                 }
372
373                 [MonoTODO]
374                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
375                         //FIXME
376                         throw new NotImplementedException ();
377                 }
378
379                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
380                         if (fields == null)
381                                 return new FieldInfo [0];
382                         ArrayList l = new ArrayList ();
383                         bool match;
384                         FieldAttributes mattrs;
385                         
386                         foreach (FieldInfo c in fields) {
387                                 match = false;
388                                 mattrs = c.Attributes;
389                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
390                                         if ((bindingAttr & BindingFlags.Public) != 0)
391                                                 match = true;
392                                 } else {
393                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
394                                                 match = true;
395                                 }
396                                 if (!match)
397                                         continue;
398                                 match = false;
399                                 if ((mattrs & FieldAttributes.Static) != 0) {
400                                         if ((bindingAttr & BindingFlags.Static) != 0)
401                                                 match = true;
402                                 } else {
403                                         if ((bindingAttr & BindingFlags.Instance) != 0)
404                                                 match = true;
405                                 }
406                                 if (!match)
407                                         continue;
408                                 l.Add (c);
409                         }
410                         FieldInfo[] result = new FieldInfo [l.Count];
411                         l.CopyTo (result);
412                         return result;
413                 }
414
415                 [MonoTODO]
416                 public override Type GetInterface (string name, bool ignoreCase) {
417                         throw new NotImplementedException ();
418                 }
419                 
420                 public override Type[] GetInterfaces () {
421                         if (interfaces != null) {
422                                 Type[] ret = new Type [interfaces.Length];
423                                 interfaces.CopyTo (ret, 0);
424                                 return ret;
425                         } else {
426                                 return Type.EmptyTypes;
427                         }
428                 }
429
430                 [MonoTODO]
431                 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
432                         // FIXME
433                         throw new NotImplementedException ();
434                 }
435
436                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
437                         if (methods == null)
438                                 return new MethodInfo [0];
439                         ArrayList l = new ArrayList ();
440                         bool match;
441                         MethodAttributes mattrs;
442
443                         foreach (MethodInfo c in methods) {
444                                 match = false;
445                                 mattrs = c.Attributes;
446                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
447                                         if ((bindingAttr & BindingFlags.Public) != 0)
448                                                 match = true;
449                                 } else {
450                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
451                                                 match = true;
452                                 }
453                                 if (!match)
454                                         continue;
455                                 match = false;
456                                 if ((mattrs & MethodAttributes.Static) != 0) {
457                                         if ((bindingAttr & BindingFlags.Static) != 0)
458                                                 match = true;
459                                 } else {
460                                         if ((bindingAttr & BindingFlags.Instance) != 0)
461                                                 match = true;
462                                 }
463                                 if (!match)
464                                         continue;
465                                 l.Add (c);
466                         }
467                         MethodInfo[] result = new MethodInfo [l.Count];
468                         l.CopyTo (result);
469                         return result;
470                 }
471
472                 [MonoTODO]
473                 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
474                         // FIXME
475                         throw new NotImplementedException ();
476                 }
477                 
478                 [MonoTODO]
479                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
480                         // FIXME
481                         throw new NotImplementedException ();
482                 }
483
484                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
485                         bool match;
486                         ArrayList result = new ArrayList ();
487                 
488                         if (subtypes == null)
489                                 return Type.EmptyTypes;
490                         foreach (TypeBuilder t in subtypes) {
491                                 match = false;
492                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
493                                         if ((bindingAttr & BindingFlags.Public) != 0)
494                                                 match = true;
495                                 } else {
496                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
497                                                 match = true;
498                                 }
499                                 if (!match)
500                                         continue;
501                                 result.Add (t);
502                         }
503                         Type[] r = new Type [result.Count];
504                         result.CopyTo (r);
505                         return r;
506                 }
507
508                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
509                         if (properties == null)
510                                 return new PropertyInfo [0];
511                         ArrayList l = new ArrayList ();
512                         bool match;
513                         MethodAttributes mattrs;
514                         MethodInfo accessor;
515                         
516                         foreach (PropertyInfo c in properties) {
517                                 match = false;
518                                 accessor = c.GetGetMethod (true);
519                                 if (accessor == null)
520                                         accessor = c.GetSetMethod (true);
521                                 if (accessor == null)
522                                         continue;
523                                 mattrs = accessor.Attributes;
524                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
525                                         if ((bindingAttr & BindingFlags.Public) != 0)
526                                                 match = true;
527                                 } else {
528                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
529                                                 match = true;
530                                 }
531                                 if (!match)
532                                         continue;
533                                 match = false;
534                                 if ((mattrs & MethodAttributes.Static) != 0) {
535                                         if ((bindingAttr & BindingFlags.Static) != 0)
536                                                 match = true;
537                                 } else {
538                                         if ((bindingAttr & BindingFlags.Instance) != 0)
539                                                 match = true;
540                                 }
541                                 if (!match)
542                                         continue;
543                                 l.Add (c);
544                         }
545                         PropertyInfo[] result = new PropertyInfo [l.Count];
546                         l.CopyTo (result);
547                         return result;
548                 }
549                 
550                 [MonoTODO]
551                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
552                         // FIXME
553                         throw new NotImplementedException ();
554                 }
555
556                 protected override bool HasElementTypeImpl () {
557                         return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
558                 }
559
560                 [MonoTODO]
561                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
562                         // FIXME
563                         throw new NotImplementedException ();
564                 }
565
566                 protected override bool IsArrayImpl () {
567                         return type_is_subtype_of (this, typeof (System.Array), false);
568                 }
569                 protected override bool IsByRefImpl () {
570                         // FIXME
571                         return false;
572                 }
573                 protected override bool IsCOMObjectImpl () {
574                         return false;
575                 }
576                 protected override bool IsPointerImpl () {
577                         // FIXME
578                         return false;
579                 }
580                 protected override bool IsPrimitiveImpl () {
581                         // FIXME
582                         return false;
583                 }
584                 protected override bool IsValueTypeImpl () {
585                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
586                                 this != pmodule.assemblyb.corlib_value_type &&
587                                 this != pmodule.assemblyb.corlib_enum_type);
588                 }
589                 
590                 public override RuntimeTypeHandle TypeHandle { get { return _impl; } }
591
592                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
593                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
594                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
595                                 byte[] data = customBuilder.Data;
596                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
597                                 layout_kind = (int)data [2];
598                                 layout_kind |= ((int)data [3]) << 8;
599                                 attrs &= ~TypeAttributes.LayoutMask;
600                                 switch ((LayoutKind)layout_kind) {
601                                 case LayoutKind.Auto:
602                                         attrs |= TypeAttributes.AutoLayout;
603                                         break;
604                                 case LayoutKind.Explicit:
605                                         attrs |= TypeAttributes.ExplicitLayout;
606                                         break;
607                                 case LayoutKind.Sequential:
608                                         attrs |= TypeAttributes.SequentialLayout;
609                                         break;
610                                 default:
611                                         // we should ignore it since it can be any value anyway...
612                                         throw new Exception ("Error in customattr");
613                                 }
614                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
615                                 int pos = 6;
616                                 if (first_type_name == "System.Int16")
617                                         pos = 4;
618                                 int nnamed = (int)data [pos++];
619                                 nnamed |= ((int)data [pos++]) << 8;
620                                 for (int i = 0; i < nnamed; ++i) {
621                                         byte named_type = data [pos++];
622                                         byte type = data [pos++];
623                                         int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
624                                         string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
625                                         pos += len;
626                                         /* all the fields are integers in StructLayout */
627                                         int value = (int)data [pos++];
628                                         value |= ((int)data [pos++]) << 8;
629                                         value |= ((int)data [pos++]) << 16;
630                                         value |= ((int)data [pos++]) << 24;
631                                         switch (named_name) {
632                                         case "CharSet":
633                                                 switch ((CharSet)value) {
634                                                 case CharSet.None:
635                                                 case CharSet.Ansi:
636                                                         break;
637                                                 case CharSet.Unicode:
638                                                         attrs |= TypeAttributes.UnicodeClass;
639                                                         break;
640                                                 case CharSet.Auto:
641                                                         attrs |= TypeAttributes.AutoClass;
642                                                         break;
643                                                 default:
644                                                         break; // error out...
645                                                 }
646                                                 break;
647                                         case "Pack":
648                                                 packing_size = (PackingSize)value;
649                                                 break;
650                                         case "Size":
651                                                 class_size = value;
652                                                 break;
653                                         default:
654                                                 break; // error out...
655                                         }
656                                 }
657                                 return;
658                         } else if (attrname == "System.SerializableAttribute") {
659                                 attrs |= TypeAttributes.Serializable;
660                                 return;
661                         }
662                         if (cattrs != null) {
663                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
664                                 cattrs.CopyTo (new_array, 0);
665                                 new_array [cattrs.Length] = customBuilder;
666                                 cattrs = new_array;
667                         } else {
668                                 cattrs = new CustomAttributeBuilder [1];
669                                 cattrs [0] = customBuilder;
670                         }
671                 }
672                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
673                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
674                 }
675
676                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
677                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
678                         if (events != null) {
679                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
680                                 System.Array.Copy (events, new_events, events.Length);
681                                 new_events [events.Length] = res;
682                                 events = new_events;
683                         } else {
684                                 events = new EventBuilder [1];
685                                 events [0] = res;
686                         }
687                         return res;
688                 }
689
690                 static int InitializedDataCount = 0;
691                 
692                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
693                         TypeBuilder datablobtype = pmodule.DefineType ("$ArrayType$"+InitializedDataCount.ToString(),
694                                 TypeAttributes.Public|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
695                                 pmodule.assemblyb.corlib_value_type, PackingSize.Size1, data.Length);
696                         datablobtype.packing_size = PackingSize.Size1;
697                         datablobtype.class_size = data.Length;
698                         datablobtype.CreateType ();
699                         FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
700                         res.SetRVAData (data);
701                         InitializedDataCount++;
702                         return res;
703                 }
704
705                 [MonoTODO]
706                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
707                         throw new NotImplementedException ();
708                 }
709
710                 public TypeToken TypeToken {
711                         get {
712                                 return new TypeToken (0x02000000 | table_idx);
713                         }
714                 }
715                 public void SetParent (Type parentType) {
716                         parent = parentType;
717                 }
718                 internal int get_next_table_index (object obj, int table, bool inc) {
719                         return pmodule.get_next_table_index (obj, table, inc);
720                 }
721
722                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
723                 {
724                         if (created == null)
725                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
726
727                         return created.GetInterfaceMap (interfaceType);
728                 }
729         }
730 }