2 // System.Reflection.Emit/TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
16 using System.Globalization;
17 using System.Collections;
18 using System.Security;
19 using System.Security.Permissions;
21 namespace System.Reflection.Emit {
23 public sealed class TypeBuilder : Type {
25 private string nspace;
27 private Type nesting_type;
28 private Type[] interfaces;
29 private int num_methods;
30 private MethodBuilder[] methods;
31 private ConstructorBuilder[] ctors;
32 private PropertyBuilder[] properties;
33 private int num_fields;
34 private FieldBuilder[] fields;
35 private EventBuilder[] events;
36 private CustomAttributeBuilder[] cattrs;
37 internal TypeBuilder[] subtypes;
38 private TypeAttributes attrs;
39 private int table_idx;
40 private ModuleBuilder pmodule;
41 private int class_size;
42 private PackingSize packing_size;
43 private MonoGenericParam[] generic_params;
47 public const int UnspecifiedTypeSize = 0;
49 protected override TypeAttributes GetAttributeFlagsImpl () {
53 [MethodImplAttribute(MethodImplOptions.InternalCall)]
54 private extern void setup_internal_class (TypeBuilder tb);
56 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57 private extern void create_internal_class (TypeBuilder tb);
59 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60 private extern void setup_generic_class (TypeBuilder tb);
62 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
66 fullname = this.tname = "<Module>";
69 setup_internal_class (this);
72 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
76 this.class_size = type_size;
77 this.packing_size = packing_size;
78 this.nesting_type = nesting_type;
79 sep_index = name.LastIndexOf('.');
80 if (sep_index != -1) {
81 this.tname = name.Substring (sep_index + 1);
82 this.nspace = name.Substring (0, sep_index);
87 if (interfaces != null) {
88 this.interfaces = new Type[interfaces.Length];
89 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
93 table_idx = mb.get_next_table_index (this, 0x02, true);
94 setup_internal_class (this);
95 fullname = GetFullName ();
98 public override Assembly Assembly {
99 get {return pmodule.Assembly;}
102 public override string AssemblyQualifiedName {
104 return fullname + ", " + Assembly.GetName().FullName;
107 public override Type BaseType {
112 public override Type DeclaringType {get {return nesting_type;}}
114 /* public override bool IsSubclassOf (Type c)
131 public override Type UnderlyingSystemType {
133 // This should return the type itself for non-enum types but
135 if (fields != null) {
136 foreach (FieldBuilder f in fields) {
137 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
141 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
145 string GetFullName () {
146 if (nesting_type != null)
147 return String.Concat (nesting_type.FullName, "+", tname);
148 if ((nspace != null) && (nspace.Length > 0))
149 return String.Concat (nspace, ".", tname);
153 public override string FullName {
159 public override Guid GUID {
161 throw not_supported ();
165 public override Module Module {
166 get {return pmodule;}
168 public override string Name {
171 public override string Namespace {
174 public PackingSize PackingSize {
175 get {return packing_size;}
178 get { return class_size; }
180 public override Type ReflectedType {get {return nesting_type;}}
183 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
184 throw new NotImplementedException ();
187 public void AddInterfaceImplementation( Type interfaceType) {
188 if (interfaceType == null)
189 throw new ArgumentNullException ("interfaceType");
191 throw not_after_created ();
193 if (interfaces != null) {
194 // Check for duplicates
195 foreach (Type t in interfaces)
196 if (t == interfaceType)
199 Type[] ifnew = new Type [interfaces.Length + 1];
200 interfaces.CopyTo (ifnew, 0);
201 ifnew [interfaces.Length] = interfaceType;
204 interfaces = new Type [1];
205 interfaces [0] = interfaceType;
210 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
211 CallingConventions callConvention, Type[] types,
212 ParameterModifier[] modifiers)
217 ConstructorBuilder found = null;
220 foreach (ConstructorBuilder cb in ctors){
221 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
231 throw new AmbiguousMatchException ();
234 MethodBase[] match = new MethodBase [count];
239 foreach (ConstructorInfo m in ctors) {
240 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
246 binder = Binder.DefaultBinder;
247 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
250 public override bool IsDefined( Type attributeType, bool inherit)
253 * MS throws NotSupported here, but we can't because some corlib
254 * classes make calls to IsDefined.
256 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
259 public override object[] GetCustomAttributes(bool inherit)
261 throw not_supported ();
264 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
266 throw not_supported ();
269 public TypeBuilder DefineNestedType (string name) {
270 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
273 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
274 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
277 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
278 return DefineNestedType (name, attr, parent, null);
281 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
282 PackingSize packsize, int typesize)
284 check_name ("name", name);
285 // Visibility must be NestedXXX
287 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
288 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
289 throw new ArgumentException ("attr", "Bad type flags for nested type.");
291 if (interfaces != null)
292 foreach (Type iface in interfaces)
294 throw new ArgumentNullException ("interfaces");
296 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
297 res.fullname = res.GetFullName ();
298 pmodule.RegisterTypeName (res, res.fullname);
299 if (subtypes != null) {
300 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
301 System.Array.Copy (subtypes, new_types, subtypes.Length);
302 new_types [subtypes.Length] = res;
303 subtypes = new_types;
305 subtypes = new TypeBuilder [1];
311 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
312 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
315 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
316 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
319 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
320 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
323 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
324 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
332 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
335 throw not_after_created ();
336 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
338 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
339 System.Array.Copy (ctors, new_ctors, ctors.Length);
340 new_ctors [ctors.Length] = cb;
343 ctors = new ConstructorBuilder [1];
349 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
351 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
356 parent_type = parent;
358 parent_type = pmodule.assemblyb.corlib_object_type;
360 ConstructorInfo parent_constructor =
361 parent_type.GetConstructor (
362 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
363 null, Type.EmptyTypes, null);
365 ILGenerator ig = cb.GetILGenerator ();
366 if (parent_constructor != null){
367 ig.Emit (OpCodes.Ldarg_0);
368 ig.Emit (OpCodes.Call, parent_constructor);
370 ig.Emit (OpCodes.Ret);
374 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
375 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
378 private void append_method (MethodBuilder mb) {
379 if (methods != null) {
380 if (methods.Length == num_methods) {
381 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
382 System.Array.Copy (methods, new_methods, num_methods);
383 methods = new_methods;
386 methods = new MethodBuilder [1];
388 methods [num_methods] = mb;
392 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
393 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
401 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
402 check_name ("name", name);
404 throw not_after_created ();
406 !((attributes & MethodAttributes.Abstract) != 0) ||
407 !((attributes & MethodAttributes.Virtual) != 0)))
408 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
410 if (returnType == null)
411 returnType = pmodule.assemblyb.corlib_void_type;
412 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
417 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
418 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
426 MethodBuilder DefinePInvokeMethod (
429 string entryName, MethodAttributes attributes,
430 CallingConventions callingConvention,
432 Type[] returnTypeRequiredCustomModifiers,
433 Type[] returnTypeOptionalCustomModifiers,
434 Type[] parameterTypes,
435 Type[][] parameterTypeRequiredCustomModifiers,
436 Type[][] parameterTypeOptionalCustomModifiers,
437 CallingConvention nativeCallConv,
438 CharSet nativeCharSet) {
439 check_name ("name", name);
440 check_name ("dllName", dllName);
441 check_name ("entryName", entryName);
442 if ((attributes & MethodAttributes.Abstract) != 0)
443 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
445 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
447 throw not_after_created ();
450 = new MethodBuilder (
456 returnTypeRequiredCustomModifiers,
457 returnTypeOptionalCustomModifiers,
459 parameterTypeRequiredCustomModifiers,
460 parameterTypeOptionalCustomModifiers,
469 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
470 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
471 nativeCallConv, nativeCharSet);
474 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
475 if (methodInfoBody == null)
476 throw new ArgumentNullException ("methodInfoBody");
477 if (methodInfoDeclaration == null)
478 throw new ArgumentNullException ("methodInfoDeclaration");
480 throw not_after_created ();
482 if (methodInfoBody is MethodBuilder) {
483 MethodBuilder mb = (MethodBuilder)methodInfoBody;
484 mb.set_override (methodInfoDeclaration);
488 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
489 return DefineField (fieldName, type, null, null, attributes);
497 FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
498 check_name ("fieldName", fieldName);
499 if (type == typeof (void))
500 throw new ArgumentException ("type", "Bad field type in defining field.");
502 throw not_after_created ();
504 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
505 if (fields != null) {
506 if (fields.Length == num_fields) {
507 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
508 System.Array.Copy (fields, new_fields, num_fields);
511 fields [num_fields] = res;
514 fields = new FieldBuilder [1];
517 create_internal_class (this);
522 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
523 check_name ("name", name);
524 if (parameterTypes != null)
525 foreach (Type param in parameterTypes)
527 throw new ArgumentNullException ("parameterTypes");
529 throw not_after_created ();
531 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
533 if (properties != null) {
534 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
535 System.Array.Copy (properties, new_properties, properties.Length);
536 new_properties [properties.Length] = res;
537 properties = new_properties;
539 properties = new PropertyBuilder [1];
540 properties [0] = res;
545 public ConstructorBuilder DefineTypeInitializer() {
546 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
549 [MethodImplAttribute(MethodImplOptions.InternalCall)]
550 private extern Type create_runtime_class (TypeBuilder tb);
552 private bool is_nested_in (Type t) {
562 public Type CreateType() {
563 /* handle nesting_type */
565 throw not_after_created ();
567 // Fire TypeResolve events for fields whose type is an unfinished
569 if (fields != null) {
570 foreach (FieldBuilder fb in fields) {
573 Type ft = fb.FieldType;
574 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
575 TypeBuilder tb = (TypeBuilder)ft;
576 if (!tb.is_created) {
577 AppDomain.CurrentDomain.DoTypeResolve (tb);
578 if (!tb.is_created) {
579 // FIXME: We should throw an exception here,
580 // but mcs expects that the type is created
581 // even if the exception is thrown
582 //throw new TypeLoadException ("Could not load type " + tb);
589 if (methods != null) {
590 for (int i = 0; i < num_methods; ++i)
591 ((MethodBuilder)(methods[i])).fixup ();
595 // On classes, define a default constructor if not provided
597 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
598 DefineDefaultConstructor (MethodAttributes.Public);
601 foreach (ConstructorBuilder ctor in ctors)
605 created = create_runtime_class (this);
611 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
614 return new ConstructorInfo [0];
615 ArrayList l = new ArrayList ();
617 MethodAttributes mattrs;
619 foreach (ConstructorBuilder c in ctors) {
621 mattrs = c.Attributes;
622 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
623 if ((bindingAttr & BindingFlags.Public) != 0)
626 if ((bindingAttr & BindingFlags.NonPublic) != 0)
632 if ((mattrs & MethodAttributes.Static) != 0) {
633 if ((bindingAttr & BindingFlags.Static) != 0)
636 if ((bindingAttr & BindingFlags.Instance) != 0)
643 ConstructorInfo[] result = new ConstructorInfo [l.Count];
648 public override Type GetElementType () {
649 throw not_supported ();
652 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
653 throw not_supported ();
656 /* Needed to keep signature compatibility with MS.NET */
657 public override EventInfo[] GetEvents ()
659 return GetEvents (DefaultBindingFlags);
662 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
663 // FIXME: Under MS.NET, this throws a NotImplementedException
664 // But mcs calls this method. How can that be?
665 return new EventInfo [0];
668 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
673 FieldAttributes mattrs;
675 foreach (FieldInfo c in fields) {
681 mattrs = c.Attributes;
682 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
683 if ((bindingAttr & BindingFlags.Public) != 0)
686 if ((bindingAttr & BindingFlags.NonPublic) != 0)
692 if ((mattrs & FieldAttributes.Static) != 0) {
693 if ((bindingAttr & BindingFlags.Static) != 0)
696 if ((bindingAttr & BindingFlags.Instance) != 0)
706 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
708 return new FieldInfo [0];
709 ArrayList l = new ArrayList ();
711 FieldAttributes mattrs;
713 foreach (FieldInfo c in fields) {
717 mattrs = c.Attributes;
718 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
719 if ((bindingAttr & BindingFlags.Public) != 0)
722 if ((bindingAttr & BindingFlags.NonPublic) != 0)
728 if ((mattrs & FieldAttributes.Static) != 0) {
729 if ((bindingAttr & BindingFlags.Static) != 0)
732 if ((bindingAttr & BindingFlags.Instance) != 0)
739 FieldInfo[] result = new FieldInfo [l.Count];
744 public override Type GetInterface (string name, bool ignoreCase) {
745 throw not_supported ();
748 public override Type[] GetInterfaces () {
749 if (interfaces != null) {
750 Type[] ret = new Type [interfaces.Length];
751 interfaces.CopyTo (ret, 0);
754 return Type.EmptyTypes;
758 public override MemberInfo[] GetMember (string name, MemberTypes type,
759 BindingFlags bindingAttr) {
760 throw not_supported ();
763 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
764 throw not_supported ();
767 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
769 return new MethodInfo [0];
770 ArrayList l = new ArrayList ();
772 MethodAttributes mattrs;
774 foreach (MethodInfo c in methods) {
778 mattrs = c.Attributes;
779 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
780 if ((bindingAttr & BindingFlags.Public) != 0)
783 if ((bindingAttr & BindingFlags.NonPublic) != 0)
789 if ((mattrs & MethodAttributes.Static) != 0) {
790 if ((bindingAttr & BindingFlags.Static) != 0)
793 if ((bindingAttr & BindingFlags.Instance) != 0)
800 MethodInfo[] result = new MethodInfo [l.Count];
805 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
806 throw not_supported ();
809 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
810 throw not_supported ();
813 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
815 ArrayList result = new ArrayList ();
817 if (subtypes == null)
818 return Type.EmptyTypes;
819 foreach (TypeBuilder t in subtypes) {
821 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
822 if ((bindingAttr & BindingFlags.Public) != 0)
825 if ((bindingAttr & BindingFlags.NonPublic) != 0)
832 Type[] r = new Type [result.Count];
837 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
838 if (properties == null)
839 return new PropertyInfo [0];
840 ArrayList l = new ArrayList ();
842 MethodAttributes mattrs;
845 foreach (PropertyInfo c in properties) {
847 accessor = c.GetGetMethod (true);
848 if (accessor == null)
849 accessor = c.GetSetMethod (true);
850 if (accessor == null)
852 mattrs = accessor.Attributes;
853 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
854 if ((bindingAttr & BindingFlags.Public) != 0)
857 if ((bindingAttr & BindingFlags.NonPublic) != 0)
863 if ((mattrs & MethodAttributes.Static) != 0) {
864 if ((bindingAttr & BindingFlags.Static) != 0)
867 if ((bindingAttr & BindingFlags.Instance) != 0)
874 PropertyInfo[] result = new PropertyInfo [l.Count];
879 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
880 throw not_supported ();
883 protected override bool HasElementTypeImpl () {
884 // According to the MSDN docs, this is supported for TypeBuilders,
885 // but in reality, it is not
886 throw not_supported ();
887 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
890 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
891 throw not_supported ();
894 protected override bool IsArrayImpl ()
896 return Type.IsArrayImpl (this);
899 protected override bool IsByRefImpl () {
903 protected override bool IsCOMObjectImpl () {
906 protected override bool IsPointerImpl () {
910 protected override bool IsPrimitiveImpl () {
914 protected override bool IsValueTypeImpl () {
915 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
916 this != pmodule.assemblyb.corlib_value_type &&
917 this != pmodule.assemblyb.corlib_enum_type);
920 public override RuntimeTypeHandle TypeHandle {
922 throw not_supported ();
926 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
927 if (customBuilder == null)
928 throw new ArgumentNullException ("customBuilder");
930 string attrname = customBuilder.Ctor.ReflectedType.FullName;
931 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
932 byte[] data = customBuilder.Data;
933 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
934 layout_kind = (int)data [2];
935 layout_kind |= ((int)data [3]) << 8;
936 attrs &= ~TypeAttributes.LayoutMask;
937 switch ((LayoutKind)layout_kind) {
938 case LayoutKind.Auto:
939 attrs |= TypeAttributes.AutoLayout;
941 case LayoutKind.Explicit:
942 attrs |= TypeAttributes.ExplicitLayout;
944 case LayoutKind.Sequential:
945 attrs |= TypeAttributes.SequentialLayout;
948 // we should ignore it since it can be any value anyway...
949 throw new Exception ("Error in customattr");
951 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
953 if (first_type_name == "System.Int16")
955 int nnamed = (int)data [pos++];
956 nnamed |= ((int)data [pos++]) << 8;
957 for (int i = 0; i < nnamed; ++i) {
958 byte named_type = data [pos++];
959 byte type = data [pos++];
960 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
961 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
963 /* all the fields are integers in StructLayout */
964 int value = (int)data [pos++];
965 value |= ((int)data [pos++]) << 8;
966 value |= ((int)data [pos++]) << 16;
967 value |= ((int)data [pos++]) << 24;
968 switch (named_name) {
970 switch ((CharSet)value) {
974 case CharSet.Unicode:
975 attrs |= TypeAttributes.UnicodeClass;
978 attrs |= TypeAttributes.AutoClass;
981 break; // error out...
985 packing_size = (PackingSize)value;
991 break; // error out...
995 } else if (attrname == "System.SerializableAttribute") {
996 attrs |= TypeAttributes.Serializable;
999 if (cattrs != null) {
1000 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1001 cattrs.CopyTo (new_array, 0);
1002 new_array [cattrs.Length] = customBuilder;
1005 cattrs = new CustomAttributeBuilder [1];
1006 cattrs [0] = customBuilder;
1009 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1010 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1013 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1014 check_name ("name", name);
1015 if (eventtype == null)
1016 throw new ArgumentNullException ("eventtype");
1018 throw not_after_created ();
1020 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1021 if (events != null) {
1022 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1023 System.Array.Copy (events, new_events, events.Length);
1024 new_events [events.Length] = res;
1025 events = new_events;
1027 events = new EventBuilder [1];
1033 static int InitializedDataCount = 0;
1035 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1036 check_name ("name", name);
1038 throw new ArgumentNullException ("data");
1039 if ((data.Length == 0) || (data.Length > 0x3f0000))
1040 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1042 throw not_after_created ();
1044 string s = "$ArrayType$"+InitializedDataCount.ToString();
1045 TypeBuilder datablobtype = DefineNestedType (s,
1046 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1047 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
1048 datablobtype.CreateType ();
1049 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1050 res.SetRVAData (data);
1051 InitializedDataCount++;
1056 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1057 check_name ("name", name);
1058 if ((size <= 0) || (size > 0x3f0000))
1059 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1061 throw not_after_created ();
1063 throw new NotImplementedException ();
1066 public TypeToken TypeToken {
1068 return new TypeToken (0x02000000 | table_idx);
1071 public void SetParent (Type parentType) {
1072 if (parentType == null)
1073 throw new ArgumentNullException ("parentType");
1075 throw not_after_created ();
1077 parent = parentType;
1079 internal int get_next_table_index (object obj, int table, bool inc) {
1080 return pmodule.get_next_table_index (obj, table, inc);
1083 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1085 if (created == null)
1086 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1088 return created.GetInterfaceMap (interfaceType);
1091 internal bool is_created {
1093 return created != null;
1097 private Exception not_supported ()
1099 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1102 private Exception not_after_created ()
1104 return new InvalidOperationException ("Unable to change after type has been created.");
1107 private void check_name (string argName, string name)
1110 throw new ArgumentNullException (argName);
1112 throw new ArgumentException (argName, "Empty name is not legal.");
1113 if (name.IndexOf ((char)0) != -1)
1114 throw new ArgumentException (argName, "Illegal name.");
1118 public override Type GetGenericTypeDefinition ()
1120 setup_generic_class (this);
1122 return base.GetGenericTypeDefinition ();
1125 public override bool HasGenericArguments {
1127 throw new NotImplementedException ();
1131 public override bool ContainsGenericParameters {
1133 throw new NotImplementedException ();
1137 public extern override bool IsGenericParameter {
1138 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1142 public override int GenericParameterPosition {
1144 throw new NotImplementedException ();
1148 public override MethodInfo DeclaringMethod {
1150 throw new NotImplementedException ();
1154 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1155 private extern MonoGenericParam define_generic_parameter (string name, int index);
1157 public Type DefineGenericParameter (string name)
1160 if (generic_params != null) {
1161 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1162 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1163 index = generic_params.Length;
1164 generic_params = new_generic_params;
1166 generic_params = new MonoGenericParam [1];
1170 generic_params [index] = define_generic_parameter (name, index);
1171 return generic_params [index];
1174 public void SetGenericParameterConstraints (int index, Type[] constraints)
1176 generic_params [index].SetConstraints (constraints);
1179 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1181 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);