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.
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Globalization;
41 using System.Collections;
42 using System.Security;
43 using System.Security.Permissions;
44 using System.Diagnostics.SymbolStore;
46 namespace System.Reflection.Emit
49 [ComDefaultInterface (typeof (_TypeBuilder))]
50 [ClassInterface (ClassInterfaceType.None)]
51 [StructLayout (LayoutKind.Sequential)]
52 public sealed class TypeBuilder : Type, _TypeBuilder
54 #pragma warning disable 169
55 #region Sync with reflection.h
57 private string nspace;
59 private Type nesting_type;
60 internal Type[] interfaces;
61 internal int num_methods;
62 internal MethodBuilder[] methods;
63 internal ConstructorBuilder[] ctors;
64 internal PropertyBuilder[] properties;
65 internal int num_fields;
66 internal FieldBuilder[] fields;
67 internal EventBuilder[] events;
68 private CustomAttributeBuilder[] cattrs;
69 internal TypeBuilder[] subtypes;
70 internal TypeAttributes attrs;
71 private int table_idx;
72 private ModuleBuilder pmodule;
73 private int class_size;
74 private PackingSize packing_size;
75 private IntPtr generic_container;
76 private GenericTypeParameterBuilder[] generic_params;
77 private RefEmitPermissionSet[] permissions;
80 #pragma warning restore 169
83 bool createTypeCalled;
84 private Type underlying_type;
86 public const int UnspecifiedTypeSize = 0;
88 protected override TypeAttributes GetAttributeFlagsImpl ()
93 [MethodImplAttribute(MethodImplOptions.InternalCall)]
94 private extern void setup_internal_class (TypeBuilder tb);
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 private extern void create_internal_class (TypeBuilder tb);
99 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100 private extern void setup_generic_class ();
102 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103 private extern void create_generic_class ();
105 [MethodImplAttribute(MethodImplOptions.InternalCall)]
106 private extern EventInfo get_event_info (EventBuilder eb);
108 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
112 this.class_size = UnspecifiedTypeSize;
113 this.table_idx = table_idx;
114 fullname = this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
115 this.nspace = String.Empty;
117 setup_internal_class (this);
120 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
123 this.parent = parent;
125 this.class_size = type_size;
126 this.packing_size = packing_size;
127 this.nesting_type = nesting_type;
129 check_name ("fullname", name);
131 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
132 throw new InvalidOperationException ("Interface must be declared abstract.");
134 sep_index = name.LastIndexOf('.');
135 if (sep_index != -1) {
136 this.tname = name.Substring (sep_index + 1);
137 this.nspace = name.Substring (0, sep_index);
140 this.nspace = String.Empty;
142 if (interfaces != null) {
143 this.interfaces = new Type[interfaces.Length];
144 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
148 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
149 this.parent = typeof (object);
152 table_idx = mb.get_next_table_index (this, 0x02, true);
153 setup_internal_class (this);
154 fullname = GetFullName ();
157 public override Assembly Assembly {
158 get {return pmodule.Assembly;}
161 public override string AssemblyQualifiedName {
163 return fullname + ", " + Assembly.FullName;
167 public override Type BaseType {
173 public override Type DeclaringType {
174 get { return nesting_type; }
178 public override bool IsSubclassOf (Type c)
194 public override Type UnderlyingSystemType {
197 return created.UnderlyingSystemType;
200 if (underlying_type != null)
201 return underlying_type;
202 throw new InvalidOperationException (
203 "Enumeration type is not defined.");
210 string GetFullName ()
212 if (nesting_type != null)
213 return String.Concat (nesting_type.FullName, "+", tname);
214 if ((nspace != null) && (nspace.Length > 0))
215 return String.Concat (nspace, ".", tname);
219 public override string FullName {
225 public override Guid GUID {
232 public override Module Module {
233 get {return pmodule;}
236 public override string Name {
240 public override string Namespace {
244 public PackingSize PackingSize {
245 get {return packing_size;}
249 get { return class_size; }
252 public override Type ReflectedType {
253 get { return nesting_type; }
256 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
260 throw new ArgumentNullException ("pset");
261 if ((action == SecurityAction.RequestMinimum) ||
262 (action == SecurityAction.RequestOptional) ||
263 (action == SecurityAction.RequestRefuse))
264 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
266 check_not_created ();
268 if (permissions != null) {
269 /* Check duplicate actions */
270 foreach (RefEmitPermissionSet set in permissions)
271 if (set.action == action)
272 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
274 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
275 permissions.CopyTo (new_array, 0);
276 permissions = new_array;
279 permissions = new RefEmitPermissionSet [1];
281 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
282 attrs |= TypeAttributes.HasSecurity;
287 public void AddInterfaceImplementation (Type interfaceType)
289 if (interfaceType == null)
290 throw new ArgumentNullException ("interfaceType");
291 check_not_created ();
293 if (interfaces != null) {
294 // Check for duplicates
295 foreach (Type t in interfaces)
296 if (t == interfaceType)
299 Type[] ifnew = new Type [interfaces.Length + 1];
300 interfaces.CopyTo (ifnew, 0);
301 ifnew [interfaces.Length] = interfaceType;
304 interfaces = new Type [1];
305 interfaces [0] = interfaceType;
309 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
310 CallingConventions callConvention, Type[] types,
311 ParameterModifier[] modifiers)
315 if (created == typeof (object)) {
317 * This happens when building corlib. Calling created.GetConstructor
318 * would return constructors from the real mscorlib, instead of the
325 ConstructorBuilder found = null;
328 foreach (ConstructorBuilder cb in ctors) {
329 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
339 throw new AmbiguousMatchException ();
342 MethodBase[] match = new MethodBase [count];
347 foreach (ConstructorInfo m in ctors) {
348 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
354 binder = Binder.DefaultBinder;
355 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
359 return created.GetConstructor (bindingAttr, binder,
360 callConvention, types, modifiers);
363 public override bool IsDefined (Type attributeType, bool inherit)
366 throw new NotSupportedException ();
368 * MS throws NotSupported here, but we can't because some corlib
369 * classes make calls to IsDefined.
371 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
374 public override object[] GetCustomAttributes(bool inherit)
378 return created.GetCustomAttributes (inherit);
381 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
385 return created.GetCustomAttributes (attributeType, inherit);
388 public TypeBuilder DefineNestedType (string name)
390 return DefineNestedType (name, TypeAttributes.NestedPrivate,
391 pmodule.assemblyb.corlib_object_type, null);
394 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
396 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
399 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
401 return DefineNestedType (name, attr, parent, null);
404 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
405 PackingSize packSize, int typeSize)
407 // Visibility must be NestedXXX
409 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
410 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
411 throw new ArgumentException ("attr", "Bad type flags for nested type.");
413 if (interfaces != null)
414 foreach (Type iface in interfaces)
416 throw new ArgumentNullException ("interfaces");
418 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
419 res.fullname = res.GetFullName ();
420 pmodule.RegisterTypeName (res, res.fullname);
421 if (subtypes != null) {
422 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
423 System.Array.Copy (subtypes, new_types, subtypes.Length);
424 new_types [subtypes.Length] = res;
425 subtypes = new_types;
427 subtypes = new TypeBuilder [1];
434 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
436 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
439 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
441 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
444 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
446 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
450 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
452 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
456 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
458 check_not_created ();
459 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
460 callingConvention, parameterTypes, requiredCustomModifiers,
461 optionalCustomModifiers);
463 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
464 System.Array.Copy (ctors, new_ctors, ctors.Length);
465 new_ctors [ctors.Length] = cb;
468 ctors = new ConstructorBuilder [1];
475 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
477 Type parent_type, old_parent_type;
480 parent_type = parent;
482 parent_type = pmodule.assemblyb.corlib_object_type;
484 old_parent_type = parent_type;
485 parent_type = parent_type.InternalResolve ();
486 /*This avoids corlib to have self references.*/
487 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
488 parent_type = old_parent_type;
490 ConstructorInfo parent_constructor =
491 parent_type.GetConstructor (
492 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
493 null, Type.EmptyTypes, null);
494 if (parent_constructor == null) {
495 throw new NotSupportedException ("Parent does"
496 + " not have a default constructor."
497 + " The default constructor must be"
498 + " explicitly defined.");
501 ConstructorBuilder cb = DefineConstructor (attributes,
502 CallingConventions.Standard, Type.EmptyTypes);
503 ILGenerator ig = cb.GetILGenerator ();
504 ig.Emit (OpCodes.Ldarg_0);
505 ig.Emit (OpCodes.Call, parent_constructor);
506 ig.Emit (OpCodes.Ret);
510 private void append_method (MethodBuilder mb)
512 if (methods != null) {
513 if (methods.Length == num_methods) {
514 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
515 System.Array.Copy (methods, new_methods, num_methods);
516 methods = new_methods;
519 methods = new MethodBuilder [1];
521 methods [num_methods] = mb;
525 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
527 return DefineMethod (name, attributes, CallingConventions.Standard,
528 returnType, parameterTypes);
531 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
533 return DefineMethod (name, attributes, callingConvention, returnType,
534 null, null, parameterTypes, null, null);
537 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
539 check_name ("name", name);
540 check_not_created ();
542 !((attributes & MethodAttributes.Abstract) != 0) ||
543 !((attributes & MethodAttributes.Virtual) != 0)) &&
544 !(((attributes & MethodAttributes.Static) != 0)))
545 throw new ArgumentException ("Interface method must be abstract and virtual.");
547 if (returnType == null)
548 returnType = pmodule.assemblyb.corlib_void_type;
549 MethodBuilder res = new MethodBuilder (this, name, attributes,
550 callingConvention, returnType,
551 returnTypeRequiredCustomModifiers,
552 returnTypeOptionalCustomModifiers, parameterTypes,
553 parameterTypeRequiredCustomModifiers,
554 parameterTypeOptionalCustomModifiers);
559 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
561 return DefinePInvokeMethod (name, dllName, entryName, attributes,
562 callingConvention, returnType, null, null, parameterTypes,
563 null, null, nativeCallConv, nativeCharSet);
566 public MethodBuilder DefinePInvokeMethod (
569 string entryName, MethodAttributes attributes,
570 CallingConventions callingConvention,
572 Type[] returnTypeRequiredCustomModifiers,
573 Type[] returnTypeOptionalCustomModifiers,
574 Type[] parameterTypes,
575 Type[][] parameterTypeRequiredCustomModifiers,
576 Type[][] parameterTypeOptionalCustomModifiers,
577 CallingConvention nativeCallConv,
578 CharSet nativeCharSet)
580 check_name ("name", name);
581 check_name ("dllName", dllName);
582 check_name ("entryName", entryName);
583 if ((attributes & MethodAttributes.Abstract) != 0)
584 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
586 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
587 check_not_created ();
590 = new MethodBuilder (
596 returnTypeRequiredCustomModifiers,
597 returnTypeOptionalCustomModifiers,
599 parameterTypeRequiredCustomModifiers,
600 parameterTypeOptionalCustomModifiers,
609 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
610 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
611 nativeCallConv, nativeCharSet);
614 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
616 return DefineMethod (name, attributes, CallingConventions.Standard);
619 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
621 return DefineMethod (name, attributes, callingConvention, null, null);
624 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
626 if (methodInfoBody == null)
627 throw new ArgumentNullException ("methodInfoBody");
628 if (methodInfoDeclaration == null)
629 throw new ArgumentNullException ("methodInfoDeclaration");
630 check_not_created ();
631 if (methodInfoBody.DeclaringType != this)
632 throw new ArgumentException ("method body must belong to this type");
634 if (methodInfoBody is MethodBuilder) {
635 MethodBuilder mb = (MethodBuilder)methodInfoBody;
636 mb.set_override (methodInfoDeclaration);
640 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
642 return DefineField (fieldName, type, null, null, attributes);
645 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
647 check_name ("fieldName", fieldName);
648 if (type == typeof (void))
649 throw new ArgumentException ("Bad field type in defining field.");
650 check_not_created ();
652 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
653 if (fields != null) {
654 if (fields.Length == num_fields) {
655 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
656 System.Array.Copy (fields, new_fields, num_fields);
659 fields [num_fields] = res;
662 fields = new FieldBuilder [1];
665 create_internal_class (this);
669 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
670 underlying_type = type;
676 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
678 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
682 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
684 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
688 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
690 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
693 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
695 check_name ("name", name);
696 if (parameterTypes != null)
697 foreach (Type param in parameterTypes)
699 throw new ArgumentNullException ("parameterTypes");
700 check_not_created ();
702 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
704 if (properties != null) {
705 Array.Resize (ref properties, properties.Length + 1);
706 properties [properties.Length - 1] = res;
708 properties = new PropertyBuilder [1] { res };
714 public ConstructorBuilder DefineTypeInitializer()
716 return DefineConstructor (MethodAttributes.Public |
717 MethodAttributes.Static | MethodAttributes.SpecialName |
718 MethodAttributes.RTSpecialName, CallingConventions.Standard,
722 [MethodImplAttribute(MethodImplOptions.InternalCall)]
723 private extern Type create_runtime_class (TypeBuilder tb);
725 private bool is_nested_in (Type t)
736 // Return whenever this type has a ctor defined using DefineMethod ()
737 private bool has_ctor_method () {
738 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
740 for (int i = 0; i < num_methods; ++i) {
741 MethodBuilder mb = (MethodBuilder)(methods[i]);
743 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
750 public Type CreateType()
752 /* handle nesting_type */
753 if (createTypeCalled)
756 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
757 SetParent (pmodule.assemblyb.corlib_object_type);
760 create_generic_class ();
762 // Fire TypeResolve events for fields whose type is an unfinished
764 if (fields != null) {
765 foreach (FieldBuilder fb in fields) {
768 Type ft = fb.FieldType;
769 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
770 TypeBuilder tb = (TypeBuilder)ft;
771 if (!tb.is_created) {
772 AppDomain.CurrentDomain.DoTypeResolve (tb);
773 if (!tb.is_created) {
774 // FIXME: We should throw an exception here,
775 // but mcs expects that the type is created
776 // even if the exception is thrown
777 //throw new TypeLoadException ("Could not load type " + tb);
785 // On classes, define a default constructor if not provided
787 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
788 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
789 DefineDefaultConstructor (MethodAttributes.Public);
791 createTypeCalled = true;
793 if ((parent != null) && parent.IsSealed)
794 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
796 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
797 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
799 if (methods != null) {
800 bool is_concrete = !IsAbstract;
801 for (int i = 0; i < num_methods; ++i) {
802 MethodBuilder mb = (MethodBuilder)(methods[i]);
803 if (is_concrete && mb.IsAbstract)
804 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
805 mb.check_override ();
811 foreach (ConstructorBuilder ctor in ctors)
815 created = create_runtime_class (this);
821 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
823 symbolWriter.OpenNamespace (this.Namespace);
825 if (methods != null) {
826 for (int i = 0; i < num_methods; ++i) {
827 MethodBuilder metb = (MethodBuilder) methods[i];
828 metb.GenerateDebugInfo (symbolWriter);
833 foreach (ConstructorBuilder ctor in ctors)
834 ctor.GenerateDebugInfo (symbolWriter);
837 symbolWriter.CloseNamespace ();
839 if (subtypes != null) {
840 for (int i = 0; i < subtypes.Length; ++i)
841 subtypes [i].GenerateDebugInfo (symbolWriter);
846 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
849 return created.GetConstructors (bindingAttr);
851 throw new NotSupportedException ();
854 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
857 return new ConstructorInfo [0];
858 ArrayList l = new ArrayList ();
860 MethodAttributes mattrs;
862 foreach (ConstructorBuilder c in ctors) {
864 mattrs = c.Attributes;
865 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
866 if ((bindingAttr & BindingFlags.Public) != 0)
869 if ((bindingAttr & BindingFlags.NonPublic) != 0)
875 if ((mattrs & MethodAttributes.Static) != 0) {
876 if ((bindingAttr & BindingFlags.Static) != 0)
879 if ((bindingAttr & BindingFlags.Instance) != 0)
886 ConstructorInfo[] result = new ConstructorInfo [l.Count];
891 public override Type GetElementType ()
893 throw new NotSupportedException ();
896 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
899 return created.GetEvent (name, bindingAttr);
902 /* Needed to keep signature compatibility with MS.NET */
903 public override EventInfo[] GetEvents ()
905 return GetEvents (DefaultBindingFlags);
908 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
911 return created.GetEvents (bindingAttr);
912 throw new NotSupportedException ();
915 // This is only used from MonoGenericInst.initialize().
916 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
919 return new EventInfo [0];
920 ArrayList l = new ArrayList ();
922 MethodAttributes mattrs;
925 foreach (EventBuilder eb in events) {
928 EventInfo c = get_event_info (eb);
930 accessor = c.GetAddMethod (true);
931 if (accessor == null)
932 accessor = c.GetRemoveMethod (true);
933 if (accessor == null)
935 mattrs = accessor.Attributes;
936 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
937 if ((bindingAttr & BindingFlags.Public) != 0)
940 if ((bindingAttr & BindingFlags.NonPublic) != 0)
946 if ((mattrs & MethodAttributes.Static) != 0) {
947 if ((bindingAttr & BindingFlags.Static) != 0)
950 if ((bindingAttr & BindingFlags.Instance) != 0)
957 EventInfo[] result = new EventInfo [l.Count];
962 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
965 return created.GetField (name, bindingAttr);
971 FieldAttributes mattrs;
973 foreach (FieldInfo c in fields) {
979 mattrs = c.Attributes;
980 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
981 if ((bindingAttr & BindingFlags.Public) != 0)
984 if ((bindingAttr & BindingFlags.NonPublic) != 0)
990 if ((mattrs & FieldAttributes.Static) != 0) {
991 if ((bindingAttr & BindingFlags.Static) != 0)
994 if ((bindingAttr & BindingFlags.Instance) != 0)
1004 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1006 if (created != null)
1007 return created.GetFields (bindingAttr);
1010 return new FieldInfo [0];
1011 ArrayList l = new ArrayList ();
1013 FieldAttributes mattrs;
1015 foreach (FieldInfo c in fields) {
1019 mattrs = c.Attributes;
1020 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1021 if ((bindingAttr & BindingFlags.Public) != 0)
1024 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1030 if ((mattrs & FieldAttributes.Static) != 0) {
1031 if ((bindingAttr & BindingFlags.Static) != 0)
1034 if ((bindingAttr & BindingFlags.Instance) != 0)
1041 FieldInfo[] result = new FieldInfo [l.Count];
1046 public override Type GetInterface (string name, bool ignoreCase)
1049 return created.GetInterface (name, ignoreCase);
1052 public override Type[] GetInterfaces ()
1055 return created.GetInterfaces ();
1057 if (interfaces != null) {
1058 Type[] ret = new Type [interfaces.Length];
1059 interfaces.CopyTo (ret, 0);
1062 return Type.EmptyTypes;
1066 public override MemberInfo[] GetMember (string name, MemberTypes type,
1067 BindingFlags bindingAttr)
1070 return created.GetMember (name, type, bindingAttr);
1073 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1076 return created.GetMembers (bindingAttr);
1079 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1081 MethodInfo[] candidates;
1083 MethodAttributes mattrs;
1085 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1086 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1087 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1089 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1091 for (int i = 0; i < parent_methods.Length; i++) {
1092 MethodInfo m = parent_methods [i];
1094 mattrs = m.Attributes;
1096 if (m.IsStatic && !flatten)
1099 switch (mattrs & MethodAttributes.MemberAccessMask) {
1100 case MethodAttributes.Public:
1101 match = (bindingAttr & BindingFlags.Public) != 0;
1103 case MethodAttributes.Assembly:
1104 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1106 case MethodAttributes.Private:
1110 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1115 parent_candidates.Add (m);
1118 if (methods == null) {
1119 candidates = new MethodInfo [parent_candidates.Count];
1120 parent_candidates.CopyTo (candidates);
1122 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1123 parent_candidates.CopyTo (candidates, 0);
1124 methods.CopyTo (candidates, parent_candidates.Count);
1128 candidates = methods;
1130 if (candidates == null)
1131 return new MethodInfo [0];
1133 ArrayList l = new ArrayList ();
1135 foreach (MethodInfo c in candidates) {
1139 if (String.Compare (c.Name, name, ignoreCase) != 0)
1143 mattrs = c.Attributes;
1144 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1145 if ((bindingAttr & BindingFlags.Public) != 0)
1148 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1154 if ((mattrs & MethodAttributes.Static) != 0) {
1155 if ((bindingAttr & BindingFlags.Static) != 0)
1158 if ((bindingAttr & BindingFlags.Instance) != 0)
1166 MethodInfo[] result = new MethodInfo [l.Count];
1171 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1173 return GetMethodsByName (null, bindingAttr, false, this);
1176 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1178 CallingConventions callConvention,
1179 Type[] types, ParameterModifier[] modifiers)
1183 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
1184 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
1185 MethodInfo found = null;
1187 int typesLen = (types != null) ? types.Length : 0;
1190 foreach (MethodInfo m in methods) {
1191 // Under MS.NET, Standard|HasThis matches Standard...
1192 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1201 if (count == 1 && typesLen == 0)
1204 match = new MethodBase [count];
1209 foreach (MethodInfo m in methods) {
1210 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1212 match [count++] = m;
1217 return (MethodInfo) Binder.FindMostDerivedMatch (match);
1220 binder = Binder.DefaultBinder;
1222 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
1225 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1229 if (subtypes == null)
1232 foreach (TypeBuilder t in subtypes) {
1235 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1236 if ((bindingAttr & BindingFlags.Public) == 0)
1239 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1249 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1252 throw new NotSupportedException ();
1255 ArrayList result = new ArrayList ();
1257 if (subtypes == null)
1258 return Type.EmptyTypes;
1259 foreach (TypeBuilder t in subtypes) {
1261 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1262 if ((bindingAttr & BindingFlags.Public) != 0)
1265 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1272 Type[] r = new Type [result.Count];
1277 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1280 return created.GetProperties (bindingAttr);
1282 if (properties == null)
1283 return new PropertyInfo [0];
1284 ArrayList l = new ArrayList ();
1286 MethodAttributes mattrs;
1287 MethodInfo accessor;
1289 foreach (PropertyInfo c in properties) {
1291 accessor = c.GetGetMethod (true);
1292 if (accessor == null)
1293 accessor = c.GetSetMethod (true);
1294 if (accessor == null)
1296 mattrs = accessor.Attributes;
1297 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1298 if ((bindingAttr & BindingFlags.Public) != 0)
1301 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1307 if ((mattrs & MethodAttributes.Static) != 0) {
1308 if ((bindingAttr & BindingFlags.Static) != 0)
1311 if ((bindingAttr & BindingFlags.Instance) != 0)
1318 PropertyInfo[] result = new PropertyInfo [l.Count];
1323 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1325 throw not_supported ();
1328 protected override bool HasElementTypeImpl ()
1330 // a TypeBuilder can never represent an array, pointer
1334 return created.HasElementType;
1337 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1340 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1343 protected override bool IsArrayImpl ()
1345 return false; /*A TypeBuilder never represents a non typedef type.*/
1348 protected override bool IsByRefImpl ()
1350 return false; /*A TypeBuilder never represents a non typedef type.*/
1353 protected override bool IsCOMObjectImpl ()
1355 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1358 protected override bool IsPointerImpl ()
1360 return false; /*A TypeBuilder never represents a non typedef type.*/
1363 protected override bool IsPrimitiveImpl ()
1369 // FIXME: I doubt just removing this still works.
1370 protected override bool IsValueTypeImpl ()
1372 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1374 Type parent_type = parent;
1375 while (parent_type != null) {
1376 if (parent_type == pmodule.assemblyb.corlib_value_type)
1378 parent_type = parent_type.BaseType;
1383 public override Type MakeArrayType ()
1385 return new ArrayType (this, 0);
1388 public override Type MakeArrayType (int rank)
1391 throw new IndexOutOfRangeException ();
1392 return new ArrayType (this, rank);
1395 public override Type MakeByRefType ()
1397 return new ByRefType (this);
1400 public override Type MakeGenericType (params Type [] typeArguments)
1402 //return base.MakeGenericType (typeArguments);
1404 if (!IsGenericTypeDefinition)
1405 throw new InvalidOperationException ("not a generic type definition");
1406 if (typeArguments == null)
1407 throw new ArgumentNullException ("typeArguments");
1409 if (generic_params.Length != typeArguments.Length)
1410 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");
1412 foreach (Type t in typeArguments) {
1414 throw new ArgumentNullException ("typeArguments");
1417 Type[] copy = new Type [typeArguments.Length];
1418 typeArguments.CopyTo (copy, 0);
1419 return pmodule.assemblyb.MakeGenericType (this, copy);
1422 public override Type MakePointerType ()
1424 return new PointerType (this);
1427 public override RuntimeTypeHandle TypeHandle {
1430 return created.TypeHandle;
1434 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1436 if (customBuilder == null)
1437 throw new ArgumentNullException ("customBuilder");
1439 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1440 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1441 byte[] data = customBuilder.Data;
1442 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1443 layout_kind = (int)data [2];
1444 layout_kind |= ((int)data [3]) << 8;
1445 attrs &= ~TypeAttributes.LayoutMask;
1446 switch ((LayoutKind)layout_kind) {
1447 case LayoutKind.Auto:
1448 attrs |= TypeAttributes.AutoLayout;
1450 case LayoutKind.Explicit:
1451 attrs |= TypeAttributes.ExplicitLayout;
1453 case LayoutKind.Sequential:
1454 attrs |= TypeAttributes.SequentialLayout;
1457 // we should ignore it since it can be any value anyway...
1458 throw new Exception ("Error in customattr");
1461 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParameters()[0].ParameterType;
1463 if (ctor_type.FullName == "System.Int16")
1465 int nnamed = (int)data [pos++];
1466 nnamed |= ((int)data [pos++]) << 8;
1467 for (int i = 0; i < nnamed; ++i) {
1468 //byte named_type = data [pos++];
1470 byte type = data [pos++];
1475 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1476 //string named_typename =
1477 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1479 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1480 // See related code/FIXME in mono/mono/metadata/reflection.c
1483 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1484 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1486 /* all the fields are integers in StructLayout */
1487 int value = (int)data [pos++];
1488 value |= ((int)data [pos++]) << 8;
1489 value |= ((int)data [pos++]) << 16;
1490 value |= ((int)data [pos++]) << 24;
1491 switch (named_name) {
1493 switch ((CharSet)value) {
1496 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1498 case CharSet.Unicode:
1499 attrs &= ~TypeAttributes.AutoClass;
1500 attrs |= TypeAttributes.UnicodeClass;
1503 attrs &= ~TypeAttributes.UnicodeClass;
1504 attrs |= TypeAttributes.AutoClass;
1507 break; // error out...
1511 packing_size = (PackingSize)value;
1517 break; // error out...
1521 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1522 attrs |= TypeAttributes.SpecialName;
1524 } else if (attrname == "System.SerializableAttribute") {
1525 attrs |= TypeAttributes.Serializable;
1527 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1528 attrs |= TypeAttributes.Import;
1530 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1531 attrs |= TypeAttributes.HasSecurity;
1534 if (cattrs != null) {
1535 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1536 cattrs.CopyTo (new_array, 0);
1537 new_array [cattrs.Length] = customBuilder;
1540 cattrs = new CustomAttributeBuilder [1];
1541 cattrs [0] = customBuilder;
1546 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1548 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1551 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1553 check_name ("name", name);
1554 if (eventtype == null)
1555 throw new ArgumentNullException ("type");
1556 check_not_created ();
1558 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1559 if (events != null) {
1560 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1561 System.Array.Copy (events, new_events, events.Length);
1562 new_events [events.Length] = res;
1563 events = new_events;
1565 events = new EventBuilder [1];
1571 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1573 throw new ArgumentNullException ("data");
1575 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1576 res.SetRVAData (data);
1580 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1583 throw new ArgumentNullException ("name");
1584 if (name.Length == 0)
1585 throw new ArgumentException ("Empty name is not legal", "name");
1586 if ((size <= 0) || (size > 0x3f0000))
1587 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1588 check_not_created ();
1590 string typeName = "$ArrayType$" + size;
1591 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1592 if (datablobtype == null) {
1593 TypeBuilder tb = DefineNestedType (typeName,
1594 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1595 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1599 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1602 public TypeToken TypeToken {
1604 return new TypeToken (0x02000000 | table_idx);
1608 public void SetParent (Type parent)
1610 check_not_created ();
1612 if (parent == null) {
1613 if ((attrs & TypeAttributes.Interface) != 0) {
1614 if ((attrs & TypeAttributes.Abstract) == 0)
1615 throw new InvalidOperationException ("Interface must be declared abstract.");
1618 this.parent = typeof (object);
1621 this.parent = parent;
1624 // will just set the parent-related bits if called a second time
1625 setup_internal_class (this);
1628 internal int get_next_table_index (object obj, int table, bool inc) {
1629 return pmodule.get_next_table_index (obj, table, inc);
1633 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1635 if (created == null)
1636 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1638 return created.GetInterfaceMap (interfaceType);
1641 internal override Type InternalResolve ()
1647 internal bool is_created {
1649 return createTypeCalled;
1653 private Exception not_supported ()
1655 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1658 private void check_not_created ()
1661 throw new InvalidOperationException ("Unable to change after type has been created.");
1664 private void check_created ()
1667 throw not_supported ();
1670 private void check_name (string argName, string name)
1673 throw new ArgumentNullException (argName);
1674 if (name.Length == 0)
1675 throw new ArgumentException ("Empty name is not legal", argName);
1676 if (name [0] == ((char)0))
1677 throw new ArgumentException ("Illegal name", argName);
1680 public override String ToString ()
1686 public override bool IsAssignableFrom (Type c)
1688 return base.IsAssignableFrom (c);
1691 [MonoTODO ("arrays")]
1692 internal bool IsAssignableTo (Type c)
1697 if (c.IsInterface) {
1698 if (parent != null && is_created) {
1699 if (c.IsAssignableFrom (parent))
1703 if (interfaces == null)
1705 foreach (Type t in interfaces)
1706 if (c.IsAssignableFrom (t))
1713 return c == typeof (object);
1715 return c.IsAssignableFrom (parent);
1718 public bool IsCreated ()
1723 public override Type[] GetGenericArguments ()
1725 if (generic_params == null)
1727 Type[] args = new Type [generic_params.Length];
1728 generic_params.CopyTo (args, 0);
1732 public override Type GetGenericTypeDefinition ()
1734 if (generic_params == null)
1735 throw new InvalidOperationException ("Type is not generic");
1739 public override bool ContainsGenericParameters {
1741 return generic_params != null;
1745 public extern override bool IsGenericParameter {
1746 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1750 public override GenericParameterAttributes GenericParameterAttributes {
1751 get { return GenericParameterAttributes.None; }
1754 public override bool IsGenericTypeDefinition {
1756 return generic_params != null;
1760 public override bool IsGenericType {
1761 get { return IsGenericTypeDefinition; }
1765 public override int GenericParameterPosition {
1771 public override MethodBase DeclaringMethod {
1777 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1780 throw new ArgumentNullException ("names");
1781 if (names.Length == 0)
1782 throw new ArgumentException ("names");
1784 setup_generic_class ();
1786 generic_params = new GenericTypeParameterBuilder [names.Length];
1787 for (int i = 0; i < names.Length; i++) {
1788 string item = names [i];
1790 throw new ArgumentNullException ("names");
1791 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1794 return generic_params;
1797 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1799 /*FIXME I would expect the same checks of GetMethod here*/
1801 throw new ArgumentException ("Type is not generic", "type");
1803 if (!type.IsGenericType)
1804 throw new ArgumentException ("Type is not a generic type", "type");
1806 if (type.IsGenericTypeDefinition)
1807 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1809 if (constructor == null)
1810 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1812 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1813 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1814 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1815 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1817 ConstructorInfo res = type.GetConstructor (constructor);
1819 throw new ArgumentException ("constructor not found");
1824 static bool IsValidGetMethodType (Type type)
1826 if (type is TypeBuilder || type is MonoGenericClass)
1828 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1829 if (type.Module is ModuleBuilder)
1831 if (type.IsGenericParameter)
1834 Type[] inst = type.GetGenericArguments ();
1837 for (int i = 0; i < inst.Length; ++i) {
1838 if (IsValidGetMethodType (inst [i]))
1844 public static MethodInfo GetMethod (Type type, MethodInfo method)
1846 if (!IsValidGetMethodType (type))
1847 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1849 if (type is TypeBuilder && type.ContainsGenericParameters)
1850 type = type.MakeGenericType (type.GetGenericArguments ());
1852 if (!type.IsGenericType)
1853 throw new ArgumentException ("type is not a generic type", "type");
1855 if (!method.DeclaringType.IsGenericTypeDefinition)
1856 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1857 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1858 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1860 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1862 MethodInfo res = type.GetMethod (method);
1864 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1869 public static FieldInfo GetField (Type type, FieldInfo field)
1871 if (!type.IsGenericType)
1872 throw new ArgumentException ("Type is not a generic type", "type");
1874 if (type.IsGenericTypeDefinition)
1875 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1877 if (field is FieldOnTypeBuilderInst)
1878 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1880 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1881 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1883 FieldInfo res = type.GetField (field);
1885 throw new System.Exception ("field not found");
1890 internal TypeCode GetTypeCodeInternal () {
1891 if (parent == pmodule.assemblyb.corlib_enum_type) {
1892 for (int i = 0; i < num_fields; ++i) {
1893 FieldBuilder f = fields [i];
1895 return Type.GetTypeCode (f.FieldType);
1897 throw new InvalidOperationException ("Enum basetype field not defined");
1899 return Type.GetTypeCodeInternal (this);
1904 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1906 throw new NotImplementedException ();
1909 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1911 throw new NotImplementedException ();
1914 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1916 throw new NotImplementedException ();
1919 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1921 throw new NotImplementedException ();
1924 internal override bool IsUserType {