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 {
24 #region Sync with reflection.h
26 private string nspace;
28 private Type nesting_type;
29 private Type[] interfaces;
30 private int num_methods;
31 private MethodBuilder[] methods;
32 private ConstructorBuilder[] ctors;
33 private PropertyBuilder[] properties;
34 private int num_fields;
35 private FieldBuilder[] fields;
36 private EventBuilder[] events;
37 private CustomAttributeBuilder[] cattrs;
38 internal TypeBuilder[] subtypes;
39 private TypeAttributes attrs;
40 private int table_idx;
41 private ModuleBuilder pmodule;
42 private int class_size;
43 private PackingSize packing_size;
44 private MonoGenericParam[] generic_params;
45 private RefEmitPermissionSet[] permissions;
50 public const int UnspecifiedTypeSize = 0;
52 protected override TypeAttributes GetAttributeFlagsImpl () {
56 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57 private extern void setup_internal_class (TypeBuilder tb);
59 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60 private extern void create_internal_class (TypeBuilder tb);
62 [MethodImplAttribute(MethodImplOptions.InternalCall)]
63 private extern void setup_generic_class (TypeBuilder tb);
65 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
69 fullname = this.tname = "<Module>";
72 setup_internal_class (this);
75 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
79 this.class_size = type_size;
80 this.packing_size = packing_size;
81 this.nesting_type = nesting_type;
82 sep_index = name.LastIndexOf('.');
83 if (sep_index != -1) {
84 this.tname = name.Substring (sep_index + 1);
85 this.nspace = name.Substring (0, sep_index);
90 if (interfaces != null) {
91 this.interfaces = new Type[interfaces.Length];
92 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
96 table_idx = mb.get_next_table_index (this, 0x02, true);
97 setup_internal_class (this);
98 fullname = GetFullName ();
101 public override Assembly Assembly {
102 get {return pmodule.Assembly;}
105 public override string AssemblyQualifiedName {
107 return fullname + ", " + Assembly.GetName().FullName;
110 public override Type BaseType {
115 public override Type DeclaringType {get {return nesting_type;}}
117 /* public override bool IsSubclassOf (Type c)
134 public override Type UnderlyingSystemType {
136 // This should return the type itself for non-enum types but
138 if (fields != null) {
139 foreach (FieldBuilder f in fields) {
140 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
144 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
148 string GetFullName () {
149 if (nesting_type != null)
150 return String.Concat (nesting_type.FullName, "+", tname);
151 if ((nspace != null) && (nspace.Length > 0))
152 return String.Concat (nspace, ".", tname);
156 public override string FullName {
162 public override Guid GUID {
164 throw not_supported ();
168 public override Module Module {
169 get {return pmodule;}
171 public override string Name {
174 public override string Namespace {
177 public PackingSize PackingSize {
178 get {return packing_size;}
181 get { return class_size; }
183 public override Type ReflectedType {get {return nesting_type;}}
185 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
187 throw new ArgumentNullException ("pset");
188 if ((action == SecurityAction.RequestMinimum) ||
189 (action == SecurityAction.RequestOptional) ||
190 (action == SecurityAction.RequestRefuse))
191 throw new ArgumentException ("Request* values are not permitted", "action");
194 throw not_after_created ();
196 if (permissions != null) {
197 /* Check duplicate actions */
198 foreach (RefEmitPermissionSet set in permissions)
199 if (set.action == action)
200 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
202 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
203 permissions.CopyTo (new_array, 0);
204 permissions = new_array;
207 permissions = new RefEmitPermissionSet [1];
209 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
210 attrs |= TypeAttributes.HasSecurity;
213 public void AddInterfaceImplementation( Type interfaceType) {
214 if (interfaceType == null)
215 throw new ArgumentNullException ("interfaceType");
217 throw not_after_created ();
219 if (interfaces != null) {
220 // Check for duplicates
221 foreach (Type t in interfaces)
222 if (t == interfaceType)
225 Type[] ifnew = new Type [interfaces.Length + 1];
226 interfaces.CopyTo (ifnew, 0);
227 ifnew [interfaces.Length] = interfaceType;
230 interfaces = new Type [1];
231 interfaces [0] = interfaceType;
236 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
237 CallingConventions callConvention, Type[] types,
238 ParameterModifier[] modifiers)
243 ConstructorBuilder found = null;
246 foreach (ConstructorBuilder cb in ctors){
247 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
257 throw new AmbiguousMatchException ();
260 MethodBase[] match = new MethodBase [count];
265 foreach (ConstructorInfo m in ctors) {
266 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
272 binder = Binder.DefaultBinder;
273 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
276 public override bool IsDefined( Type attributeType, bool inherit)
279 * MS throws NotSupported here, but we can't because some corlib
280 * classes make calls to IsDefined.
282 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
285 public override object[] GetCustomAttributes(bool inherit)
287 throw not_supported ();
290 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
292 throw not_supported ();
295 public TypeBuilder DefineNestedType (string name) {
296 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
299 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
300 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
303 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
304 return DefineNestedType (name, attr, parent, null);
307 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
308 PackingSize packsize, int typesize)
310 check_name ("name", name);
311 // Visibility must be NestedXXX
313 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
314 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
315 throw new ArgumentException ("attr", "Bad type flags for nested type.");
317 if (interfaces != null)
318 foreach (Type iface in interfaces)
320 throw new ArgumentNullException ("interfaces");
322 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
323 res.fullname = res.GetFullName ();
324 pmodule.RegisterTypeName (res, res.fullname);
325 if (subtypes != null) {
326 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
327 System.Array.Copy (subtypes, new_types, subtypes.Length);
328 new_types [subtypes.Length] = res;
329 subtypes = new_types;
331 subtypes = new TypeBuilder [1];
337 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
338 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
341 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
342 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
345 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
346 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
349 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
350 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
358 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
361 throw not_after_created ();
362 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
364 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
365 System.Array.Copy (ctors, new_ctors, ctors.Length);
366 new_ctors [ctors.Length] = cb;
369 ctors = new ConstructorBuilder [1];
375 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
377 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
382 parent_type = parent;
384 parent_type = pmodule.assemblyb.corlib_object_type;
386 ConstructorInfo parent_constructor =
387 parent_type.GetConstructor (
388 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
389 null, Type.EmptyTypes, null);
391 ILGenerator ig = cb.GetILGenerator ();
392 if (parent_constructor != null){
393 ig.Emit (OpCodes.Ldarg_0);
394 ig.Emit (OpCodes.Call, parent_constructor);
396 ig.Emit (OpCodes.Ret);
400 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
401 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
404 private void append_method (MethodBuilder mb) {
405 if (methods != null) {
406 if (methods.Length == num_methods) {
407 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
408 System.Array.Copy (methods, new_methods, num_methods);
409 methods = new_methods;
412 methods = new MethodBuilder [1];
414 methods [num_methods] = mb;
418 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
419 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
427 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
428 check_name ("name", name);
430 throw not_after_created ();
432 !((attributes & MethodAttributes.Abstract) != 0) ||
433 !((attributes & MethodAttributes.Virtual) != 0)))
434 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
436 if (returnType == null)
437 returnType = pmodule.assemblyb.corlib_void_type;
438 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
443 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
444 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
452 MethodBuilder DefinePInvokeMethod (
455 string entryName, MethodAttributes attributes,
456 CallingConventions callingConvention,
458 Type[] returnTypeRequiredCustomModifiers,
459 Type[] returnTypeOptionalCustomModifiers,
460 Type[] parameterTypes,
461 Type[][] parameterTypeRequiredCustomModifiers,
462 Type[][] parameterTypeOptionalCustomModifiers,
463 CallingConvention nativeCallConv,
464 CharSet nativeCharSet) {
465 check_name ("name", name);
466 check_name ("dllName", dllName);
467 check_name ("entryName", entryName);
468 if ((attributes & MethodAttributes.Abstract) != 0)
469 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
471 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
473 throw not_after_created ();
476 = new MethodBuilder (
482 returnTypeRequiredCustomModifiers,
483 returnTypeOptionalCustomModifiers,
485 parameterTypeRequiredCustomModifiers,
486 parameterTypeOptionalCustomModifiers,
495 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
496 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
497 nativeCallConv, nativeCharSet);
500 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
501 if (methodInfoBody == null)
502 throw new ArgumentNullException ("methodInfoBody");
503 if (methodInfoDeclaration == null)
504 throw new ArgumentNullException ("methodInfoDeclaration");
506 throw not_after_created ();
508 if (methodInfoBody is MethodBuilder) {
509 MethodBuilder mb = (MethodBuilder)methodInfoBody;
510 mb.set_override (methodInfoDeclaration);
514 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
515 return DefineField (fieldName, type, null, null, attributes);
523 FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
524 check_name ("fieldName", fieldName);
525 if (type == typeof (void))
526 throw new ArgumentException ("type", "Bad field type in defining field.");
528 throw not_after_created ();
530 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
531 if (fields != null) {
532 if (fields.Length == num_fields) {
533 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
534 System.Array.Copy (fields, new_fields, num_fields);
537 fields [num_fields] = res;
540 fields = new FieldBuilder [1];
543 create_internal_class (this);
548 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
549 check_name ("name", name);
550 if (parameterTypes != null)
551 foreach (Type param in parameterTypes)
553 throw new ArgumentNullException ("parameterTypes");
555 throw not_after_created ();
557 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
559 if (properties != null) {
560 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
561 System.Array.Copy (properties, new_properties, properties.Length);
562 new_properties [properties.Length] = res;
563 properties = new_properties;
565 properties = new PropertyBuilder [1];
566 properties [0] = res;
571 public ConstructorBuilder DefineTypeInitializer() {
572 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
575 [MethodImplAttribute(MethodImplOptions.InternalCall)]
576 private extern Type create_runtime_class (TypeBuilder tb);
578 private bool is_nested_in (Type t) {
588 public Type CreateType() {
589 /* handle nesting_type */
591 throw not_after_created ();
593 // Fire TypeResolve events for fields whose type is an unfinished
595 if (fields != null) {
596 foreach (FieldBuilder fb in fields) {
599 Type ft = fb.FieldType;
600 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
601 TypeBuilder tb = (TypeBuilder)ft;
602 if (!tb.is_created) {
603 AppDomain.CurrentDomain.DoTypeResolve (tb);
604 if (!tb.is_created) {
605 // FIXME: We should throw an exception here,
606 // but mcs expects that the type is created
607 // even if the exception is thrown
608 //throw new TypeLoadException ("Could not load type " + tb);
615 if (methods != null) {
616 for (int i = 0; i < num_methods; ++i)
617 ((MethodBuilder)(methods[i])).fixup ();
621 // On classes, define a default constructor if not provided
623 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
624 DefineDefaultConstructor (MethodAttributes.Public);
627 foreach (ConstructorBuilder ctor in ctors)
631 created = create_runtime_class (this);
637 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
640 return new ConstructorInfo [0];
641 ArrayList l = new ArrayList ();
643 MethodAttributes mattrs;
645 foreach (ConstructorBuilder c in ctors) {
647 mattrs = c.Attributes;
648 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
649 if ((bindingAttr & BindingFlags.Public) != 0)
652 if ((bindingAttr & BindingFlags.NonPublic) != 0)
658 if ((mattrs & MethodAttributes.Static) != 0) {
659 if ((bindingAttr & BindingFlags.Static) != 0)
662 if ((bindingAttr & BindingFlags.Instance) != 0)
669 ConstructorInfo[] result = new ConstructorInfo [l.Count];
674 public override Type GetElementType () {
675 throw not_supported ();
678 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
679 throw not_supported ();
682 /* Needed to keep signature compatibility with MS.NET */
683 public override EventInfo[] GetEvents ()
685 return GetEvents (DefaultBindingFlags);
688 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
689 // FIXME: Under MS.NET, this throws a NotImplementedException
690 // But mcs calls this method. How can that be?
691 return new EventInfo [0];
694 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
699 FieldAttributes mattrs;
701 foreach (FieldInfo c in fields) {
707 mattrs = c.Attributes;
708 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
709 if ((bindingAttr & BindingFlags.Public) != 0)
712 if ((bindingAttr & BindingFlags.NonPublic) != 0)
718 if ((mattrs & FieldAttributes.Static) != 0) {
719 if ((bindingAttr & BindingFlags.Static) != 0)
722 if ((bindingAttr & BindingFlags.Instance) != 0)
732 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
734 return new FieldInfo [0];
735 ArrayList l = new ArrayList ();
737 FieldAttributes mattrs;
739 foreach (FieldInfo c in fields) {
743 mattrs = c.Attributes;
744 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
745 if ((bindingAttr & BindingFlags.Public) != 0)
748 if ((bindingAttr & BindingFlags.NonPublic) != 0)
754 if ((mattrs & FieldAttributes.Static) != 0) {
755 if ((bindingAttr & BindingFlags.Static) != 0)
758 if ((bindingAttr & BindingFlags.Instance) != 0)
765 FieldInfo[] result = new FieldInfo [l.Count];
770 public override Type GetInterface (string name, bool ignoreCase) {
771 throw not_supported ();
774 public override Type[] GetInterfaces () {
775 if (interfaces != null) {
776 Type[] ret = new Type [interfaces.Length];
777 interfaces.CopyTo (ret, 0);
780 return Type.EmptyTypes;
784 public override MemberInfo[] GetMember (string name, MemberTypes type,
785 BindingFlags bindingAttr) {
786 throw not_supported ();
789 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
790 throw not_supported ();
793 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
795 return new MethodInfo [0];
796 ArrayList l = new ArrayList ();
798 MethodAttributes mattrs;
800 foreach (MethodInfo c in methods) {
804 mattrs = c.Attributes;
805 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
806 if ((bindingAttr & BindingFlags.Public) != 0)
809 if ((bindingAttr & BindingFlags.NonPublic) != 0)
815 if ((mattrs & MethodAttributes.Static) != 0) {
816 if ((bindingAttr & BindingFlags.Static) != 0)
819 if ((bindingAttr & BindingFlags.Instance) != 0)
826 MethodInfo[] result = new MethodInfo [l.Count];
831 protected override MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
832 throw not_supported ();
835 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
836 throw not_supported ();
839 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
841 ArrayList result = new ArrayList ();
843 if (subtypes == null)
844 return Type.EmptyTypes;
845 foreach (TypeBuilder t in subtypes) {
847 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
848 if ((bindingAttr & BindingFlags.Public) != 0)
851 if ((bindingAttr & BindingFlags.NonPublic) != 0)
858 Type[] r = new Type [result.Count];
863 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
864 if (properties == null)
865 return new PropertyInfo [0];
866 ArrayList l = new ArrayList ();
868 MethodAttributes mattrs;
871 foreach (PropertyInfo c in properties) {
873 accessor = c.GetGetMethod (true);
874 if (accessor == null)
875 accessor = c.GetSetMethod (true);
876 if (accessor == null)
878 mattrs = accessor.Attributes;
879 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
880 if ((bindingAttr & BindingFlags.Public) != 0)
883 if ((bindingAttr & BindingFlags.NonPublic) != 0)
889 if ((mattrs & MethodAttributes.Static) != 0) {
890 if ((bindingAttr & BindingFlags.Static) != 0)
893 if ((bindingAttr & BindingFlags.Instance) != 0)
900 PropertyInfo[] result = new PropertyInfo [l.Count];
905 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
906 throw not_supported ();
909 protected override bool HasElementTypeImpl () {
910 // According to the MSDN docs, this is supported for TypeBuilders,
911 // but in reality, it is not
912 throw not_supported ();
913 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
916 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
917 throw not_supported ();
920 protected override bool IsArrayImpl ()
922 return Type.IsArrayImpl (this);
925 protected override bool IsByRefImpl () {
929 protected override bool IsCOMObjectImpl () {
932 protected override bool IsPointerImpl () {
936 protected override bool IsPrimitiveImpl () {
940 protected override bool IsValueTypeImpl () {
941 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
942 this != pmodule.assemblyb.corlib_value_type &&
943 this != pmodule.assemblyb.corlib_enum_type);
946 public override RuntimeTypeHandle TypeHandle {
948 throw not_supported ();
952 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
953 if (customBuilder == null)
954 throw new ArgumentNullException ("customBuilder");
956 string attrname = customBuilder.Ctor.ReflectedType.FullName;
957 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
958 byte[] data = customBuilder.Data;
959 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
960 layout_kind = (int)data [2];
961 layout_kind |= ((int)data [3]) << 8;
962 attrs &= ~TypeAttributes.LayoutMask;
963 switch ((LayoutKind)layout_kind) {
964 case LayoutKind.Auto:
965 attrs |= TypeAttributes.AutoLayout;
967 case LayoutKind.Explicit:
968 attrs |= TypeAttributes.ExplicitLayout;
970 case LayoutKind.Sequential:
971 attrs |= TypeAttributes.SequentialLayout;
974 // we should ignore it since it can be any value anyway...
975 throw new Exception ("Error in customattr");
977 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
979 if (first_type_name == "System.Int16")
981 int nnamed = (int)data [pos++];
982 nnamed |= ((int)data [pos++]) << 8;
983 for (int i = 0; i < nnamed; ++i) {
984 byte named_type = data [pos++];
985 byte type = data [pos++];
986 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
987 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
989 /* all the fields are integers in StructLayout */
990 int value = (int)data [pos++];
991 value |= ((int)data [pos++]) << 8;
992 value |= ((int)data [pos++]) << 16;
993 value |= ((int)data [pos++]) << 24;
994 switch (named_name) {
996 switch ((CharSet)value) {
1000 case CharSet.Unicode:
1001 attrs |= TypeAttributes.UnicodeClass;
1004 attrs |= TypeAttributes.AutoClass;
1007 break; // error out...
1011 packing_size = (PackingSize)value;
1017 break; // error out...
1021 } else if (attrname == "System.SerializableAttribute") {
1022 attrs |= TypeAttributes.Serializable;
1025 if (cattrs != null) {
1026 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1027 cattrs.CopyTo (new_array, 0);
1028 new_array [cattrs.Length] = customBuilder;
1031 cattrs = new CustomAttributeBuilder [1];
1032 cattrs [0] = customBuilder;
1035 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1036 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1039 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1040 check_name ("name", name);
1041 if (eventtype == null)
1042 throw new ArgumentNullException ("eventtype");
1044 throw not_after_created ();
1046 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1047 if (events != null) {
1048 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1049 System.Array.Copy (events, new_events, events.Length);
1050 new_events [events.Length] = res;
1051 events = new_events;
1053 events = new EventBuilder [1];
1059 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1061 throw new ArgumentNullException ("data");
1062 if ((data.Length == 0) || (data.Length > 0x3f0000))
1063 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1065 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1066 res.SetRVAData (data);
1071 static int UnmanagedDataCount = 0;
1073 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1074 check_name ("name", name);
1075 if ((size <= 0) || (size > 0x3f0000))
1076 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1078 throw not_after_created ();
1080 string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1081 UnmanagedDataCount++;
1082 TypeBuilder datablobtype = DefineNestedType (s,
1083 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1084 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1085 datablobtype.CreateType ();
1086 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1089 public TypeToken TypeToken {
1091 return new TypeToken (0x02000000 | table_idx);
1094 public void SetParent (Type parentType) {
1095 if (parentType == null)
1096 throw new ArgumentNullException ("parentType");
1098 throw not_after_created ();
1100 parent = parentType;
1102 internal int get_next_table_index (object obj, int table, bool inc) {
1103 return pmodule.get_next_table_index (obj, table, inc);
1106 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1108 if (created == null)
1109 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1111 return created.GetInterfaceMap (interfaceType);
1114 internal bool is_created {
1116 return created != null;
1120 private Exception not_supported ()
1122 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1125 private Exception not_after_created ()
1127 return new InvalidOperationException ("Unable to change after type has been created.");
1130 private void check_name (string argName, string name)
1133 throw new ArgumentNullException (argName);
1135 throw new ArgumentException (argName, "Empty name is not legal.");
1136 if (name.IndexOf ((char)0) != -1)
1137 throw new ArgumentException (argName, "Illegal name.");
1141 public override Type GetGenericTypeDefinition ()
1143 setup_generic_class (this);
1145 return base.GetGenericTypeDefinition ();
1148 public override bool HasGenericArguments {
1150 throw new NotImplementedException ();
1154 public override bool ContainsGenericParameters {
1156 throw new NotImplementedException ();
1160 public extern override bool IsGenericParameter {
1161 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1165 public override int GenericParameterPosition {
1167 throw new NotImplementedException ();
1171 public override MethodInfo DeclaringMethod {
1173 throw new NotImplementedException ();
1177 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1178 private extern MonoGenericParam define_generic_parameter (string name, int index);
1180 public Type DefineGenericParameter (string name)
1183 if (generic_params != null) {
1184 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1185 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1186 index = generic_params.Length;
1187 generic_params = new_generic_params;
1189 generic_params = new MonoGenericParam [1];
1193 generic_params [index] = define_generic_parameter (name, index);
1194 return generic_params [index];
1197 public void SetGenericParameterConstraints (int index, Type[] constraints)
1199 generic_params [index].SetConstraints (constraints);
1202 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1204 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);