2003-05-19 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / TypeBuilder.cs
1 //
2 // System.Reflection.Emit/TypeBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 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         string fullname;
41
42         public const int UnspecifiedTypeSize = 0;
43
44                 protected override TypeAttributes GetAttributeFlagsImpl () {
45                         return attrs;
46                 }
47                 
48                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49                 private extern void setup_internal_class (TypeBuilder tb);
50                 
51                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52                 private extern void create_internal_class (TypeBuilder tb);
53                 
54                 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
55                         this.parent = null;
56                         this.attrs = attr;
57                         this.class_size = -1;
58                         fullname = this.tname = "<Module>";
59                         this.nspace = "";
60                         pmodule = mb;
61                         setup_internal_class (this);
62                 }
63
64                 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
65                         int sep_index;
66                         this.parent = parent;
67                         this.attrs = attr;
68                         this.class_size = type_size;
69                         this.packing_size = packing_size;
70                         this.nesting_type = nesting_type;
71                         sep_index = name.LastIndexOf('.');
72                         if (sep_index != -1) {
73                                 this.tname = name.Substring (sep_index + 1);
74                                 this.nspace = name.Substring (0, sep_index);
75                         } else {
76                                 this.tname = name;
77                                 this.nspace = "";
78                         }
79                         if (interfaces != null) {
80                                 this.interfaces = new Type[interfaces.Length];
81                                 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
82                         }
83                         pmodule = mb;
84                         // skip .<Module> ?
85                         table_idx = mb.get_next_table_index (this, 0x02, true);
86                         setup_internal_class (this);
87                         fullname = GetFullName ();
88                 }
89
90                 public override Assembly Assembly {
91                         get {return pmodule.Assembly;}
92                 }
93
94                 public override string AssemblyQualifiedName {
95                         get {
96                                 return fullname + ", " + Assembly.GetName().FullName;
97                         }
98                 }
99                 public override Type BaseType {
100                         get {
101                                 return parent;
102                         }
103                 }
104                 public override Type DeclaringType {get {return nesting_type;}}
105
106                 [MonoTODO]
107                 public override Type UnderlyingSystemType {
108                         get {
109                                 // This should return the type itself for non-enum types but 
110                                 // that breaks mcs.
111                                 if (fields != null) {
112                                         foreach (FieldBuilder f in fields) {
113                                                 if ((f.Attributes & FieldAttributes.Static) == 0)
114                                                         return f.FieldType;
115                                         }
116                                 }
117                                 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
118                         }
119                 }
120
121                 string GetFullName () {
122                         if (nesting_type != null)
123                                 return String.Concat (nesting_type.FullName, "+", tname);
124                         if ((nspace != null) && (nspace.Length > 0))
125                                 return String.Concat (nspace, ".", tname);
126                         return tname;
127                 }
128         
129                 public override string FullName {
130                         get {
131                                 return fullname;
132                         }
133                 }
134         
135                 public override Guid GUID {
136                         get {
137                             throw not_supported ();
138                         }
139                 }
140
141                 public override Module Module {
142                         get {return pmodule;}
143                 }
144                 public override string Name {
145                         get {return tname;}
146                 }
147                 public override string Namespace {
148                         get {return nspace;}
149                 }
150                 public PackingSize PackingSize {
151                         get {return packing_size;}
152                 }
153                 public int Size {
154                         get { return class_size; }
155                 }
156                 public override Type ReflectedType {get {return nesting_type;}}
157                 public override MemberTypes MemberType { 
158                         get {return MemberTypes.TypeInfo;}
159                 }
160
161                 [MonoTODO]
162                 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
163                         throw new NotImplementedException ();
164                 }
165
166                 public void AddInterfaceImplementation( Type interfaceType) {
167                         if (interfaceType == null)
168                                 throw new ArgumentNullException ("interfaceType");
169                         if (is_created)
170                                 throw not_after_created ();
171
172                         if (interfaces != null) {
173                                 // Check for duplicates
174                                 foreach (Type t in interfaces)
175                                         if (t == interfaceType)
176                                                 return;
177
178                                 Type[] ifnew = new Type [interfaces.Length + 1];
179                                 interfaces.CopyTo (ifnew, 0);
180                                 ifnew [interfaces.Length] = interfaceType;
181                                 interfaces = ifnew;
182                         } else {
183                                 interfaces = new Type [1];
184                                 interfaces [0] = interfaceType;
185                         }
186                 }
187
188                 [MonoTODO]
189                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
190                                                                        CallingConventions callConvention, Type[] types,
191                                                                        ParameterModifier[] modifiers)
192                 {
193                         if (ctors == null)
194                                 return null;
195
196                         ConstructorBuilder found = null;
197                         int count = 0;
198                         
199                         foreach (ConstructorBuilder cb in ctors){
200                                 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
201                                         continue;
202                                 found = cb;
203                                 count++;
204                         }
205
206                         if (count == 0)
207                                 return null;
208                         if (types == null){
209                                 if (count > 1)
210                                         throw new AmbiguousMatchException ();
211                                 return found;
212                         }
213                         MethodBase[] match = new MethodBase [count];
214                         if (count == 1)
215                                 match [0] = found;
216                         else {
217                                 count = 0;
218                                 foreach (ConstructorInfo m in ctors) {
219                                         if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
220                                                 continue;
221                                         match [count++] = m;
222                                 }
223                         }
224                         if (binder == null)
225                                 binder = Binder.DefaultBinder;
226                         return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
227                 }
228
229                 public override bool IsDefined( Type attributeType, bool inherit)
230                 {
231                         throw not_supported ();
232                 }
233                 
234                 public override object[] GetCustomAttributes(bool inherit)
235                 {
236                         throw not_supported ();
237                 }
238                 
239                 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
240                 {
241                         throw not_supported ();
242                 }
243
244                 public TypeBuilder DefineNestedType (string name) {
245                         return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
246                 }
247
248                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
249                         return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
250                 }
251
252                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
253                         return DefineNestedType (name, attr, parent, null);
254                 }
255
256                 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
257                                                       PackingSize packsize, int typesize)
258                 {
259                         check_name ("name", name);
260                         // Visibility must be NestedXXX
261                         /* This breaks mcs
262                         if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
263                                 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
264                                 throw new ArgumentException ("attr", "Bad type flags for nested type.");
265                         */
266                         if (interfaces != null)
267                                 foreach (Type iface in interfaces)
268                                         if (iface == null)
269                                                 throw new ArgumentNullException ("interfaces");
270
271                         TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
272                         res.fullname = res.GetFullName ();
273                         pmodule.RegisterTypeName (res, res.fullname);
274                         if (subtypes != null) {
275                                 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
276                                 System.Array.Copy (subtypes, new_types, subtypes.Length);
277                                 new_types [subtypes.Length] = res;
278                                 subtypes = new_types;
279                         } else {
280                                 subtypes = new TypeBuilder [1];
281                                 subtypes [0] = res;
282                         }
283                         return res;
284                 }
285
286                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
287                         return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
288                 }
289
290                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
291                         return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
292                 }
293
294                 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
295                         return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
296                 }
297
298                 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
299                 {
300                         if (is_created)
301                                 throw not_after_created ();
302                         ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
303                         if (ctors != null) {
304                                 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
305                                 System.Array.Copy (ctors, new_ctors, ctors.Length);
306                                 new_ctors [ctors.Length] = cb;
307                                 ctors = new_ctors;
308                         } else {
309                                 ctors = new ConstructorBuilder [1];
310                                 ctors [0] = cb;
311                         }
312                         return cb;
313                 }
314
315                 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
316                 {
317                         ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, null);
318
319                         ConstructorInfo parent_constructor = parent.GetConstructor (
320                                 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
321                                 null, Type.EmptyTypes, null);
322
323                         ILGenerator ig = cb.GetILGenerator ();
324                         if (parent_constructor != null){
325                                 ig.Emit (OpCodes.Ldarg_0);
326                                 ig.Emit (OpCodes.Call, parent_constructor);
327                         }
328                         ig.Emit (OpCodes.Ret);
329                         return cb;
330                 }
331
332                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
333                         return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
334                 }
335
336                 private void append_method (MethodBuilder mb) {
337                         if (methods != null) {
338                                 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
339                                 System.Array.Copy (methods, new_methods, methods.Length);
340                                 new_methods [methods.Length] = mb;
341                                 methods = new_methods;
342                         } else {
343                                 methods = new MethodBuilder [1];
344                                 methods [0] = mb;
345                         }
346                 }
347
348                 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
349                         check_name ("name", name);
350                         if (is_created)
351                                 throw not_after_created ();
352                         if (IsInterface && (
353                                 !((attributes & MethodAttributes.Abstract) != 0) || 
354                                 !((attributes & MethodAttributes.Virtual) != 0)))
355                                 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
356
357                         if (returnType == null)
358                                 returnType = pmodule.assemblyb.corlib_void_type;
359                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
360                         append_method (res);
361                         return res;
362                 }
363
364                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
365                         check_name ("name", name);
366                         check_name ("dllName", dllName);
367                         check_name ("entryName", entryName);
368                         if ((attributes & MethodAttributes.Abstract) != 0)
369                                 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
370                         if (IsInterface)
371                                 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");            
372                         if (is_created)
373                                 throw not_after_created ();
374
375                         MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
376                                 dllName, entryName, nativeCallConv, nativeCharSet);
377                         append_method (res);
378                         return res;
379                 }
380
381                 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
382                         return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
383                                 nativeCallConv, nativeCharSet);
384                 }
385
386                 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
387                         if (methodInfoBody == null)
388                                 throw new ArgumentNullException ("methodInfoBody");
389                         if (methodInfoDeclaration == null)
390                                 throw new ArgumentNullException ("methodInfoDeclaration");
391                         if (is_created)
392                                 throw not_after_created ();
393
394                         if (methodInfoBody is MethodBuilder) {
395                                 MethodBuilder mb = (MethodBuilder)methodInfoBody;
396                                 mb.set_override (methodInfoDeclaration);
397                         }
398                 }
399
400                 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
401                         check_name ("fieldName", fieldName);
402                         if (type == typeof (void))
403                                 throw new ArgumentException ("type",  "Bad field type in defining field.");
404                         if (is_created)
405                                 throw not_after_created ();
406
407                         FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
408                         if (fields != null) {
409                                 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
410                                 System.Array.Copy (fields, new_fields, fields.Length);
411                                 new_fields [fields.Length] = res;
412                                 fields = new_fields;
413                         } else {
414                                 fields = new FieldBuilder [1];
415                                 fields [0] = res;
416                                 create_internal_class (this);
417                         }
418                         return res;
419                 }
420
421                 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
422                         check_name ("name", name);
423                         if (parameterTypes != null)
424                                 foreach (Type param in parameterTypes)
425                                         if (param == null)
426                                                 throw new ArgumentNullException ("parameterTypes");
427                         if (is_created)
428                                 throw not_after_created ();
429
430                         PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
431
432                         if (properties != null) {
433                                 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
434                                 System.Array.Copy (properties, new_properties, properties.Length);
435                                 new_properties [properties.Length] = res;
436                                 properties = new_properties;
437                         } else {
438                                 properties = new PropertyBuilder [1];
439                                 properties [0] = res;
440                         }
441                         return res;
442                 }
443
444                 [MonoTODO]
445                 public ConstructorBuilder DefineTypeInitializer() {
446                         if (is_created)
447                                 throw not_after_created ();
448
449                         throw new NotImplementedException ();
450                 }
451
452                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
453                 private extern Type create_runtime_class (TypeBuilder tb);
454                 
455                 public Type CreateType() {
456                         /* handle nesting_type */
457                         if (is_created)
458                                 throw not_after_created ();
459                         if (methods != null) {
460                                 foreach (MethodBuilder method in methods) {
461                                         method.fixup ();
462                                 }
463                         }
464
465                         //
466                         // On classes, define a default constructor if not provided
467                         //
468                         if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
469                                 DefineDefaultConstructor (MethodAttributes.Public);
470
471                         if (ctors != null){
472                                 foreach (ConstructorBuilder ctor in ctors) 
473                                         ctor.fixup ();
474                         }
475                         
476                         created = create_runtime_class (this);
477                         if (created != null)
478                                 return created;
479                         return this;
480                 }
481
482                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
483                 {
484                         if (ctors == null)
485                                 return new ConstructorInfo [0];
486                         ArrayList l = new ArrayList ();
487                         bool match;
488                         MethodAttributes mattrs;
489                         
490                         foreach (ConstructorBuilder c in ctors) {
491                                 match = false;
492                                 mattrs = c.Attributes;
493                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
494                                         if ((bindingAttr & BindingFlags.Public) != 0)
495                                                 match = true;
496                                 } else {
497                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
498                                                 match = true;
499                                 }
500                                 if (!match)
501                                         continue;
502                                 match = false;
503                                 if ((mattrs & MethodAttributes.Static) != 0) {
504                                         if ((bindingAttr & BindingFlags.Static) != 0)
505                                                 match = true;
506                                 } else {
507                                         if ((bindingAttr & BindingFlags.Instance) != 0)
508                                                 match = true;
509                                 }
510                                 if (!match)
511                                         continue;
512                                 l.Add (c);
513                         }
514                         ConstructorInfo[] result = new ConstructorInfo [l.Count];
515                         l.CopyTo (result);
516                         return result;
517                 }
518
519                 public override Type GetElementType () { 
520                         throw not_supported ();
521                 }
522
523                 [MonoTODO]
524                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
525                         if (name == null)
526                                 throw new ArgumentNullException ("name");
527
528                         throw new NotImplementedException ();
529                 }
530
531                 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
532                         return new EventInfo [0];
533                 }
534
535                 [MonoTODO]
536                 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
537                         if (name == null)
538                                 throw new ArgumentNullException ("name");
539
540                         throw new NotImplementedException ();
541                 }
542
543                 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
544                         if (fields == null)
545                                 return new FieldInfo [0];
546                         ArrayList l = new ArrayList ();
547                         bool match;
548                         FieldAttributes mattrs;
549                         
550                         foreach (FieldInfo c in fields) {
551                                 match = false;
552                                 mattrs = c.Attributes;
553                                 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
554                                         if ((bindingAttr & BindingFlags.Public) != 0)
555                                                 match = true;
556                                 } else {
557                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
558                                                 match = true;
559                                 }
560                                 if (!match)
561                                         continue;
562                                 match = false;
563                                 if ((mattrs & FieldAttributes.Static) != 0) {
564                                         if ((bindingAttr & BindingFlags.Static) != 0)
565                                                 match = true;
566                                 } else {
567                                         if ((bindingAttr & BindingFlags.Instance) != 0)
568                                                 match = true;
569                                 }
570                                 if (!match)
571                                         continue;
572                                 l.Add (c);
573                         }
574                         FieldInfo[] result = new FieldInfo [l.Count];
575                         l.CopyTo (result);
576                         return result;
577                 }
578
579                 [MonoTODO]
580                 public override Type GetInterface (string name, bool ignoreCase) {
581                         if (name == null)
582                                 throw new ArgumentNullException ("name");
583
584                         throw new NotImplementedException ();
585                 }
586                 
587                 public override Type[] GetInterfaces () {
588                         if (interfaces != null) {
589                                 Type[] ret = new Type [interfaces.Length];
590                                 interfaces.CopyTo (ret, 0);
591                                 return ret;
592                         } else {
593                                 return Type.EmptyTypes;
594                         }
595                 }
596
597                 [MonoTODO]
598                 public override MemberInfo[] GetMembers( BindingFlags bindingAttr) {
599                         // FIXME
600                         throw new NotImplementedException ();
601                 }
602
603                 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
604                         if (methods == null)
605                                 return new MethodInfo [0];
606                         ArrayList l = new ArrayList ();
607                         bool match;
608                         MethodAttributes mattrs;
609
610                         foreach (MethodInfo c in methods) {
611                                 match = false;
612                                 mattrs = c.Attributes;
613                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
614                                         if ((bindingAttr & BindingFlags.Public) != 0)
615                                                 match = true;
616                                 } else {
617                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
618                                                 match = true;
619                                 }
620                                 if (!match)
621                                         continue;
622                                 match = false;
623                                 if ((mattrs & MethodAttributes.Static) != 0) {
624                                         if ((bindingAttr & BindingFlags.Static) != 0)
625                                                 match = true;
626                                 } else {
627                                         if ((bindingAttr & BindingFlags.Instance) != 0)
628                                                 match = true;
629                                 }
630                                 if (!match)
631                                         continue;
632                                 l.Add (c);
633                         }
634                         MethodInfo[] result = new MethodInfo [l.Count];
635                         l.CopyTo (result);
636                         return result;
637                 }
638
639                 [MonoTODO]
640                 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
641                         // FIXME
642                         throw new NotImplementedException ();
643                 }
644                 
645                 [MonoTODO]
646                 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
647                         if (name == null)
648                                 throw new ArgumentNullException ("name");
649
650                         throw new NotImplementedException ();
651                 }
652
653                 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
654                         bool match;
655                         ArrayList result = new ArrayList ();
656                 
657                         if (subtypes == null)
658                                 return Type.EmptyTypes;
659                         foreach (TypeBuilder t in subtypes) {
660                                 match = false;
661                                 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
662                                         if ((bindingAttr & BindingFlags.Public) != 0)
663                                                 match = true;
664                                 } else {
665                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
666                                                 match = true;
667                                 }
668                                 if (!match)
669                                         continue;
670                                 result.Add (t);
671                         }
672                         Type[] r = new Type [result.Count];
673                         result.CopyTo (r);
674                         return r;
675                 }
676
677                 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
678                         if (properties == null)
679                                 return new PropertyInfo [0];
680                         ArrayList l = new ArrayList ();
681                         bool match;
682                         MethodAttributes mattrs;
683                         MethodInfo accessor;
684                         
685                         foreach (PropertyInfo c in properties) {
686                                 match = false;
687                                 accessor = c.GetGetMethod (true);
688                                 if (accessor == null)
689                                         accessor = c.GetSetMethod (true);
690                                 if (accessor == null)
691                                         continue;
692                                 mattrs = accessor.Attributes;
693                                 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
694                                         if ((bindingAttr & BindingFlags.Public) != 0)
695                                                 match = true;
696                                 } else {
697                                         if ((bindingAttr & BindingFlags.NonPublic) != 0)
698                                                 match = true;
699                                 }
700                                 if (!match)
701                                         continue;
702                                 match = false;
703                                 if ((mattrs & MethodAttributes.Static) != 0) {
704                                         if ((bindingAttr & BindingFlags.Static) != 0)
705                                                 match = true;
706                                 } else {
707                                         if ((bindingAttr & BindingFlags.Instance) != 0)
708                                                 match = true;
709                                 }
710                                 if (!match)
711                                         continue;
712                                 l.Add (c);
713                         }
714                         PropertyInfo[] result = new PropertyInfo [l.Count];
715                         l.CopyTo (result);
716                         return result;
717                 }
718                 
719                 [MonoTODO]
720                 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
721                         // FIXME
722                         throw new NotImplementedException ();
723                 }
724
725                 protected override bool HasElementTypeImpl () {
726                         // According to the MSDN docs, this is supported for TypeBuilders,
727                         // but in reality, it is not
728                         throw not_supported ();
729                         //                      return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
730                 }
731
732                 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
733                         throw not_supported ();
734                 }
735
736                 protected override bool IsArrayImpl () {
737                         return type_is_subtype_of (this, typeof (System.Array), false);
738                 }
739                 protected override bool IsByRefImpl () {
740                         // FIXME
741                         return false;
742                 }
743                 protected override bool IsCOMObjectImpl () {
744                         return false;
745                 }
746                 protected override bool IsPointerImpl () {
747                         // FIXME
748                         return false;
749                 }
750                 protected override bool IsPrimitiveImpl () {
751                         // FIXME
752                         return false;
753                 }
754                 protected override bool IsValueTypeImpl () {
755                         return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
756                                 this != pmodule.assemblyb.corlib_value_type &&
757                                 this != pmodule.assemblyb.corlib_enum_type);
758                 }
759                 
760                 public override RuntimeTypeHandle TypeHandle { 
761                         get { 
762                                 throw not_supported (); 
763                         } 
764                 }
765
766                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
767                         if (customBuilder == null)
768                                 throw new ArgumentNullException ("customBuilder");
769
770                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
771                         if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
772                                 byte[] data = customBuilder.Data;
773                                 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
774                                 layout_kind = (int)data [2];
775                                 layout_kind |= ((int)data [3]) << 8;
776                                 attrs &= ~TypeAttributes.LayoutMask;
777                                 switch ((LayoutKind)layout_kind) {
778                                 case LayoutKind.Auto:
779                                         attrs |= TypeAttributes.AutoLayout;
780                                         break;
781                                 case LayoutKind.Explicit:
782                                         attrs |= TypeAttributes.ExplicitLayout;
783                                         break;
784                                 case LayoutKind.Sequential:
785                                         attrs |= TypeAttributes.SequentialLayout;
786                                         break;
787                                 default:
788                                         // we should ignore it since it can be any value anyway...
789                                         throw new Exception ("Error in customattr");
790                                 }
791                                 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
792                                 int pos = 6;
793                                 if (first_type_name == "System.Int16")
794                                         pos = 4;
795                                 int nnamed = (int)data [pos++];
796                                 nnamed |= ((int)data [pos++]) << 8;
797                                 for (int i = 0; i < nnamed; ++i) {
798                                         byte named_type = data [pos++];
799                                         byte type = data [pos++];
800                                         int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
801                                         string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
802                                         pos += len;
803                                         /* all the fields are integers in StructLayout */
804                                         int value = (int)data [pos++];
805                                         value |= ((int)data [pos++]) << 8;
806                                         value |= ((int)data [pos++]) << 16;
807                                         value |= ((int)data [pos++]) << 24;
808                                         switch (named_name) {
809                                         case "CharSet":
810                                                 switch ((CharSet)value) {
811                                                 case CharSet.None:
812                                                 case CharSet.Ansi:
813                                                         break;
814                                                 case CharSet.Unicode:
815                                                         attrs |= TypeAttributes.UnicodeClass;
816                                                         break;
817                                                 case CharSet.Auto:
818                                                         attrs |= TypeAttributes.AutoClass;
819                                                         break;
820                                                 default:
821                                                         break; // error out...
822                                                 }
823                                                 break;
824                                         case "Pack":
825                                                 packing_size = (PackingSize)value;
826                                                 break;
827                                         case "Size":
828                                                 class_size = value;
829                                                 break;
830                                         default:
831                                                 break; // error out...
832                                         }
833                                 }
834                                 return;
835                         } else if (attrname == "System.SerializableAttribute") {
836                                 attrs |= TypeAttributes.Serializable;
837                                 return;
838                         }
839                         if (cattrs != null) {
840                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
841                                 cattrs.CopyTo (new_array, 0);
842                                 new_array [cattrs.Length] = customBuilder;
843                                 cattrs = new_array;
844                         } else {
845                                 cattrs = new CustomAttributeBuilder [1];
846                                 cattrs [0] = customBuilder;
847                         }
848                 }
849                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
850                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
851                 }
852
853                 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
854                         check_name ("name", name);
855                         if (eventtype == null)
856                                 throw new ArgumentNullException ("eventtype");
857                         if (is_created)
858                                 throw not_after_created ();
859
860                         EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
861                         if (events != null) {
862                                 EventBuilder[] new_events = new EventBuilder [events.Length+1];
863                                 System.Array.Copy (events, new_events, events.Length);
864                                 new_events [events.Length] = res;
865                                 events = new_events;
866                         } else {
867                                 events = new EventBuilder [1];
868                                 events [0] = res;
869                         }
870                         return res;
871                 }
872
873                 static int InitializedDataCount = 0;
874                 
875                 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
876                         check_name ("name", name);
877                         if (data == null)
878                                 throw new ArgumentNullException ("data");
879                         if ((data.Length == 0) || (data.Length > 0x3f0000))
880                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
881                         if (is_created)
882                                 throw not_after_created ();
883
884                         TypeBuilder datablobtype = DefineNestedType ("$ArrayType$"+InitializedDataCount.ToString(),
885                                 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
886                                 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
887                         datablobtype.CreateType ();
888                         FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
889                         res.SetRVAData (data);
890                         InitializedDataCount++;
891                         return res;
892                 }
893
894                 [MonoTODO]
895                 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
896                         check_name ("name", name);
897                         if ((size <= 0) || (size > 0x3f0000))
898                                 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
899                         if (is_created)
900                                 throw not_after_created ();
901
902                         throw new NotImplementedException ();
903                 }
904
905                 public TypeToken TypeToken {
906                         get {
907                                 return new TypeToken (0x02000000 | table_idx);
908                         }
909                 }
910                 public void SetParent (Type parentType) {
911                         if (parentType == null)
912                                 throw new ArgumentNullException ("parentType");
913                         if (is_created)
914                                 throw not_after_created ();
915
916                         parent = parentType;
917                 }
918                 internal int get_next_table_index (object obj, int table, bool inc) {
919                         return pmodule.get_next_table_index (obj, table, inc);
920                 }
921
922                 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
923                 {
924                         if (created == null)
925                                 throw new NotSupportedException ("This method is not implemented for incomplete types.");
926
927                         return created.GetInterfaceMap (interfaceType);
928                 }
929
930                 internal bool is_created {
931                         get {
932                                 return created != null;
933                         }
934                 }
935
936                 private Exception not_supported ()
937                 {
938                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
939                 }
940
941                 private Exception not_after_created ()
942                 {
943                         return new InvalidOperationException ("Unable to change after type has been created.");
944                 }
945
946                 private void check_name (string argName, string name)
947                 {
948                         if (name == null)
949                                 throw new ArgumentNullException (argName);
950                         if (name == "")
951                                 throw new ArgumentException (argName, "Empty name is not legal.");
952                         if (name.IndexOf ((char)0) != -1)
953                                 throw new ArgumentException (argName, "Illegal name.");
954                 }
955         }
956 }