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 : Type, _TypeBuilder
55 #pragma warning disable 169
56 #region Sync with reflection.h
58 private string nspace;
60 private Type nesting_type;
61 internal Type[] interfaces;
62 internal int num_methods;
63 internal MethodBuilder[] methods;
64 internal ConstructorBuilder[] ctors;
65 internal PropertyBuilder[] properties;
66 internal int num_fields;
67 internal FieldBuilder[] fields;
68 internal EventBuilder[] events;
69 private CustomAttributeBuilder[] cattrs;
70 internal TypeBuilder[] subtypes;
71 internal TypeAttributes attrs;
72 private int table_idx;
73 private ModuleBuilder pmodule;
74 private int class_size;
75 private PackingSize packing_size;
76 private IntPtr generic_container;
77 private GenericTypeParameterBuilder[] generic_params;
78 private RefEmitPermissionSet[] permissions;
81 #pragma warning restore 169
84 bool createTypeCalled;
85 private Type underlying_type;
87 public const int UnspecifiedTypeSize = 0;
89 protected override TypeAttributes GetAttributeFlagsImpl ()
94 [MethodImplAttribute(MethodImplOptions.InternalCall)]
95 private extern void setup_internal_class (TypeBuilder tb);
97 [MethodImplAttribute(MethodImplOptions.InternalCall)]
98 private extern void create_internal_class (TypeBuilder tb);
100 [MethodImplAttribute(MethodImplOptions.InternalCall)]
101 private extern void setup_generic_class ();
103 [MethodImplAttribute(MethodImplOptions.InternalCall)]
104 private extern void create_generic_class ();
106 [MethodImplAttribute(MethodImplOptions.InternalCall)]
107 private extern EventInfo get_event_info (EventBuilder eb);
109 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
113 this.class_size = UnspecifiedTypeSize;
114 this.table_idx = table_idx;
115 fullname = this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
116 this.nspace = String.Empty;
118 setup_internal_class (this);
121 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
124 this.parent = parent;
126 this.class_size = type_size;
127 this.packing_size = packing_size;
128 this.nesting_type = nesting_type;
130 check_name ("fullname", name);
132 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
133 throw new InvalidOperationException ("Interface must be declared abstract.");
135 sep_index = name.LastIndexOf('.');
136 if (sep_index != -1) {
137 this.tname = name.Substring (sep_index + 1);
138 this.nspace = name.Substring (0, sep_index);
141 this.nspace = String.Empty;
143 if (interfaces != null) {
144 this.interfaces = new Type[interfaces.Length];
145 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
149 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
150 this.parent = typeof (object);
153 table_idx = mb.get_next_table_index (this, 0x02, true);
154 setup_internal_class (this);
155 fullname = GetFullName ();
158 public override Assembly Assembly {
159 get {return pmodule.Assembly;}
162 public override string AssemblyQualifiedName {
164 return fullname + ", " + Assembly.FullName;
168 public override Type BaseType {
174 public override Type DeclaringType {
175 get { return nesting_type; }
179 public override bool IsSubclassOf (Type c)
195 public override Type UnderlyingSystemType {
198 return created.UnderlyingSystemType;
201 if (underlying_type != null)
202 return underlying_type;
203 throw new InvalidOperationException (
204 "Enumeration type is not defined.");
211 string GetFullName ()
213 if (nesting_type != null)
214 return String.Concat (nesting_type.FullName, "+", tname);
215 if ((nspace != null) && (nspace.Length > 0))
216 return String.Concat (nspace, ".", tname);
220 public override string FullName {
226 public override Guid GUID {
233 public override Module Module {
234 get {return pmodule;}
237 public override string Name {
241 public override string Namespace {
245 public PackingSize PackingSize {
246 get {return packing_size;}
250 get { return class_size; }
253 public override Type ReflectedType {
254 get { return nesting_type; }
257 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
261 throw new ArgumentNullException ("pset");
262 if ((action == SecurityAction.RequestMinimum) ||
263 (action == SecurityAction.RequestOptional) ||
264 (action == SecurityAction.RequestRefuse))
265 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
267 check_not_created ();
269 if (permissions != null) {
270 /* Check duplicate actions */
271 foreach (RefEmitPermissionSet set in permissions)
272 if (set.action == action)
273 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
275 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
276 permissions.CopyTo (new_array, 0);
277 permissions = new_array;
280 permissions = new RefEmitPermissionSet [1];
282 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
283 attrs |= TypeAttributes.HasSecurity;
288 public void AddInterfaceImplementation (Type interfaceType)
290 if (interfaceType == null)
291 throw new ArgumentNullException ("interfaceType");
292 check_not_created ();
294 if (interfaces != null) {
295 // Check for duplicates
296 foreach (Type t in interfaces)
297 if (t == interfaceType)
300 Type[] ifnew = new Type [interfaces.Length + 1];
301 interfaces.CopyTo (ifnew, 0);
302 ifnew [interfaces.Length] = interfaceType;
305 interfaces = new Type [1];
306 interfaces [0] = interfaceType;
310 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
311 CallingConventions callConvention, Type[] types,
312 ParameterModifier[] modifiers)
316 if (created == typeof (object)) {
318 * This happens when building corlib. Calling created.GetConstructor
319 * would return constructors from the real mscorlib, instead of the
326 ConstructorBuilder found = null;
329 foreach (ConstructorBuilder cb in ctors) {
330 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
340 throw new AmbiguousMatchException ();
343 MethodBase[] match = new MethodBase [count];
348 foreach (ConstructorInfo m in ctors) {
349 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
355 binder = Binder.DefaultBinder;
356 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
360 return created.GetConstructor (bindingAttr, binder,
361 callConvention, types, modifiers);
364 public override bool IsDefined (Type attributeType, bool inherit)
367 throw new NotSupportedException ();
369 * MS throws NotSupported here, but we can't because some corlib
370 * classes make calls to IsDefined.
372 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
375 public override object[] GetCustomAttributes(bool inherit)
379 return created.GetCustomAttributes (inherit);
382 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
386 return created.GetCustomAttributes (attributeType, inherit);
389 public TypeBuilder DefineNestedType (string name)
391 return DefineNestedType (name, TypeAttributes.NestedPrivate,
392 pmodule.assemblyb.corlib_object_type, null);
395 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
397 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
400 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
402 return DefineNestedType (name, attr, parent, null);
405 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
406 PackingSize packSize, int typeSize)
408 // Visibility must be NestedXXX
410 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
411 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
412 throw new ArgumentException ("attr", "Bad type flags for nested type.");
414 if (interfaces != null)
415 foreach (Type iface in interfaces)
417 throw new ArgumentNullException ("interfaces");
419 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
420 res.fullname = res.GetFullName ();
421 pmodule.RegisterTypeName (res, res.fullname);
422 if (subtypes != null) {
423 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
424 System.Array.Copy (subtypes, new_types, subtypes.Length);
425 new_types [subtypes.Length] = res;
426 subtypes = new_types;
428 subtypes = new TypeBuilder [1];
435 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
437 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
440 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
442 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
445 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
447 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
451 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
453 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
457 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
459 check_not_created ();
460 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
461 callingConvention, parameterTypes, requiredCustomModifiers,
462 optionalCustomModifiers);
464 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
465 System.Array.Copy (ctors, new_ctors, ctors.Length);
466 new_ctors [ctors.Length] = cb;
469 ctors = new ConstructorBuilder [1];
476 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
478 Type parent_type, old_parent_type;
481 parent_type = parent;
483 parent_type = pmodule.assemblyb.corlib_object_type;
485 old_parent_type = parent_type;
486 parent_type = parent_type.InternalResolve ();
487 /*This avoids corlib to have self references.*/
488 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
489 parent_type = old_parent_type;
491 ConstructorInfo parent_constructor =
492 parent_type.GetConstructor (
493 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
494 null, Type.EmptyTypes, null);
495 if (parent_constructor == null) {
496 throw new NotSupportedException ("Parent does"
497 + " not have a default constructor."
498 + " The default constructor must be"
499 + " explicitly defined.");
502 ConstructorBuilder cb = DefineConstructor (attributes,
503 CallingConventions.Standard, Type.EmptyTypes);
504 ILGenerator ig = cb.GetILGenerator ();
505 ig.Emit (OpCodes.Ldarg_0);
506 ig.Emit (OpCodes.Call, parent_constructor);
507 ig.Emit (OpCodes.Ret);
511 private void append_method (MethodBuilder mb)
513 if (methods != null) {
514 if (methods.Length == num_methods) {
515 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
516 System.Array.Copy (methods, new_methods, num_methods);
517 methods = new_methods;
520 methods = new MethodBuilder [1];
522 methods [num_methods] = mb;
526 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
528 return DefineMethod (name, attributes, CallingConventions.Standard,
529 returnType, parameterTypes);
532 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
534 return DefineMethod (name, attributes, callingConvention, returnType,
535 null, null, parameterTypes, null, null);
538 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
540 check_name ("name", name);
541 check_not_created ();
543 !((attributes & MethodAttributes.Abstract) != 0) ||
544 !((attributes & MethodAttributes.Virtual) != 0)) &&
545 !(((attributes & MethodAttributes.Static) != 0)))
546 throw new ArgumentException ("Interface method must be abstract and virtual.");
548 if (returnType == null)
549 returnType = pmodule.assemblyb.corlib_void_type;
550 MethodBuilder res = new MethodBuilder (this, name, attributes,
551 callingConvention, returnType,
552 returnTypeRequiredCustomModifiers,
553 returnTypeOptionalCustomModifiers, parameterTypes,
554 parameterTypeRequiredCustomModifiers,
555 parameterTypeOptionalCustomModifiers);
560 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
562 return DefinePInvokeMethod (name, dllName, entryName, attributes,
563 callingConvention, returnType, null, null, parameterTypes,
564 null, null, nativeCallConv, nativeCharSet);
567 public MethodBuilder DefinePInvokeMethod (
570 string entryName, MethodAttributes attributes,
571 CallingConventions callingConvention,
573 Type[] returnTypeRequiredCustomModifiers,
574 Type[] returnTypeOptionalCustomModifiers,
575 Type[] parameterTypes,
576 Type[][] parameterTypeRequiredCustomModifiers,
577 Type[][] parameterTypeOptionalCustomModifiers,
578 CallingConvention nativeCallConv,
579 CharSet nativeCharSet)
581 check_name ("name", name);
582 check_name ("dllName", dllName);
583 check_name ("entryName", entryName);
584 if ((attributes & MethodAttributes.Abstract) != 0)
585 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
587 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
588 check_not_created ();
591 = new MethodBuilder (
597 returnTypeRequiredCustomModifiers,
598 returnTypeOptionalCustomModifiers,
600 parameterTypeRequiredCustomModifiers,
601 parameterTypeOptionalCustomModifiers,
610 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
611 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
612 nativeCallConv, nativeCharSet);
615 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
617 return DefineMethod (name, attributes, CallingConventions.Standard);
620 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
622 return DefineMethod (name, attributes, callingConvention, null, null);
625 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
627 if (methodInfoBody == null)
628 throw new ArgumentNullException ("methodInfoBody");
629 if (methodInfoDeclaration == null)
630 throw new ArgumentNullException ("methodInfoDeclaration");
631 check_not_created ();
632 if (methodInfoBody.DeclaringType != this)
633 throw new ArgumentException ("method body must belong to this type");
635 if (methodInfoBody is MethodBuilder) {
636 MethodBuilder mb = (MethodBuilder)methodInfoBody;
637 mb.set_override (methodInfoDeclaration);
641 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
643 return DefineField (fieldName, type, null, null, attributes);
646 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
648 check_name ("fieldName", fieldName);
649 if (type == typeof (void))
650 throw new ArgumentException ("Bad field type in defining field.");
651 check_not_created ();
653 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
654 if (fields != null) {
655 if (fields.Length == num_fields) {
656 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
657 System.Array.Copy (fields, new_fields, num_fields);
660 fields [num_fields] = res;
663 fields = new FieldBuilder [1];
666 create_internal_class (this);
670 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
671 underlying_type = type;
677 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
679 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
683 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
685 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
689 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
691 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
694 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
696 check_name ("name", name);
697 if (parameterTypes != null)
698 foreach (Type param in parameterTypes)
700 throw new ArgumentNullException ("parameterTypes");
701 check_not_created ();
703 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
705 if (properties != null) {
706 Array.Resize (ref properties, properties.Length + 1);
707 properties [properties.Length - 1] = res;
709 properties = new PropertyBuilder [1] { res };
715 public ConstructorBuilder DefineTypeInitializer()
717 return DefineConstructor (MethodAttributes.Public |
718 MethodAttributes.Static | MethodAttributes.SpecialName |
719 MethodAttributes.RTSpecialName, CallingConventions.Standard,
723 [MethodImplAttribute(MethodImplOptions.InternalCall)]
724 private extern Type create_runtime_class (TypeBuilder tb);
726 private bool is_nested_in (Type t)
737 // Return whenever this type has a ctor defined using DefineMethod ()
738 private bool has_ctor_method () {
739 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
741 for (int i = 0; i < num_methods; ++i) {
742 MethodBuilder mb = (MethodBuilder)(methods[i]);
744 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
751 public Type CreateType()
753 /* handle nesting_type */
754 if (createTypeCalled)
757 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
758 SetParent (pmodule.assemblyb.corlib_object_type);
761 create_generic_class ();
763 // Fire TypeResolve events for fields whose type is an unfinished
765 if (fields != null) {
766 foreach (FieldBuilder fb in fields) {
769 Type ft = fb.FieldType;
770 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
771 TypeBuilder tb = (TypeBuilder)ft;
772 if (!tb.is_created) {
773 AppDomain.CurrentDomain.DoTypeResolve (tb);
774 if (!tb.is_created) {
775 // FIXME: We should throw an exception here,
776 // but mcs expects that the type is created
777 // even if the exception is thrown
778 //throw new TypeLoadException ("Could not load type " + tb);
786 // On classes, define a default constructor if not provided
788 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
789 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
790 DefineDefaultConstructor (MethodAttributes.Public);
792 createTypeCalled = true;
794 if ((parent != null) && parent.IsSealed)
795 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
797 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
798 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
800 if (methods != null) {
801 bool is_concrete = !IsAbstract;
802 for (int i = 0; i < num_methods; ++i) {
803 MethodBuilder mb = (MethodBuilder)(methods[i]);
804 if (is_concrete && mb.IsAbstract)
805 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
806 mb.check_override ();
812 foreach (ConstructorBuilder ctor in ctors)
816 created = create_runtime_class (this);
822 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
824 symbolWriter.OpenNamespace (this.Namespace);
826 if (methods != null) {
827 for (int i = 0; i < num_methods; ++i) {
828 MethodBuilder metb = (MethodBuilder) methods[i];
829 metb.GenerateDebugInfo (symbolWriter);
834 foreach (ConstructorBuilder ctor in ctors)
835 ctor.GenerateDebugInfo (symbolWriter);
838 symbolWriter.CloseNamespace ();
840 if (subtypes != null) {
841 for (int i = 0; i < subtypes.Length; ++i)
842 subtypes [i].GenerateDebugInfo (symbolWriter);
847 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
850 return created.GetConstructors (bindingAttr);
852 throw new NotSupportedException ();
855 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
858 return new ConstructorInfo [0];
859 ArrayList l = new ArrayList ();
861 MethodAttributes mattrs;
863 foreach (ConstructorBuilder c in ctors) {
865 mattrs = c.Attributes;
866 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
867 if ((bindingAttr & BindingFlags.Public) != 0)
870 if ((bindingAttr & BindingFlags.NonPublic) != 0)
876 if ((mattrs & MethodAttributes.Static) != 0) {
877 if ((bindingAttr & BindingFlags.Static) != 0)
880 if ((bindingAttr & BindingFlags.Instance) != 0)
887 ConstructorInfo[] result = new ConstructorInfo [l.Count];
892 public override Type GetElementType ()
894 throw new NotSupportedException ();
897 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
900 return created.GetEvent (name, bindingAttr);
903 /* Needed to keep signature compatibility with MS.NET */
904 public override EventInfo[] GetEvents ()
906 return GetEvents (DefaultBindingFlags);
909 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
912 return created.GetEvents (bindingAttr);
913 throw new NotSupportedException ();
916 // This is only used from MonoGenericInst.initialize().
917 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
920 return new EventInfo [0];
921 ArrayList l = new ArrayList ();
923 MethodAttributes mattrs;
926 foreach (EventBuilder eb in events) {
929 EventInfo c = get_event_info (eb);
931 accessor = c.GetAddMethod (true);
932 if (accessor == null)
933 accessor = c.GetRemoveMethod (true);
934 if (accessor == null)
936 mattrs = accessor.Attributes;
937 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
938 if ((bindingAttr & BindingFlags.Public) != 0)
941 if ((bindingAttr & BindingFlags.NonPublic) != 0)
947 if ((mattrs & MethodAttributes.Static) != 0) {
948 if ((bindingAttr & BindingFlags.Static) != 0)
951 if ((bindingAttr & BindingFlags.Instance) != 0)
958 EventInfo[] result = new EventInfo [l.Count];
963 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
966 return created.GetField (name, bindingAttr);
972 FieldAttributes mattrs;
974 foreach (FieldInfo c in fields) {
980 mattrs = c.Attributes;
981 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
982 if ((bindingAttr & BindingFlags.Public) != 0)
985 if ((bindingAttr & BindingFlags.NonPublic) != 0)
991 if ((mattrs & FieldAttributes.Static) != 0) {
992 if ((bindingAttr & BindingFlags.Static) != 0)
995 if ((bindingAttr & BindingFlags.Instance) != 0)
1005 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1007 if (created != null)
1008 return created.GetFields (bindingAttr);
1011 return new FieldInfo [0];
1012 ArrayList l = new ArrayList ();
1014 FieldAttributes mattrs;
1016 foreach (FieldInfo c in fields) {
1020 mattrs = c.Attributes;
1021 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1022 if ((bindingAttr & BindingFlags.Public) != 0)
1025 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1031 if ((mattrs & FieldAttributes.Static) != 0) {
1032 if ((bindingAttr & BindingFlags.Static) != 0)
1035 if ((bindingAttr & BindingFlags.Instance) != 0)
1042 FieldInfo[] result = new FieldInfo [l.Count];
1047 public override Type GetInterface (string name, bool ignoreCase)
1050 return created.GetInterface (name, ignoreCase);
1053 public override Type[] GetInterfaces ()
1056 return created.GetInterfaces ();
1058 if (interfaces != null) {
1059 Type[] ret = new Type [interfaces.Length];
1060 interfaces.CopyTo (ret, 0);
1063 return Type.EmptyTypes;
1067 public override MemberInfo[] GetMember (string name, MemberTypes type,
1068 BindingFlags bindingAttr)
1071 return created.GetMember (name, type, bindingAttr);
1074 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1077 return created.GetMembers (bindingAttr);
1080 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1082 MethodInfo[] candidates;
1084 MethodAttributes mattrs;
1086 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1087 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1088 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1090 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1092 for (int i = 0; i < parent_methods.Length; i++) {
1093 MethodInfo m = parent_methods [i];
1095 mattrs = m.Attributes;
1097 if (m.IsStatic && !flatten)
1100 switch (mattrs & MethodAttributes.MemberAccessMask) {
1101 case MethodAttributes.Public:
1102 match = (bindingAttr & BindingFlags.Public) != 0;
1104 case MethodAttributes.Assembly:
1105 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1107 case MethodAttributes.Private:
1111 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1116 parent_candidates.Add (m);
1119 if (methods == null) {
1120 candidates = new MethodInfo [parent_candidates.Count];
1121 parent_candidates.CopyTo (candidates);
1123 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1124 parent_candidates.CopyTo (candidates, 0);
1125 methods.CopyTo (candidates, parent_candidates.Count);
1129 candidates = methods;
1131 if (candidates == null)
1132 return new MethodInfo [0];
1134 ArrayList l = new ArrayList ();
1136 foreach (MethodInfo c in candidates) {
1140 if (String.Compare (c.Name, name, ignoreCase) != 0)
1144 mattrs = c.Attributes;
1145 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1146 if ((bindingAttr & BindingFlags.Public) != 0)
1149 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1155 if ((mattrs & MethodAttributes.Static) != 0) {
1156 if ((bindingAttr & BindingFlags.Static) != 0)
1159 if ((bindingAttr & BindingFlags.Instance) != 0)
1167 MethodInfo[] result = new MethodInfo [l.Count];
1172 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1174 return GetMethodsByName (null, bindingAttr, false, this);
1177 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1179 CallingConventions callConvention,
1180 Type[] types, ParameterModifier[] modifiers)
1184 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
1185 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
1186 MethodInfo found = null;
1188 int typesLen = (types != null) ? types.Length : 0;
1191 foreach (MethodInfo m in methods) {
1192 // Under MS.NET, Standard|HasThis matches Standard...
1193 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1202 if (count == 1 && typesLen == 0)
1205 match = new MethodBase [count];
1210 foreach (MethodInfo m in methods) {
1211 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1213 match [count++] = m;
1218 return (MethodInfo) Binder.FindMostDerivedMatch (match);
1221 binder = Binder.DefaultBinder;
1223 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
1226 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1230 if (subtypes == null)
1233 foreach (TypeBuilder t in subtypes) {
1236 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1237 if ((bindingAttr & BindingFlags.Public) == 0)
1240 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1250 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1253 throw new NotSupportedException ();
1256 ArrayList result = new ArrayList ();
1258 if (subtypes == null)
1259 return Type.EmptyTypes;
1260 foreach (TypeBuilder t in subtypes) {
1262 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1263 if ((bindingAttr & BindingFlags.Public) != 0)
1266 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1273 Type[] r = new Type [result.Count];
1278 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1281 return created.GetProperties (bindingAttr);
1283 if (properties == null)
1284 return new PropertyInfo [0];
1285 ArrayList l = new ArrayList ();
1287 MethodAttributes mattrs;
1288 MethodInfo accessor;
1290 foreach (PropertyInfo c in properties) {
1292 accessor = c.GetGetMethod (true);
1293 if (accessor == null)
1294 accessor = c.GetSetMethod (true);
1295 if (accessor == null)
1297 mattrs = accessor.Attributes;
1298 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1299 if ((bindingAttr & BindingFlags.Public) != 0)
1302 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1308 if ((mattrs & MethodAttributes.Static) != 0) {
1309 if ((bindingAttr & BindingFlags.Static) != 0)
1312 if ((bindingAttr & BindingFlags.Instance) != 0)
1319 PropertyInfo[] result = new PropertyInfo [l.Count];
1324 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1326 throw not_supported ();
1329 protected override bool HasElementTypeImpl ()
1331 // a TypeBuilder can never represent an array, pointer
1335 return created.HasElementType;
1338 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1341 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1344 protected override bool IsArrayImpl ()
1346 return false; /*A TypeBuilder never represents a non typedef type.*/
1349 protected override bool IsByRefImpl ()
1351 return false; /*A TypeBuilder never represents a non typedef type.*/
1354 protected override bool IsCOMObjectImpl ()
1356 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1359 protected override bool IsPointerImpl ()
1361 return false; /*A TypeBuilder never represents a non typedef type.*/
1364 protected override bool IsPrimitiveImpl ()
1370 // FIXME: I doubt just removing this still works.
1371 protected override bool IsValueTypeImpl ()
1373 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1375 Type parent_type = parent;
1376 while (parent_type != null) {
1377 if (parent_type == pmodule.assemblyb.corlib_value_type)
1379 parent_type = parent_type.BaseType;
1384 public override Type MakeArrayType ()
1386 return new ArrayType (this, 0);
1389 public override Type MakeArrayType (int rank)
1392 throw new IndexOutOfRangeException ();
1393 return new ArrayType (this, rank);
1396 public override Type MakeByRefType ()
1398 return new ByRefType (this);
1401 public override Type MakeGenericType (params Type [] typeArguments)
1403 //return base.MakeGenericType (typeArguments);
1405 if (!IsGenericTypeDefinition)
1406 throw new InvalidOperationException ("not a generic type definition");
1407 if (typeArguments == null)
1408 throw new ArgumentNullException ("typeArguments");
1410 if (generic_params.Length != typeArguments.Length)
1411 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");
1413 foreach (Type t in typeArguments) {
1415 throw new ArgumentNullException ("typeArguments");
1418 Type[] copy = new Type [typeArguments.Length];
1419 typeArguments.CopyTo (copy, 0);
1420 return pmodule.assemblyb.MakeGenericType (this, copy);
1423 public override Type MakePointerType ()
1425 return new PointerType (this);
1428 public override RuntimeTypeHandle TypeHandle {
1431 return created.TypeHandle;
1435 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1437 if (customBuilder == null)
1438 throw new ArgumentNullException ("customBuilder");
1440 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1441 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1442 byte[] data = customBuilder.Data;
1443 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1444 layout_kind = (int)data [2];
1445 layout_kind |= ((int)data [3]) << 8;
1446 attrs &= ~TypeAttributes.LayoutMask;
1447 switch ((LayoutKind)layout_kind) {
1448 case LayoutKind.Auto:
1449 attrs |= TypeAttributes.AutoLayout;
1451 case LayoutKind.Explicit:
1452 attrs |= TypeAttributes.ExplicitLayout;
1454 case LayoutKind.Sequential:
1455 attrs |= TypeAttributes.SequentialLayout;
1458 // we should ignore it since it can be any value anyway...
1459 throw new Exception ("Error in customattr");
1462 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParameters()[0].ParameterType;
1464 if (ctor_type.FullName == "System.Int16")
1466 int nnamed = (int)data [pos++];
1467 nnamed |= ((int)data [pos++]) << 8;
1468 for (int i = 0; i < nnamed; ++i) {
1469 //byte named_type = data [pos++];
1471 byte type = data [pos++];
1476 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1477 //string named_typename =
1478 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1480 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1481 // See related code/FIXME in mono/mono/metadata/reflection.c
1484 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1485 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1487 /* all the fields are integers in StructLayout */
1488 int value = (int)data [pos++];
1489 value |= ((int)data [pos++]) << 8;
1490 value |= ((int)data [pos++]) << 16;
1491 value |= ((int)data [pos++]) << 24;
1492 switch (named_name) {
1494 switch ((CharSet)value) {
1497 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1499 case CharSet.Unicode:
1500 attrs &= ~TypeAttributes.AutoClass;
1501 attrs |= TypeAttributes.UnicodeClass;
1504 attrs &= ~TypeAttributes.UnicodeClass;
1505 attrs |= TypeAttributes.AutoClass;
1508 break; // error out...
1512 packing_size = (PackingSize)value;
1518 break; // error out...
1522 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1523 attrs |= TypeAttributes.SpecialName;
1525 } else if (attrname == "System.SerializableAttribute") {
1526 attrs |= TypeAttributes.Serializable;
1528 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1529 attrs |= TypeAttributes.Import;
1531 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1532 attrs |= TypeAttributes.HasSecurity;
1535 if (cattrs != null) {
1536 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1537 cattrs.CopyTo (new_array, 0);
1538 new_array [cattrs.Length] = customBuilder;
1541 cattrs = new CustomAttributeBuilder [1];
1542 cattrs [0] = customBuilder;
1547 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1549 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1552 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1554 check_name ("name", name);
1555 if (eventtype == null)
1556 throw new ArgumentNullException ("type");
1557 check_not_created ();
1559 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1560 if (events != null) {
1561 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1562 System.Array.Copy (events, new_events, events.Length);
1563 new_events [events.Length] = res;
1564 events = new_events;
1566 events = new EventBuilder [1];
1572 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1574 throw new ArgumentNullException ("data");
1576 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1577 res.SetRVAData (data);
1581 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1584 throw new ArgumentNullException ("name");
1585 if (name.Length == 0)
1586 throw new ArgumentException ("Empty name is not legal", "name");
1587 if ((size <= 0) || (size > 0x3f0000))
1588 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1589 check_not_created ();
1591 string typeName = "$ArrayType$" + size;
1592 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1593 if (datablobtype == null) {
1594 TypeBuilder tb = DefineNestedType (typeName,
1595 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1596 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1600 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1603 public TypeToken TypeToken {
1605 return new TypeToken (0x02000000 | table_idx);
1609 public void SetParent (Type parent)
1611 check_not_created ();
1613 if (parent == null) {
1614 if ((attrs & TypeAttributes.Interface) != 0) {
1615 if ((attrs & TypeAttributes.Abstract) == 0)
1616 throw new InvalidOperationException ("Interface must be declared abstract.");
1619 this.parent = typeof (object);
1622 this.parent = parent;
1625 // will just set the parent-related bits if called a second time
1626 setup_internal_class (this);
1629 internal int get_next_table_index (object obj, int table, bool inc) {
1630 return pmodule.get_next_table_index (obj, table, inc);
1634 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1636 if (created == null)
1637 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1639 return created.GetInterfaceMap (interfaceType);
1642 internal override Type InternalResolve ()
1648 internal bool is_created {
1650 return createTypeCalled;
1654 private Exception not_supported ()
1656 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1659 private void check_not_created ()
1662 throw new InvalidOperationException ("Unable to change after type has been created.");
1665 private void check_created ()
1668 throw not_supported ();
1671 private void check_name (string argName, string name)
1674 throw new ArgumentNullException (argName);
1675 if (name.Length == 0)
1676 throw new ArgumentException ("Empty name is not legal", argName);
1677 if (name [0] == ((char)0))
1678 throw new ArgumentException ("Illegal name", argName);
1681 public override String ToString ()
1687 public override bool IsAssignableFrom (Type c)
1689 return base.IsAssignableFrom (c);
1692 [MonoTODO ("arrays")]
1693 internal bool IsAssignableTo (Type c)
1698 if (c.IsInterface) {
1699 if (parent != null && is_created) {
1700 if (c.IsAssignableFrom (parent))
1704 if (interfaces == null)
1706 foreach (Type t in interfaces)
1707 if (c.IsAssignableFrom (t))
1714 return c == typeof (object);
1716 return c.IsAssignableFrom (parent);
1719 public bool IsCreated ()
1724 public override Type[] GetGenericArguments ()
1726 if (generic_params == null)
1728 Type[] args = new Type [generic_params.Length];
1729 generic_params.CopyTo (args, 0);
1733 public override Type GetGenericTypeDefinition ()
1735 if (generic_params == null)
1736 throw new InvalidOperationException ("Type is not generic");
1740 public override bool ContainsGenericParameters {
1742 return generic_params != null;
1746 public extern override bool IsGenericParameter {
1747 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1751 public override GenericParameterAttributes GenericParameterAttributes {
1752 get { return GenericParameterAttributes.None; }
1755 public override bool IsGenericTypeDefinition {
1757 return generic_params != null;
1761 public override bool IsGenericType {
1762 get { return IsGenericTypeDefinition; }
1766 public override int GenericParameterPosition {
1772 public override MethodBase DeclaringMethod {
1778 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1781 throw new ArgumentNullException ("names");
1782 if (names.Length == 0)
1783 throw new ArgumentException ("names");
1785 setup_generic_class ();
1787 generic_params = new GenericTypeParameterBuilder [names.Length];
1788 for (int i = 0; i < names.Length; i++) {
1789 string item = names [i];
1791 throw new ArgumentNullException ("names");
1792 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1795 return generic_params;
1798 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1800 /*FIXME I would expect the same checks of GetMethod here*/
1802 throw new ArgumentException ("Type is not generic", "type");
1804 if (!type.IsGenericType)
1805 throw new ArgumentException ("Type is not a generic type", "type");
1807 if (type.IsGenericTypeDefinition)
1808 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1810 if (constructor == null)
1811 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1813 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1814 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1815 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1816 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1818 ConstructorInfo res = type.GetConstructor (constructor);
1820 throw new ArgumentException ("constructor not found");
1825 static bool IsValidGetMethodType (Type type)
1827 if (type is TypeBuilder || type is MonoGenericClass)
1829 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1830 if (type.Module is ModuleBuilder)
1832 if (type.IsGenericParameter)
1835 Type[] inst = type.GetGenericArguments ();
1838 for (int i = 0; i < inst.Length; ++i) {
1839 if (IsValidGetMethodType (inst [i]))
1845 public static MethodInfo GetMethod (Type type, MethodInfo method)
1847 if (!IsValidGetMethodType (type))
1848 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1850 if (type is TypeBuilder && type.ContainsGenericParameters)
1851 type = type.MakeGenericType (type.GetGenericArguments ());
1853 if (!type.IsGenericType)
1854 throw new ArgumentException ("type is not a generic type", "type");
1856 if (!method.DeclaringType.IsGenericTypeDefinition)
1857 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1858 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1859 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1861 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1863 MethodInfo res = type.GetMethod (method);
1865 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1870 public static FieldInfo GetField (Type type, FieldInfo field)
1872 if (!type.IsGenericType)
1873 throw new ArgumentException ("Type is not a generic type", "type");
1875 if (type.IsGenericTypeDefinition)
1876 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1878 if (field is FieldOnTypeBuilderInst)
1879 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1881 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1882 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1884 FieldInfo res = type.GetField (field);
1886 throw new System.Exception ("field not found");
1891 internal TypeCode GetTypeCodeInternal () {
1892 if (parent == pmodule.assemblyb.corlib_enum_type) {
1893 for (int i = 0; i < num_fields; ++i) {
1894 FieldBuilder f = fields [i];
1896 return Type.GetTypeCode (f.FieldType);
1898 throw new InvalidOperationException ("Enum basetype field not defined");
1900 return Type.GetTypeCodeInternal (this);
1905 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1907 throw new NotImplementedException ();
1910 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1912 throw new NotImplementedException ();
1915 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1917 throw new NotImplementedException ();
1920 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1922 throw new NotImplementedException ();
1925 internal override bool IsUserType {