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 MethodBuilder[] methods;
30 private ConstructorBuilder[] ctors;
31 private PropertyBuilder[] properties;
32 private FieldBuilder[] fields;
33 private EventBuilder[] events;
34 private CustomAttributeBuilder[] cattrs;
35 internal TypeBuilder[] subtypes;
36 private TypeAttributes attrs;
37 private int table_idx;
38 private ModuleBuilder pmodule;
39 private int class_size;
40 private PackingSize packing_size;
41 private MonoGenericParam[] generic_params;
45 public const int UnspecifiedTypeSize = 0;
47 protected override TypeAttributes GetAttributeFlagsImpl () {
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 private extern void setup_internal_class (TypeBuilder tb);
54 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55 private extern void create_internal_class (TypeBuilder tb);
57 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58 private extern void setup_generic_class (TypeBuilder tb);
60 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
64 fullname = this.tname = "<Module>";
67 setup_internal_class (this);
70 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
74 this.class_size = type_size;
75 this.packing_size = packing_size;
76 this.nesting_type = nesting_type;
77 sep_index = name.LastIndexOf('.');
78 if (sep_index != -1) {
79 this.tname = name.Substring (sep_index + 1);
80 this.nspace = name.Substring (0, sep_index);
85 if (interfaces != null) {
86 this.interfaces = new Type[interfaces.Length];
87 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
91 table_idx = mb.get_next_table_index (this, 0x02, true);
92 setup_internal_class (this);
93 fullname = GetFullName ();
96 public override Assembly Assembly {
97 get {return pmodule.Assembly;}
100 public override string AssemblyQualifiedName {
102 return fullname + ", " + Assembly.GetName().FullName;
105 public override Type BaseType {
110 public override Type DeclaringType {get {return nesting_type;}}
112 /* public override bool IsSubclassOf (Type c)
129 public override Type UnderlyingSystemType {
131 // This should return the type itself for non-enum types but
133 if (fields != null) {
134 foreach (FieldBuilder f in fields) {
135 if ((f.Attributes & FieldAttributes.Static) == 0)
139 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
143 string GetFullName () {
144 if (nesting_type != null)
145 return String.Concat (nesting_type.FullName, "+", tname);
146 if ((nspace != null) && (nspace.Length > 0))
147 return String.Concat (nspace, ".", tname);
151 public override string FullName {
157 public override Guid GUID {
159 throw not_supported ();
163 public override Module Module {
164 get {return pmodule;}
166 public override string Name {
169 public override string Namespace {
172 public PackingSize PackingSize {
173 get {return packing_size;}
176 get { return class_size; }
178 public override Type ReflectedType {get {return nesting_type;}}
179 public override MemberTypes MemberType {
180 get {return MemberTypes.TypeInfo;}
184 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
185 throw new NotImplementedException ();
188 public void AddInterfaceImplementation( Type interfaceType) {
189 if (interfaceType == null)
190 throw new ArgumentNullException ("interfaceType");
192 throw not_after_created ();
194 if (interfaces != null) {
195 // Check for duplicates
196 foreach (Type t in interfaces)
197 if (t == interfaceType)
200 Type[] ifnew = new Type [interfaces.Length + 1];
201 interfaces.CopyTo (ifnew, 0);
202 ifnew [interfaces.Length] = interfaceType;
205 interfaces = new Type [1];
206 interfaces [0] = interfaceType;
211 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
212 CallingConventions callConvention, Type[] types,
213 ParameterModifier[] modifiers)
218 ConstructorBuilder found = null;
221 foreach (ConstructorBuilder cb in ctors){
222 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
232 throw new AmbiguousMatchException ();
235 MethodBase[] match = new MethodBase [count];
240 foreach (ConstructorInfo m in ctors) {
241 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
247 binder = Binder.DefaultBinder;
248 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
251 public override bool IsDefined( Type attributeType, bool inherit)
253 throw not_supported ();
256 public override object[] GetCustomAttributes(bool inherit)
258 throw not_supported ();
261 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
263 throw not_supported ();
266 public TypeBuilder DefineNestedType (string name) {
267 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
270 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
271 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
274 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
275 return DefineNestedType (name, attr, parent, null);
278 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
279 PackingSize packsize, int typesize)
281 check_name ("name", name);
282 // Visibility must be NestedXXX
284 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
285 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
286 throw new ArgumentException ("attr", "Bad type flags for nested type.");
288 if (interfaces != null)
289 foreach (Type iface in interfaces)
291 throw new ArgumentNullException ("interfaces");
293 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
294 res.fullname = res.GetFullName ();
295 pmodule.RegisterTypeName (res, res.fullname);
296 if (subtypes != null) {
297 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
298 System.Array.Copy (subtypes, new_types, subtypes.Length);
299 new_types [subtypes.Length] = res;
300 subtypes = new_types;
302 subtypes = new TypeBuilder [1];
308 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
309 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
312 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
313 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
316 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
317 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
320 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
323 throw not_after_created ();
324 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes);
326 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
327 System.Array.Copy (ctors, new_ctors, ctors.Length);
328 new_ctors [ctors.Length] = cb;
331 ctors = new ConstructorBuilder [1];
337 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
339 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, null);
344 parent_type = parent;
346 parent_type = pmodule.assemblyb.corlib_object_type;
348 ConstructorInfo parent_constructor =
349 parent_type.GetConstructor (
350 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
351 null, Type.EmptyTypes, null);
353 ILGenerator ig = cb.GetILGenerator ();
354 if (parent_constructor != null){
355 ig.Emit (OpCodes.Ldarg_0);
356 ig.Emit (OpCodes.Call, parent_constructor);
358 ig.Emit (OpCodes.Ret);
362 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
363 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
366 private void append_method (MethodBuilder mb) {
367 if (methods != null) {
368 MethodBuilder[] new_methods = new MethodBuilder [methods.Length+1];
369 System.Array.Copy (methods, new_methods, methods.Length);
370 new_methods [methods.Length] = mb;
371 methods = new_methods;
373 methods = new MethodBuilder [1];
378 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
379 check_name ("name", name);
381 throw not_after_created ();
383 !((attributes & MethodAttributes.Abstract) != 0) ||
384 !((attributes & MethodAttributes.Virtual) != 0)))
385 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
387 if (returnType == null)
388 returnType = pmodule.assemblyb.corlib_void_type;
389 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes);
394 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
395 check_name ("name", name);
396 check_name ("dllName", dllName);
397 check_name ("entryName", entryName);
398 if ((attributes & MethodAttributes.Abstract) != 0)
399 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
401 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
403 throw not_after_created ();
405 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, parameterTypes,
406 dllName, entryName, nativeCallConv, nativeCharSet);
411 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
412 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
413 nativeCallConv, nativeCharSet);
416 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
417 if (methodInfoBody == null)
418 throw new ArgumentNullException ("methodInfoBody");
419 if (methodInfoDeclaration == null)
420 throw new ArgumentNullException ("methodInfoDeclaration");
422 throw not_after_created ();
424 if (methodInfoBody is MethodBuilder) {
425 MethodBuilder mb = (MethodBuilder)methodInfoBody;
426 mb.set_override (methodInfoDeclaration);
430 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
431 check_name ("fieldName", fieldName);
432 if (type == typeof (void))
433 throw new ArgumentException ("type", "Bad field type in defining field.");
435 throw not_after_created ();
437 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes);
438 if (fields != null) {
439 FieldBuilder[] new_fields = new FieldBuilder [fields.Length+1];
440 System.Array.Copy (fields, new_fields, fields.Length);
441 new_fields [fields.Length] = res;
444 fields = new FieldBuilder [1];
446 create_internal_class (this);
451 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
452 check_name ("name", name);
453 if (parameterTypes != null)
454 foreach (Type param in parameterTypes)
456 throw new ArgumentNullException ("parameterTypes");
458 throw not_after_created ();
460 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
462 if (properties != null) {
463 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
464 System.Array.Copy (properties, new_properties, properties.Length);
465 new_properties [properties.Length] = res;
466 properties = new_properties;
468 properties = new PropertyBuilder [1];
469 properties [0] = res;
474 public ConstructorBuilder DefineTypeInitializer() {
475 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
478 [MethodImplAttribute(MethodImplOptions.InternalCall)]
479 private extern Type create_runtime_class (TypeBuilder tb);
481 public Type CreateType() {
482 /* handle nesting_type */
484 throw not_after_created ();
485 if (methods != null) {
486 foreach (MethodBuilder method in methods) {
492 // On classes, define a default constructor if not provided
494 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
495 DefineDefaultConstructor (MethodAttributes.Public);
498 foreach (ConstructorBuilder ctor in ctors)
502 created = create_runtime_class (this);
508 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
511 return new ConstructorInfo [0];
512 ArrayList l = new ArrayList ();
514 MethodAttributes mattrs;
516 foreach (ConstructorBuilder c in ctors) {
518 mattrs = c.Attributes;
519 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
520 if ((bindingAttr & BindingFlags.Public) != 0)
523 if ((bindingAttr & BindingFlags.NonPublic) != 0)
529 if ((mattrs & MethodAttributes.Static) != 0) {
530 if ((bindingAttr & BindingFlags.Static) != 0)
533 if ((bindingAttr & BindingFlags.Instance) != 0)
540 ConstructorInfo[] result = new ConstructorInfo [l.Count];
545 public override Type GetElementType () {
546 throw not_supported ();
549 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
550 throw not_supported ();
553 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
554 // FIXME: Under MS.NET, this throws a NotImplementedException
555 // But mcs calls this method. How can that be?
556 return new EventInfo [0];
559 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
560 throw not_supported ();
563 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
565 return new FieldInfo [0];
566 ArrayList l = new ArrayList ();
568 FieldAttributes mattrs;
570 foreach (FieldInfo c in fields) {
572 mattrs = c.Attributes;
573 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
574 if ((bindingAttr & BindingFlags.Public) != 0)
577 if ((bindingAttr & BindingFlags.NonPublic) != 0)
583 if ((mattrs & FieldAttributes.Static) != 0) {
584 if ((bindingAttr & BindingFlags.Static) != 0)
587 if ((bindingAttr & BindingFlags.Instance) != 0)
594 FieldInfo[] result = new FieldInfo [l.Count];
599 public override Type GetInterface (string name, bool ignoreCase) {
600 throw not_supported ();
603 public override Type[] GetInterfaces () {
604 if (interfaces != null) {
605 Type[] ret = new Type [interfaces.Length];
606 interfaces.CopyTo (ret, 0);
609 return Type.EmptyTypes;
613 public override MemberInfo[] GetMember (string name, MemberTypes type,
614 BindingFlags bindingAttr) {
615 throw not_supported ();
618 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
619 throw not_supported ();
622 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
624 return new MethodInfo [0];
625 ArrayList l = new ArrayList ();
627 MethodAttributes mattrs;
629 foreach (MethodInfo c in methods) {
631 mattrs = c.Attributes;
632 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
633 if ((bindingAttr & BindingFlags.Public) != 0)
636 if ((bindingAttr & BindingFlags.NonPublic) != 0)
642 if ((mattrs & MethodAttributes.Static) != 0) {
643 if ((bindingAttr & BindingFlags.Static) != 0)
646 if ((bindingAttr & BindingFlags.Instance) != 0)
653 MethodInfo[] result = new MethodInfo [l.Count];
658 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
659 throw not_supported ();
662 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
663 throw not_supported ();
666 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
668 ArrayList result = new ArrayList ();
670 if (subtypes == null)
671 return Type.EmptyTypes;
672 foreach (TypeBuilder t in subtypes) {
674 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
675 if ((bindingAttr & BindingFlags.Public) != 0)
678 if ((bindingAttr & BindingFlags.NonPublic) != 0)
685 Type[] r = new Type [result.Count];
690 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
691 if (properties == null)
692 return new PropertyInfo [0];
693 ArrayList l = new ArrayList ();
695 MethodAttributes mattrs;
698 foreach (PropertyInfo c in properties) {
700 accessor = c.GetGetMethod (true);
701 if (accessor == null)
702 accessor = c.GetSetMethod (true);
703 if (accessor == null)
705 mattrs = accessor.Attributes;
706 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
707 if ((bindingAttr & BindingFlags.Public) != 0)
710 if ((bindingAttr & BindingFlags.NonPublic) != 0)
716 if ((mattrs & MethodAttributes.Static) != 0) {
717 if ((bindingAttr & BindingFlags.Static) != 0)
720 if ((bindingAttr & BindingFlags.Instance) != 0)
727 PropertyInfo[] result = new PropertyInfo [l.Count];
732 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
733 throw not_supported ();
736 protected override bool HasElementTypeImpl () {
737 // According to the MSDN docs, this is supported for TypeBuilders,
738 // but in reality, it is not
739 throw not_supported ();
740 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
743 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
744 throw not_supported ();
747 protected override bool IsArrayImpl ()
749 return Type.IsArrayImpl (this);
752 protected override bool IsByRefImpl () {
756 protected override bool IsCOMObjectImpl () {
759 protected override bool IsPointerImpl () {
763 protected override bool IsPrimitiveImpl () {
767 protected override bool IsValueTypeImpl () {
768 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
769 this != pmodule.assemblyb.corlib_value_type &&
770 this != pmodule.assemblyb.corlib_enum_type);
773 public override RuntimeTypeHandle TypeHandle {
775 throw not_supported ();
779 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
780 if (customBuilder == null)
781 throw new ArgumentNullException ("customBuilder");
783 string attrname = customBuilder.Ctor.ReflectedType.FullName;
784 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
785 byte[] data = customBuilder.Data;
786 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
787 layout_kind = (int)data [2];
788 layout_kind |= ((int)data [3]) << 8;
789 attrs &= ~TypeAttributes.LayoutMask;
790 switch ((LayoutKind)layout_kind) {
791 case LayoutKind.Auto:
792 attrs |= TypeAttributes.AutoLayout;
794 case LayoutKind.Explicit:
795 attrs |= TypeAttributes.ExplicitLayout;
797 case LayoutKind.Sequential:
798 attrs |= TypeAttributes.SequentialLayout;
801 // we should ignore it since it can be any value anyway...
802 throw new Exception ("Error in customattr");
804 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
806 if (first_type_name == "System.Int16")
808 int nnamed = (int)data [pos++];
809 nnamed |= ((int)data [pos++]) << 8;
810 for (int i = 0; i < nnamed; ++i) {
811 byte named_type = data [pos++];
812 byte type = data [pos++];
813 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
814 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
816 /* all the fields are integers in StructLayout */
817 int value = (int)data [pos++];
818 value |= ((int)data [pos++]) << 8;
819 value |= ((int)data [pos++]) << 16;
820 value |= ((int)data [pos++]) << 24;
821 switch (named_name) {
823 switch ((CharSet)value) {
827 case CharSet.Unicode:
828 attrs |= TypeAttributes.UnicodeClass;
831 attrs |= TypeAttributes.AutoClass;
834 break; // error out...
838 packing_size = (PackingSize)value;
844 break; // error out...
848 } else if (attrname == "System.SerializableAttribute") {
849 attrs |= TypeAttributes.Serializable;
852 if (cattrs != null) {
853 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
854 cattrs.CopyTo (new_array, 0);
855 new_array [cattrs.Length] = customBuilder;
858 cattrs = new CustomAttributeBuilder [1];
859 cattrs [0] = customBuilder;
862 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
863 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
866 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
867 check_name ("name", name);
868 if (eventtype == null)
869 throw new ArgumentNullException ("eventtype");
871 throw not_after_created ();
873 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
874 if (events != null) {
875 EventBuilder[] new_events = new EventBuilder [events.Length+1];
876 System.Array.Copy (events, new_events, events.Length);
877 new_events [events.Length] = res;
880 events = new EventBuilder [1];
886 static int InitializedDataCount = 0;
888 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
889 check_name ("name", name);
891 throw new ArgumentNullException ("data");
892 if ((data.Length == 0) || (data.Length > 0x3f0000))
893 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
895 throw not_after_created ();
897 string s = "$ArrayType$"+InitializedDataCount.ToString();
898 TypeBuilder datablobtype = DefineNestedType (s,
899 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
900 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
901 datablobtype.CreateType ();
902 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
903 res.SetRVAData (data);
904 InitializedDataCount++;
909 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
910 check_name ("name", name);
911 if ((size <= 0) || (size > 0x3f0000))
912 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
914 throw not_after_created ();
916 throw new NotImplementedException ();
919 public TypeToken TypeToken {
921 return new TypeToken (0x02000000 | table_idx);
924 public void SetParent (Type parentType) {
925 if (parentType == null)
926 throw new ArgumentNullException ("parentType");
928 throw not_after_created ();
932 internal int get_next_table_index (object obj, int table, bool inc) {
933 return pmodule.get_next_table_index (obj, table, inc);
936 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
939 throw new NotSupportedException ("This method is not implemented for incomplete types.");
941 return created.GetInterfaceMap (interfaceType);
944 internal bool is_created {
946 return created != null;
950 private Exception not_supported ()
952 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
955 private Exception not_after_created ()
957 return new InvalidOperationException ("Unable to change after type has been created.");
960 private void check_name (string argName, string name)
963 throw new ArgumentNullException (argName);
965 throw new ArgumentException (argName, "Empty name is not legal.");
966 if (name.IndexOf ((char)0) != -1)
967 throw new ArgumentException (argName, "Illegal name.");
971 public override Type GetGenericTypeDefinition ()
973 setup_generic_class (this);
975 return base.GetGenericTypeDefinition ();
978 public override bool HasGenericParameters {
980 throw new NotImplementedException ();
984 public override bool HasUnboundGenericParameters {
986 throw new NotImplementedException ();
990 public extern override bool IsUnboundGenericParameter {
991 [MethodImplAttribute(MethodImplOptions.InternalCall)]
995 public override int GenericParameterPosition {
997 throw new Exception ("Unimplemented");
1001 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1002 private extern static Type define_generic_parameter (TypeBuilder tb, MonoGenericParam param);
1004 public Type DefineGenericParameter (string name, Type[] constraints)
1006 MonoGenericParam gparam = new MonoGenericParam (name, constraints);
1008 if (generic_params != null) {
1009 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1010 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1011 new_generic_params [generic_params.Length] = gparam;
1012 generic_params = new_generic_params;
1014 generic_params = new MonoGenericParam [1];
1015 generic_params [0] = gparam;
1018 return define_generic_parameter (this, gparam);
1022 internal sealed class MonoGenericParam {
1023 private readonly uint Handle;
1025 public readonly Type Type;
1026 public readonly string Name;
1027 public readonly int Flags;
1028 public readonly Type[] Constraints;
1030 public MonoGenericParam (string name, Type[] constraints)
1033 this.Constraints = constraints;