2 // System.Reflection.Emit.TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41 using System.Globalization;
42 using System.Collections;
43 using System.Security;
44 using System.Security.Permissions;
45 using System.Diagnostics.SymbolStore;
47 namespace System.Reflection.Emit
50 [ComDefaultInterface (typeof (_TypeBuilder))]
51 [ClassInterface (ClassInterfaceType.None)]
52 [StructLayout (LayoutKind.Sequential)]
53 public sealed class TypeBuilder :
61 #pragma warning disable 169
62 #region Sync with reflection.h
64 private string nspace;
66 private Type nesting_type;
67 internal Type[] interfaces;
68 internal int num_methods;
69 internal MethodBuilder[] methods;
70 internal ConstructorBuilder[] ctors;
71 internal PropertyBuilder[] properties;
72 internal int num_fields;
73 internal FieldBuilder[] fields;
74 internal EventBuilder[] events;
75 private CustomAttributeBuilder[] cattrs;
76 internal TypeBuilder[] subtypes;
77 internal TypeAttributes attrs;
78 private int table_idx;
79 private ModuleBuilder pmodule;
80 private int class_size;
81 private PackingSize packing_size;
82 private IntPtr generic_container;
83 private GenericTypeParameterBuilder[] generic_params;
84 private RefEmitPermissionSet[] permissions;
87 #pragma warning restore 169
90 bool createTypeCalled;
91 private Type underlying_type;
93 public const int UnspecifiedTypeSize = 0;
95 protected override TypeAttributes GetAttributeFlagsImpl ()
100 [MethodImplAttribute(MethodImplOptions.InternalCall)]
101 private extern void setup_internal_class (TypeBuilder tb);
103 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104 private extern void create_internal_class (TypeBuilder tb);
106 [MethodImplAttribute(MethodImplOptions.InternalCall)]
107 private extern void setup_generic_class ();
109 [MethodImplAttribute(MethodImplOptions.InternalCall)]
110 private extern void create_generic_class ();
112 [MethodImplAttribute(MethodImplOptions.InternalCall)]
113 private extern EventInfo get_event_info (EventBuilder eb);
115 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
119 this.class_size = UnspecifiedTypeSize;
120 this.table_idx = table_idx;
121 fullname = this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
122 this.nspace = String.Empty;
124 setup_internal_class (this);
127 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
130 this.parent = parent;
132 this.class_size = type_size;
133 this.packing_size = packing_size;
134 this.nesting_type = nesting_type;
136 check_name ("fullname", name);
138 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
139 throw new InvalidOperationException ("Interface must be declared abstract.");
141 sep_index = name.LastIndexOf('.');
142 if (sep_index != -1) {
143 this.tname = name.Substring (sep_index + 1);
144 this.nspace = name.Substring (0, sep_index);
147 this.nspace = String.Empty;
149 if (interfaces != null) {
150 this.interfaces = new Type[interfaces.Length];
151 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
155 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
156 this.parent = typeof (object);
159 table_idx = mb.get_next_table_index (this, 0x02, true);
160 setup_internal_class (this);
161 fullname = GetFullName ();
164 public override Assembly Assembly {
165 get {return pmodule.Assembly;}
168 public override string AssemblyQualifiedName {
170 return fullname + ", " + Assembly.FullName;
174 public override Type BaseType {
180 public override Type DeclaringType {
181 get { return nesting_type; }
185 public override bool IsSubclassOf (Type c)
201 public override Type UnderlyingSystemType {
204 return created.UnderlyingSystemType;
207 if (underlying_type != null)
208 return underlying_type;
209 throw new InvalidOperationException (
210 "Enumeration type is not defined.");
217 string GetFullName ()
219 if (nesting_type != null)
220 return String.Concat (nesting_type.FullName, "+", tname);
221 if ((nspace != null) && (nspace.Length > 0))
222 return String.Concat (nspace, ".", tname);
226 public override string FullName {
232 public override Guid GUID {
239 public override Module Module {
240 get {return pmodule;}
243 public override string Name {
247 public override string Namespace {
251 public PackingSize PackingSize {
252 get {return packing_size;}
256 get { return class_size; }
259 public override Type ReflectedType {
260 get { return nesting_type; }
263 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
267 throw new ArgumentNullException ("pset");
268 if ((action == SecurityAction.RequestMinimum) ||
269 (action == SecurityAction.RequestOptional) ||
270 (action == SecurityAction.RequestRefuse))
271 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
273 check_not_created ();
275 if (permissions != null) {
276 /* Check duplicate actions */
277 foreach (RefEmitPermissionSet set in permissions)
278 if (set.action == action)
279 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
281 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
282 permissions.CopyTo (new_array, 0);
283 permissions = new_array;
286 permissions = new RefEmitPermissionSet [1];
288 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
289 attrs |= TypeAttributes.HasSecurity;
294 public void AddInterfaceImplementation (Type interfaceType)
296 if (interfaceType == null)
297 throw new ArgumentNullException ("interfaceType");
298 check_not_created ();
300 if (interfaces != null) {
301 // Check for duplicates
302 foreach (Type t in interfaces)
303 if (t == interfaceType)
306 Type[] ifnew = new Type [interfaces.Length + 1];
307 interfaces.CopyTo (ifnew, 0);
308 ifnew [interfaces.Length] = interfaceType;
311 interfaces = new Type [1];
312 interfaces [0] = interfaceType;
316 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
317 CallingConventions callConvention, Type[] types,
318 ParameterModifier[] modifiers)
322 if (created == typeof (object)) {
324 * This happens when building corlib. Calling created.GetConstructor
325 * would return constructors from the real mscorlib, instead of the
332 ConstructorBuilder found = null;
335 foreach (ConstructorBuilder cb in ctors) {
336 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
346 throw new AmbiguousMatchException ();
349 MethodBase[] match = new MethodBase [count];
354 foreach (ConstructorInfo m in ctors) {
355 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
361 binder = Binder.DefaultBinder;
362 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
366 return created.GetConstructor (bindingAttr, binder,
367 callConvention, types, modifiers);
370 public override bool IsDefined (Type attributeType, bool inherit)
373 throw new NotSupportedException ();
375 * MS throws NotSupported here, but we can't because some corlib
376 * classes make calls to IsDefined.
378 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
381 public override object[] GetCustomAttributes(bool inherit)
385 return created.GetCustomAttributes (inherit);
388 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
392 return created.GetCustomAttributes (attributeType, inherit);
395 public TypeBuilder DefineNestedType (string name)
397 return DefineNestedType (name, TypeAttributes.NestedPrivate,
398 pmodule.assemblyb.corlib_object_type, null);
401 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
403 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
406 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
408 return DefineNestedType (name, attr, parent, null);
411 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
412 PackingSize packSize, int typeSize)
414 // Visibility must be NestedXXX
416 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
417 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
418 throw new ArgumentException ("attr", "Bad type flags for nested type.");
420 if (interfaces != null)
421 foreach (Type iface in interfaces)
423 throw new ArgumentNullException ("interfaces");
425 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
426 res.fullname = res.GetFullName ();
427 pmodule.RegisterTypeName (res, res.fullname);
428 if (subtypes != null) {
429 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
430 System.Array.Copy (subtypes, new_types, subtypes.Length);
431 new_types [subtypes.Length] = res;
432 subtypes = new_types;
434 subtypes = new TypeBuilder [1];
441 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
443 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
446 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
448 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
451 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
453 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
457 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
459 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
463 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
465 check_not_created ();
466 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
467 callingConvention, parameterTypes, requiredCustomModifiers,
468 optionalCustomModifiers);
470 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
471 System.Array.Copy (ctors, new_ctors, ctors.Length);
472 new_ctors [ctors.Length] = cb;
475 ctors = new ConstructorBuilder [1];
482 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
484 Type parent_type, old_parent_type;
487 parent_type = parent;
489 parent_type = pmodule.assemblyb.corlib_object_type;
491 old_parent_type = parent_type;
492 parent_type = parent_type.InternalResolve ();
493 /*This avoids corlib to have self references.*/
494 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
495 parent_type = old_parent_type;
497 ConstructorInfo parent_constructor =
498 parent_type.GetConstructor (
499 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
500 null, Type.EmptyTypes, null);
501 if (parent_constructor == null) {
502 throw new NotSupportedException ("Parent does"
503 + " not have a default constructor."
504 + " The default constructor must be"
505 + " explicitly defined.");
508 ConstructorBuilder cb = DefineConstructor (attributes,
509 CallingConventions.Standard, Type.EmptyTypes);
510 ILGenerator ig = cb.GetILGenerator ();
511 ig.Emit (OpCodes.Ldarg_0);
512 ig.Emit (OpCodes.Call, parent_constructor);
513 ig.Emit (OpCodes.Ret);
517 private void append_method (MethodBuilder mb)
519 if (methods != null) {
520 if (methods.Length == num_methods) {
521 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
522 System.Array.Copy (methods, new_methods, num_methods);
523 methods = new_methods;
526 methods = new MethodBuilder [1];
528 methods [num_methods] = mb;
532 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
534 return DefineMethod (name, attributes, CallingConventions.Standard,
535 returnType, parameterTypes);
538 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
540 return DefineMethod (name, attributes, callingConvention, returnType,
541 null, null, parameterTypes, null, null);
544 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
546 check_name ("name", name);
547 check_not_created ();
549 !((attributes & MethodAttributes.Abstract) != 0) ||
550 !((attributes & MethodAttributes.Virtual) != 0)) &&
551 !(((attributes & MethodAttributes.Static) != 0)))
552 throw new ArgumentException ("Interface method must be abstract and virtual.");
554 if (returnType == null)
555 returnType = pmodule.assemblyb.corlib_void_type;
556 MethodBuilder res = new MethodBuilder (this, name, attributes,
557 callingConvention, returnType,
558 returnTypeRequiredCustomModifiers,
559 returnTypeOptionalCustomModifiers, parameterTypes,
560 parameterTypeRequiredCustomModifiers,
561 parameterTypeOptionalCustomModifiers);
566 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
568 return DefinePInvokeMethod (name, dllName, entryName, attributes,
569 callingConvention, returnType, null, null, parameterTypes,
570 null, null, nativeCallConv, nativeCharSet);
573 public MethodBuilder DefinePInvokeMethod (
576 string entryName, MethodAttributes attributes,
577 CallingConventions callingConvention,
579 Type[] returnTypeRequiredCustomModifiers,
580 Type[] returnTypeOptionalCustomModifiers,
581 Type[] parameterTypes,
582 Type[][] parameterTypeRequiredCustomModifiers,
583 Type[][] parameterTypeOptionalCustomModifiers,
584 CallingConvention nativeCallConv,
585 CharSet nativeCharSet)
587 check_name ("name", name);
588 check_name ("dllName", dllName);
589 check_name ("entryName", entryName);
590 if ((attributes & MethodAttributes.Abstract) != 0)
591 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
593 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
594 check_not_created ();
597 = new MethodBuilder (
603 returnTypeRequiredCustomModifiers,
604 returnTypeOptionalCustomModifiers,
606 parameterTypeRequiredCustomModifiers,
607 parameterTypeOptionalCustomModifiers,
616 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
617 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
618 nativeCallConv, nativeCharSet);
621 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
623 return DefineMethod (name, attributes, CallingConventions.Standard);
626 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
628 return DefineMethod (name, attributes, callingConvention, null, null);
631 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
633 if (methodInfoBody == null)
634 throw new ArgumentNullException ("methodInfoBody");
635 if (methodInfoDeclaration == null)
636 throw new ArgumentNullException ("methodInfoDeclaration");
637 check_not_created ();
638 if (methodInfoBody.DeclaringType != this)
639 throw new ArgumentException ("method body must belong to this type");
641 if (methodInfoBody is MethodBuilder) {
642 MethodBuilder mb = (MethodBuilder)methodInfoBody;
643 mb.set_override (methodInfoDeclaration);
647 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
649 return DefineField (fieldName, type, null, null, attributes);
652 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
654 check_name ("fieldName", fieldName);
655 if (type == typeof (void))
656 throw new ArgumentException ("Bad field type in defining field.");
657 check_not_created ();
659 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
660 if (fields != null) {
661 if (fields.Length == num_fields) {
662 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
663 System.Array.Copy (fields, new_fields, num_fields);
666 fields [num_fields] = res;
669 fields = new FieldBuilder [1];
672 create_internal_class (this);
676 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
677 underlying_type = type;
683 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
685 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
689 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
691 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
695 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
697 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
700 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
702 check_name ("name", name);
703 if (parameterTypes != null)
704 foreach (Type param in parameterTypes)
706 throw new ArgumentNullException ("parameterTypes");
707 check_not_created ();
709 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
711 if (properties != null) {
712 Array.Resize (ref properties, properties.Length + 1);
713 properties [properties.Length - 1] = res;
715 properties = new PropertyBuilder [1] { res };
721 public ConstructorBuilder DefineTypeInitializer()
723 return DefineConstructor (MethodAttributes.Public |
724 MethodAttributes.Static | MethodAttributes.SpecialName |
725 MethodAttributes.RTSpecialName, CallingConventions.Standard,
729 [MethodImplAttribute(MethodImplOptions.InternalCall)]
730 private extern Type create_runtime_class (TypeBuilder tb);
732 private bool is_nested_in (Type t)
743 // Return whenever this type has a ctor defined using DefineMethod ()
744 private bool has_ctor_method () {
745 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
747 for (int i = 0; i < num_methods; ++i) {
748 MethodBuilder mb = (MethodBuilder)(methods[i]);
750 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
757 public Type CreateType()
759 /* handle nesting_type */
760 if (createTypeCalled)
763 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
764 SetParent (pmodule.assemblyb.corlib_object_type);
767 create_generic_class ();
769 // Fire TypeResolve events for fields whose type is an unfinished
771 if (fields != null) {
772 foreach (FieldBuilder fb in fields) {
775 Type ft = fb.FieldType;
776 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
777 TypeBuilder tb = (TypeBuilder)ft;
778 if (!tb.is_created) {
779 AppDomain.CurrentDomain.DoTypeResolve (tb);
780 if (!tb.is_created) {
781 // FIXME: We should throw an exception here,
782 // but mcs expects that the type is created
783 // even if the exception is thrown
784 //throw new TypeLoadException ("Could not load type " + tb);
792 // On classes, define a default constructor if not provided
794 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
795 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
796 DefineDefaultConstructor (MethodAttributes.Public);
798 createTypeCalled = true;
800 if ((parent != null) && parent.IsSealed)
801 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
803 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
804 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
806 if (methods != null) {
807 bool is_concrete = !IsAbstract;
808 for (int i = 0; i < num_methods; ++i) {
809 MethodBuilder mb = (MethodBuilder)(methods[i]);
810 if (is_concrete && mb.IsAbstract)
811 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
812 mb.check_override ();
818 foreach (ConstructorBuilder ctor in ctors)
822 created = create_runtime_class (this);
828 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
830 symbolWriter.OpenNamespace (this.Namespace);
832 if (methods != null) {
833 for (int i = 0; i < num_methods; ++i) {
834 MethodBuilder metb = (MethodBuilder) methods[i];
835 metb.GenerateDebugInfo (symbolWriter);
840 foreach (ConstructorBuilder ctor in ctors)
841 ctor.GenerateDebugInfo (symbolWriter);
844 symbolWriter.CloseNamespace ();
846 if (subtypes != null) {
847 for (int i = 0; i < subtypes.Length; ++i)
848 subtypes [i].GenerateDebugInfo (symbolWriter);
853 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
856 return created.GetConstructors (bindingAttr);
858 throw new NotSupportedException ();
861 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
864 return new ConstructorInfo [0];
865 ArrayList l = new ArrayList ();
867 MethodAttributes mattrs;
869 foreach (ConstructorBuilder c in ctors) {
871 mattrs = c.Attributes;
872 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
873 if ((bindingAttr & BindingFlags.Public) != 0)
876 if ((bindingAttr & BindingFlags.NonPublic) != 0)
882 if ((mattrs & MethodAttributes.Static) != 0) {
883 if ((bindingAttr & BindingFlags.Static) != 0)
886 if ((bindingAttr & BindingFlags.Instance) != 0)
893 ConstructorInfo[] result = new ConstructorInfo [l.Count];
898 public override Type GetElementType ()
900 throw new NotSupportedException ();
903 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
906 return created.GetEvent (name, bindingAttr);
909 /* Needed to keep signature compatibility with MS.NET */
910 public override EventInfo[] GetEvents ()
912 return GetEvents (DefaultBindingFlags);
915 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
918 return created.GetEvents (bindingAttr);
919 throw new NotSupportedException ();
922 // This is only used from MonoGenericInst.initialize().
923 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
926 return new EventInfo [0];
927 ArrayList l = new ArrayList ();
929 MethodAttributes mattrs;
932 foreach (EventBuilder eb in events) {
935 EventInfo c = get_event_info (eb);
937 accessor = c.GetAddMethod (true);
938 if (accessor == null)
939 accessor = c.GetRemoveMethod (true);
940 if (accessor == null)
942 mattrs = accessor.Attributes;
943 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
944 if ((bindingAttr & BindingFlags.Public) != 0)
947 if ((bindingAttr & BindingFlags.NonPublic) != 0)
953 if ((mattrs & MethodAttributes.Static) != 0) {
954 if ((bindingAttr & BindingFlags.Static) != 0)
957 if ((bindingAttr & BindingFlags.Instance) != 0)
964 EventInfo[] result = new EventInfo [l.Count];
969 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
972 return created.GetField (name, bindingAttr);
978 FieldAttributes mattrs;
980 foreach (FieldInfo c in fields) {
986 mattrs = c.Attributes;
987 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
988 if ((bindingAttr & BindingFlags.Public) != 0)
991 if ((bindingAttr & BindingFlags.NonPublic) != 0)
997 if ((mattrs & FieldAttributes.Static) != 0) {
998 if ((bindingAttr & BindingFlags.Static) != 0)
1001 if ((bindingAttr & BindingFlags.Instance) != 0)
1011 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1013 if (created != null)
1014 return created.GetFields (bindingAttr);
1017 return new FieldInfo [0];
1018 ArrayList l = new ArrayList ();
1020 FieldAttributes mattrs;
1022 foreach (FieldInfo c in fields) {
1026 mattrs = c.Attributes;
1027 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1028 if ((bindingAttr & BindingFlags.Public) != 0)
1031 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1037 if ((mattrs & FieldAttributes.Static) != 0) {
1038 if ((bindingAttr & BindingFlags.Static) != 0)
1041 if ((bindingAttr & BindingFlags.Instance) != 0)
1048 FieldInfo[] result = new FieldInfo [l.Count];
1053 public override Type GetInterface (string name, bool ignoreCase)
1056 return created.GetInterface (name, ignoreCase);
1059 public override Type[] GetInterfaces ()
1062 return created.GetInterfaces ();
1064 if (interfaces != null) {
1065 Type[] ret = new Type [interfaces.Length];
1066 interfaces.CopyTo (ret, 0);
1069 return Type.EmptyTypes;
1073 public override MemberInfo[] GetMember (string name, MemberTypes type,
1074 BindingFlags bindingAttr)
1077 return created.GetMember (name, type, bindingAttr);
1080 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1083 return created.GetMembers (bindingAttr);
1086 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1088 MethodInfo[] candidates;
1090 MethodAttributes mattrs;
1092 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1093 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1094 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1096 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1098 for (int i = 0; i < parent_methods.Length; i++) {
1099 MethodInfo m = parent_methods [i];
1101 mattrs = m.Attributes;
1103 if (m.IsStatic && !flatten)
1106 switch (mattrs & MethodAttributes.MemberAccessMask) {
1107 case MethodAttributes.Public:
1108 match = (bindingAttr & BindingFlags.Public) != 0;
1110 case MethodAttributes.Assembly:
1111 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1113 case MethodAttributes.Private:
1117 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1122 parent_candidates.Add (m);
1125 if (methods == null) {
1126 candidates = new MethodInfo [parent_candidates.Count];
1127 parent_candidates.CopyTo (candidates);
1129 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1130 parent_candidates.CopyTo (candidates, 0);
1131 methods.CopyTo (candidates, parent_candidates.Count);
1135 candidates = methods;
1137 if (candidates == null)
1138 return new MethodInfo [0];
1140 ArrayList l = new ArrayList ();
1142 foreach (MethodInfo c in candidates) {
1146 if (String.Compare (c.Name, name, ignoreCase) != 0)
1150 mattrs = c.Attributes;
1151 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1152 if ((bindingAttr & BindingFlags.Public) != 0)
1155 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1161 if ((mattrs & MethodAttributes.Static) != 0) {
1162 if ((bindingAttr & BindingFlags.Static) != 0)
1165 if ((bindingAttr & BindingFlags.Instance) != 0)
1173 MethodInfo[] result = new MethodInfo [l.Count];
1178 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1180 return GetMethodsByName (null, bindingAttr, false, this);
1183 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1185 CallingConventions callConvention,
1186 Type[] types, ParameterModifier[] modifiers)
1190 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
1191 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
1192 MethodInfo found = null;
1194 int typesLen = (types != null) ? types.Length : 0;
1197 foreach (MethodInfo m in methods) {
1198 // Under MS.NET, Standard|HasThis matches Standard...
1199 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1208 if (count == 1 && typesLen == 0)
1211 match = new MethodBase [count];
1216 foreach (MethodInfo m in methods) {
1217 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1219 match [count++] = m;
1224 return (MethodInfo) Binder.FindMostDerivedMatch (match);
1227 binder = Binder.DefaultBinder;
1229 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
1232 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1236 if (subtypes == null)
1239 foreach (TypeBuilder t in subtypes) {
1242 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1243 if ((bindingAttr & BindingFlags.Public) == 0)
1246 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1256 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1259 throw new NotSupportedException ();
1262 ArrayList result = new ArrayList ();
1264 if (subtypes == null)
1265 return Type.EmptyTypes;
1266 foreach (TypeBuilder t in subtypes) {
1268 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1269 if ((bindingAttr & BindingFlags.Public) != 0)
1272 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1279 Type[] r = new Type [result.Count];
1284 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1287 return created.GetProperties (bindingAttr);
1289 if (properties == null)
1290 return new PropertyInfo [0];
1291 ArrayList l = new ArrayList ();
1293 MethodAttributes mattrs;
1294 MethodInfo accessor;
1296 foreach (PropertyInfo c in properties) {
1298 accessor = c.GetGetMethod (true);
1299 if (accessor == null)
1300 accessor = c.GetSetMethod (true);
1301 if (accessor == null)
1303 mattrs = accessor.Attributes;
1304 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1305 if ((bindingAttr & BindingFlags.Public) != 0)
1308 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1314 if ((mattrs & MethodAttributes.Static) != 0) {
1315 if ((bindingAttr & BindingFlags.Static) != 0)
1318 if ((bindingAttr & BindingFlags.Instance) != 0)
1325 PropertyInfo[] result = new PropertyInfo [l.Count];
1330 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1332 throw not_supported ();
1335 protected override bool HasElementTypeImpl ()
1337 // a TypeBuilder can never represent an array, pointer
1341 return created.HasElementType;
1344 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1347 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1350 protected override bool IsArrayImpl ()
1352 return false; /*A TypeBuilder never represents a non typedef type.*/
1355 protected override bool IsByRefImpl ()
1357 return false; /*A TypeBuilder never represents a non typedef type.*/
1360 protected override bool IsCOMObjectImpl ()
1362 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1365 protected override bool IsPointerImpl ()
1367 return false; /*A TypeBuilder never represents a non typedef type.*/
1370 protected override bool IsPrimitiveImpl ()
1376 // FIXME: I doubt just removing this still works.
1377 protected override bool IsValueTypeImpl ()
1379 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1381 Type parent_type = parent;
1382 while (parent_type != null) {
1383 if (parent_type == pmodule.assemblyb.corlib_value_type)
1385 parent_type = parent_type.BaseType;
1390 public override Type MakeArrayType ()
1392 return new ArrayType (this, 0);
1395 public override Type MakeArrayType (int rank)
1398 throw new IndexOutOfRangeException ();
1399 return new ArrayType (this, rank);
1402 public override Type MakeByRefType ()
1404 return new ByRefType (this);
1407 public override Type MakeGenericType (params Type [] typeArguments)
1409 //return base.MakeGenericType (typeArguments);
1411 if (!IsGenericTypeDefinition)
1412 throw new InvalidOperationException ("not a generic type definition");
1413 if (typeArguments == null)
1414 throw new ArgumentNullException ("typeArguments");
1416 if (generic_params.Length != typeArguments.Length)
1417 throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", generic_params.Length, typeArguments.Length), "typeArguments");
1419 foreach (Type t in typeArguments) {
1421 throw new ArgumentNullException ("typeArguments");
1424 Type[] copy = new Type [typeArguments.Length];
1425 typeArguments.CopyTo (copy, 0);
1426 return pmodule.assemblyb.MakeGenericType (this, copy);
1429 public override Type MakePointerType ()
1431 return new PointerType (this);
1434 public override RuntimeTypeHandle TypeHandle {
1437 return created.TypeHandle;
1441 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1443 if (customBuilder == null)
1444 throw new ArgumentNullException ("customBuilder");
1446 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1447 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1448 byte[] data = customBuilder.Data;
1449 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1450 layout_kind = (int)data [2];
1451 layout_kind |= ((int)data [3]) << 8;
1452 attrs &= ~TypeAttributes.LayoutMask;
1453 switch ((LayoutKind)layout_kind) {
1454 case LayoutKind.Auto:
1455 attrs |= TypeAttributes.AutoLayout;
1457 case LayoutKind.Explicit:
1458 attrs |= TypeAttributes.ExplicitLayout;
1460 case LayoutKind.Sequential:
1461 attrs |= TypeAttributes.SequentialLayout;
1464 // we should ignore it since it can be any value anyway...
1465 throw new Exception ("Error in customattr");
1468 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1470 if (ctor_type.FullName == "System.Int16")
1472 int nnamed = (int)data [pos++];
1473 nnamed |= ((int)data [pos++]) << 8;
1474 for (int i = 0; i < nnamed; ++i) {
1475 //byte named_type = data [pos++];
1477 byte type = data [pos++];
1482 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1483 //string named_typename =
1484 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1486 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1487 // See related code/FIXME in mono/mono/metadata/reflection.c
1490 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1491 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1493 /* all the fields are integers in StructLayout */
1494 int value = (int)data [pos++];
1495 value |= ((int)data [pos++]) << 8;
1496 value |= ((int)data [pos++]) << 16;
1497 value |= ((int)data [pos++]) << 24;
1498 switch (named_name) {
1500 switch ((CharSet)value) {
1503 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1505 case CharSet.Unicode:
1506 attrs &= ~TypeAttributes.AutoClass;
1507 attrs |= TypeAttributes.UnicodeClass;
1510 attrs &= ~TypeAttributes.UnicodeClass;
1511 attrs |= TypeAttributes.AutoClass;
1514 break; // error out...
1518 packing_size = (PackingSize)value;
1524 break; // error out...
1528 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1529 attrs |= TypeAttributes.SpecialName;
1531 } else if (attrname == "System.SerializableAttribute") {
1532 attrs |= TypeAttributes.Serializable;
1534 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1535 attrs |= TypeAttributes.Import;
1537 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1538 attrs |= TypeAttributes.HasSecurity;
1541 if (cattrs != null) {
1542 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1543 cattrs.CopyTo (new_array, 0);
1544 new_array [cattrs.Length] = customBuilder;
1547 cattrs = new CustomAttributeBuilder [1];
1548 cattrs [0] = customBuilder;
1553 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1555 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1558 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1560 check_name ("name", name);
1561 if (eventtype == null)
1562 throw new ArgumentNullException ("type");
1563 check_not_created ();
1565 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1566 if (events != null) {
1567 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1568 System.Array.Copy (events, new_events, events.Length);
1569 new_events [events.Length] = res;
1570 events = new_events;
1572 events = new EventBuilder [1];
1578 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1580 throw new ArgumentNullException ("data");
1582 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1583 res.SetRVAData (data);
1587 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1590 throw new ArgumentNullException ("name");
1591 if (name.Length == 0)
1592 throw new ArgumentException ("Empty name is not legal", "name");
1593 if ((size <= 0) || (size > 0x3f0000))
1594 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1595 check_not_created ();
1597 string typeName = "$ArrayType$" + size;
1598 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1599 if (datablobtype == null) {
1600 TypeBuilder tb = DefineNestedType (typeName,
1601 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1602 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1606 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1609 public TypeToken TypeToken {
1611 return new TypeToken (0x02000000 | table_idx);
1615 public void SetParent (Type parent)
1617 check_not_created ();
1619 if (parent == null) {
1620 if ((attrs & TypeAttributes.Interface) != 0) {
1621 if ((attrs & TypeAttributes.Abstract) == 0)
1622 throw new InvalidOperationException ("Interface must be declared abstract.");
1625 this.parent = typeof (object);
1628 this.parent = parent;
1631 // will just set the parent-related bits if called a second time
1632 setup_internal_class (this);
1635 internal int get_next_table_index (object obj, int table, bool inc) {
1636 return pmodule.get_next_table_index (obj, table, inc);
1640 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1642 if (created == null)
1643 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1645 return created.GetInterfaceMap (interfaceType);
1648 internal override Type InternalResolve ()
1654 internal bool is_created {
1656 return createTypeCalled;
1660 private Exception not_supported ()
1662 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1665 private void check_not_created ()
1668 throw new InvalidOperationException ("Unable to change after type has been created.");
1671 private void check_created ()
1674 throw not_supported ();
1677 private void check_name (string argName, string name)
1680 throw new ArgumentNullException (argName);
1681 if (name.Length == 0)
1682 throw new ArgumentException ("Empty name is not legal", argName);
1683 if (name [0] == ((char)0))
1684 throw new ArgumentException ("Illegal name", argName);
1687 public override String ToString ()
1693 public override bool IsAssignableFrom (Type c)
1695 return base.IsAssignableFrom (c);
1698 [MonoTODO ("arrays")]
1699 internal bool IsAssignableTo (Type c)
1704 if (c.IsInterface) {
1705 if (parent != null && is_created) {
1706 if (c.IsAssignableFrom (parent))
1710 if (interfaces == null)
1712 foreach (Type t in interfaces)
1713 if (c.IsAssignableFrom (t))
1720 return c == typeof (object);
1722 return c.IsAssignableFrom (parent);
1725 public bool IsCreated ()
1730 public override Type[] GetGenericArguments ()
1732 if (generic_params == null)
1734 Type[] args = new Type [generic_params.Length];
1735 generic_params.CopyTo (args, 0);
1739 public override Type GetGenericTypeDefinition ()
1741 if (generic_params == null)
1742 throw new InvalidOperationException ("Type is not generic");
1746 public override bool ContainsGenericParameters {
1748 return generic_params != null;
1752 public extern override bool IsGenericParameter {
1753 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1757 public override GenericParameterAttributes GenericParameterAttributes {
1758 get { return GenericParameterAttributes.None; }
1761 public override bool IsGenericTypeDefinition {
1763 return generic_params != null;
1767 public override bool IsGenericType {
1768 get { return IsGenericTypeDefinition; }
1772 public override int GenericParameterPosition {
1778 public override MethodBase DeclaringMethod {
1784 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1787 throw new ArgumentNullException ("names");
1788 if (names.Length == 0)
1789 throw new ArgumentException ("names");
1791 setup_generic_class ();
1793 generic_params = new GenericTypeParameterBuilder [names.Length];
1794 for (int i = 0; i < names.Length; i++) {
1795 string item = names [i];
1797 throw new ArgumentNullException ("names");
1798 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1801 return generic_params;
1804 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1806 /*FIXME I would expect the same checks of GetMethod here*/
1808 throw new ArgumentException ("Type is not generic", "type");
1810 if (!type.IsGenericType)
1811 throw new ArgumentException ("Type is not a generic type", "type");
1813 if (type.IsGenericTypeDefinition)
1814 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1816 if (constructor == null)
1817 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1819 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1820 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1821 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1822 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1824 ConstructorInfo res = type.GetConstructor (constructor);
1826 throw new ArgumentException ("constructor not found");
1831 static bool IsValidGetMethodType (Type type)
1833 if (type is TypeBuilder || type is MonoGenericClass)
1835 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1836 if (type.Module is ModuleBuilder)
1838 if (type.IsGenericParameter)
1841 Type[] inst = type.GetGenericArguments ();
1844 for (int i = 0; i < inst.Length; ++i) {
1845 if (IsValidGetMethodType (inst [i]))
1851 public static MethodInfo GetMethod (Type type, MethodInfo method)
1853 if (!IsValidGetMethodType (type))
1854 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1856 if (type is TypeBuilder && type.ContainsGenericParameters)
1857 type = type.MakeGenericType (type.GetGenericArguments ());
1859 if (!type.IsGenericType)
1860 throw new ArgumentException ("type is not a generic type", "type");
1862 if (!method.DeclaringType.IsGenericTypeDefinition)
1863 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1864 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1865 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1867 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1869 MethodInfo res = type.GetMethod (method);
1871 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1876 public static FieldInfo GetField (Type type, FieldInfo field)
1878 if (!type.IsGenericType)
1879 throw new ArgumentException ("Type is not a generic type", "type");
1881 if (type.IsGenericTypeDefinition)
1882 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1884 if (field is FieldOnTypeBuilderInst)
1885 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1887 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1888 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1890 FieldInfo res = type.GetField (field);
1892 throw new System.Exception ("field not found");
1897 internal TypeCode GetTypeCodeInternal () {
1898 if (parent == pmodule.assemblyb.corlib_enum_type) {
1899 for (int i = 0; i < num_fields; ++i) {
1900 FieldBuilder f = fields [i];
1902 return Type.GetTypeCode (f.FieldType);
1904 throw new InvalidOperationException ("Enum basetype field not defined");
1906 return Type.GetTypeCodeInternal (this);
1911 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1913 throw new NotImplementedException ();
1916 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1918 throw new NotImplementedException ();
1921 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1923 throw new NotImplementedException ();
1926 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1928 throw new NotImplementedException ();
1931 internal override bool IsUserType {
1938 public override bool IsConstructedGenericType {
1939 get { return false; }
1942 public override bool IsAssignableFrom (TypeInfo typeInfo)
1944 return base.IsAssignableFrom (typeInfo);