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, new Type [0]);
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 private bool is_nested_in (Type t) {
491 public Type CreateType() {
492 /* handle nesting_type */
494 throw not_after_created ();
496 // Fire TypeResolve events for fields whose type is an unfinished
498 if (fields != null) {
499 foreach (FieldBuilder fb in fields) {
500 Type ft = fb.FieldType;
501 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
502 TypeBuilder tb = (TypeBuilder)ft;
503 if (!tb.is_created) {
504 AppDomain.CurrentDomain.DoTypeResolve (tb);
505 if (!tb.is_created) {
506 // FIXME: We should throw an exception here,
507 // but mcs expects that the type is created
508 // even if the exception is thrown
509 //throw new TypeLoadException ("Could not load type " + tb);
516 if (methods != null) {
517 foreach (MethodBuilder method in methods) {
523 // On classes, define a default constructor if not provided
525 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
526 DefineDefaultConstructor (MethodAttributes.Public);
529 foreach (ConstructorBuilder ctor in ctors)
533 created = create_runtime_class (this);
539 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
542 return new ConstructorInfo [0];
543 ArrayList l = new ArrayList ();
545 MethodAttributes mattrs;
547 foreach (ConstructorBuilder c in ctors) {
549 mattrs = c.Attributes;
550 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
551 if ((bindingAttr & BindingFlags.Public) != 0)
554 if ((bindingAttr & BindingFlags.NonPublic) != 0)
560 if ((mattrs & MethodAttributes.Static) != 0) {
561 if ((bindingAttr & BindingFlags.Static) != 0)
564 if ((bindingAttr & BindingFlags.Instance) != 0)
571 ConstructorInfo[] result = new ConstructorInfo [l.Count];
576 public override Type GetElementType () {
577 throw not_supported ();
580 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
581 throw not_supported ();
584 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
585 // FIXME: Under MS.NET, this throws a NotImplementedException
586 // But mcs calls this method. How can that be?
587 return new EventInfo [0];
590 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
591 throw not_supported ();
594 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
596 return new FieldInfo [0];
597 ArrayList l = new ArrayList ();
599 FieldAttributes mattrs;
601 foreach (FieldInfo c in fields) {
603 mattrs = c.Attributes;
604 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
605 if ((bindingAttr & BindingFlags.Public) != 0)
608 if ((bindingAttr & BindingFlags.NonPublic) != 0)
614 if ((mattrs & FieldAttributes.Static) != 0) {
615 if ((bindingAttr & BindingFlags.Static) != 0)
618 if ((bindingAttr & BindingFlags.Instance) != 0)
625 FieldInfo[] result = new FieldInfo [l.Count];
630 public override Type GetInterface (string name, bool ignoreCase) {
631 throw not_supported ();
634 public override Type[] GetInterfaces () {
635 if (interfaces != null) {
636 Type[] ret = new Type [interfaces.Length];
637 interfaces.CopyTo (ret, 0);
640 return Type.EmptyTypes;
644 public override MemberInfo[] GetMember (string name, MemberTypes type,
645 BindingFlags bindingAttr) {
646 throw not_supported ();
649 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
650 throw not_supported ();
653 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
655 return new MethodInfo [0];
656 ArrayList l = new ArrayList ();
658 MethodAttributes mattrs;
660 foreach (MethodInfo c in methods) {
662 mattrs = c.Attributes;
663 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
664 if ((bindingAttr & BindingFlags.Public) != 0)
667 if ((bindingAttr & BindingFlags.NonPublic) != 0)
673 if ((mattrs & MethodAttributes.Static) != 0) {
674 if ((bindingAttr & BindingFlags.Static) != 0)
677 if ((bindingAttr & BindingFlags.Instance) != 0)
684 MethodInfo[] result = new MethodInfo [l.Count];
689 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
690 throw not_supported ();
693 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
694 throw not_supported ();
697 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
699 ArrayList result = new ArrayList ();
701 if (subtypes == null)
702 return Type.EmptyTypes;
703 foreach (TypeBuilder t in subtypes) {
705 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
706 if ((bindingAttr & BindingFlags.Public) != 0)
709 if ((bindingAttr & BindingFlags.NonPublic) != 0)
716 Type[] r = new Type [result.Count];
721 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
722 if (properties == null)
723 return new PropertyInfo [0];
724 ArrayList l = new ArrayList ();
726 MethodAttributes mattrs;
729 foreach (PropertyInfo c in properties) {
731 accessor = c.GetGetMethod (true);
732 if (accessor == null)
733 accessor = c.GetSetMethod (true);
734 if (accessor == null)
736 mattrs = accessor.Attributes;
737 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
738 if ((bindingAttr & BindingFlags.Public) != 0)
741 if ((bindingAttr & BindingFlags.NonPublic) != 0)
747 if ((mattrs & MethodAttributes.Static) != 0) {
748 if ((bindingAttr & BindingFlags.Static) != 0)
751 if ((bindingAttr & BindingFlags.Instance) != 0)
758 PropertyInfo[] result = new PropertyInfo [l.Count];
763 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
764 throw not_supported ();
767 protected override bool HasElementTypeImpl () {
768 // According to the MSDN docs, this is supported for TypeBuilders,
769 // but in reality, it is not
770 throw not_supported ();
771 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
774 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
775 throw not_supported ();
778 protected override bool IsArrayImpl ()
780 return Type.IsArrayImpl (this);
783 protected override bool IsByRefImpl () {
787 protected override bool IsCOMObjectImpl () {
790 protected override bool IsPointerImpl () {
794 protected override bool IsPrimitiveImpl () {
798 protected override bool IsValueTypeImpl () {
799 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
800 this != pmodule.assemblyb.corlib_value_type &&
801 this != pmodule.assemblyb.corlib_enum_type);
804 public override RuntimeTypeHandle TypeHandle {
806 throw not_supported ();
810 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
811 if (customBuilder == null)
812 throw new ArgumentNullException ("customBuilder");
814 string attrname = customBuilder.Ctor.ReflectedType.FullName;
815 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
816 byte[] data = customBuilder.Data;
817 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
818 layout_kind = (int)data [2];
819 layout_kind |= ((int)data [3]) << 8;
820 attrs &= ~TypeAttributes.LayoutMask;
821 switch ((LayoutKind)layout_kind) {
822 case LayoutKind.Auto:
823 attrs |= TypeAttributes.AutoLayout;
825 case LayoutKind.Explicit:
826 attrs |= TypeAttributes.ExplicitLayout;
828 case LayoutKind.Sequential:
829 attrs |= TypeAttributes.SequentialLayout;
832 // we should ignore it since it can be any value anyway...
833 throw new Exception ("Error in customattr");
835 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
837 if (first_type_name == "System.Int16")
839 int nnamed = (int)data [pos++];
840 nnamed |= ((int)data [pos++]) << 8;
841 for (int i = 0; i < nnamed; ++i) {
842 byte named_type = data [pos++];
843 byte type = data [pos++];
844 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
845 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
847 /* all the fields are integers in StructLayout */
848 int value = (int)data [pos++];
849 value |= ((int)data [pos++]) << 8;
850 value |= ((int)data [pos++]) << 16;
851 value |= ((int)data [pos++]) << 24;
852 switch (named_name) {
854 switch ((CharSet)value) {
858 case CharSet.Unicode:
859 attrs |= TypeAttributes.UnicodeClass;
862 attrs |= TypeAttributes.AutoClass;
865 break; // error out...
869 packing_size = (PackingSize)value;
875 break; // error out...
879 } else if (attrname == "System.SerializableAttribute") {
880 attrs |= TypeAttributes.Serializable;
883 if (cattrs != null) {
884 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
885 cattrs.CopyTo (new_array, 0);
886 new_array [cattrs.Length] = customBuilder;
889 cattrs = new CustomAttributeBuilder [1];
890 cattrs [0] = customBuilder;
893 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
894 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
897 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
898 check_name ("name", name);
899 if (eventtype == null)
900 throw new ArgumentNullException ("eventtype");
902 throw not_after_created ();
904 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
905 if (events != null) {
906 EventBuilder[] new_events = new EventBuilder [events.Length+1];
907 System.Array.Copy (events, new_events, events.Length);
908 new_events [events.Length] = res;
911 events = new EventBuilder [1];
917 static int InitializedDataCount = 0;
919 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
920 check_name ("name", name);
922 throw new ArgumentNullException ("data");
923 if ((data.Length == 0) || (data.Length > 0x3f0000))
924 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
926 throw not_after_created ();
928 string s = "$ArrayType$"+InitializedDataCount.ToString();
929 TypeBuilder datablobtype = DefineNestedType (s,
930 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
931 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, data.Length);
932 datablobtype.CreateType ();
933 FieldBuilder res = DefineField (name, datablobtype, attributes|FieldAttributes.Assembly|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
934 res.SetRVAData (data);
935 InitializedDataCount++;
940 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
941 check_name ("name", name);
942 if ((size <= 0) || (size > 0x3f0000))
943 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
945 throw not_after_created ();
947 throw new NotImplementedException ();
950 public TypeToken TypeToken {
952 return new TypeToken (0x02000000 | table_idx);
955 public void SetParent (Type parentType) {
956 if (parentType == null)
957 throw new ArgumentNullException ("parentType");
959 throw not_after_created ();
963 internal int get_next_table_index (object obj, int table, bool inc) {
964 return pmodule.get_next_table_index (obj, table, inc);
967 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
970 throw new NotSupportedException ("This method is not implemented for incomplete types.");
972 return created.GetInterfaceMap (interfaceType);
975 internal bool is_created {
977 return created != null;
981 private Exception not_supported ()
983 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
986 private Exception not_after_created ()
988 return new InvalidOperationException ("Unable to change after type has been created.");
991 private void check_name (string argName, string name)
994 throw new ArgumentNullException (argName);
996 throw new ArgumentException (argName, "Empty name is not legal.");
997 if (name.IndexOf ((char)0) != -1)
998 throw new ArgumentException (argName, "Illegal name.");
1002 public override Type GetGenericTypeDefinition ()
1004 setup_generic_class (this);
1006 return base.GetGenericTypeDefinition ();
1009 public override bool HasGenericArguments {
1011 throw new NotImplementedException ();
1015 public override bool ContainsGenericParameters {
1017 throw new NotImplementedException ();
1021 public extern override bool IsGenericParameter {
1022 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1026 public override int GenericParameterPosition {
1028 throw new NotImplementedException ();
1032 public override MethodInfo DeclaringMethod {
1034 throw new NotImplementedException ();
1038 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1039 private extern Type define_generic_parameter (MonoGenericParam param);
1041 public Type DefineGenericParameter (string name, Type[] constraints)
1043 MonoGenericParam gparam = new MonoGenericParam (name, constraints);
1045 if (generic_params != null) {
1046 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1047 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1048 new_generic_params [generic_params.Length] = gparam;
1049 generic_params = new_generic_params;
1051 generic_params = new MonoGenericParam [1];
1052 generic_params [0] = gparam;
1055 return define_generic_parameter (gparam);
1058 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1060 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);
1064 internal sealed class MonoGenericParam {
1065 private readonly uint Handle;
1067 public readonly Type Type;
1068 public readonly string Name;
1069 public readonly int Flags;
1070 public readonly Type[] Constraints;
1072 public MonoGenericParam (string name, Type[] constraints)
1075 this.Constraints = constraints;