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;
48 using TypeInfo = System.Type;
51 namespace System.Reflection.Emit
54 [ComDefaultInterface (typeof (_TypeBuilder))]
55 [ClassInterface (ClassInterfaceType.None)]
56 [StructLayout (LayoutKind.Sequential)]
57 public sealed class TypeBuilder : TypeInfo, _TypeBuilder
59 #pragma warning disable 169
60 #region Sync with reflection.h
62 private string nspace;
64 private Type nesting_type;
65 internal Type[] interfaces;
66 internal int num_methods;
67 internal MethodBuilder[] methods;
68 internal ConstructorBuilder[] ctors;
69 internal PropertyBuilder[] properties;
70 internal int num_fields;
71 internal FieldBuilder[] fields;
72 internal EventBuilder[] events;
73 private CustomAttributeBuilder[] cattrs;
74 internal TypeBuilder[] subtypes;
75 internal TypeAttributes attrs;
76 private int table_idx;
77 private ModuleBuilder pmodule;
78 private int class_size;
79 private PackingSize packing_size;
80 private IntPtr generic_container;
81 private GenericTypeParameterBuilder[] generic_params;
82 private RefEmitPermissionSet[] permissions;
83 private TypeInfo created;
85 #pragma warning restore 169
88 bool createTypeCalled;
89 private Type underlying_type;
91 public const int UnspecifiedTypeSize = 0;
93 protected override TypeAttributes GetAttributeFlagsImpl ()
98 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99 private extern void setup_internal_class (TypeBuilder tb);
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 private extern void create_internal_class (TypeBuilder tb);
104 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105 private extern void setup_generic_class ();
107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108 private extern void create_generic_class ();
110 [MethodImplAttribute(MethodImplOptions.InternalCall)]
111 private extern EventInfo get_event_info (EventBuilder eb);
113 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
117 this.class_size = UnspecifiedTypeSize;
118 this.table_idx = table_idx;
119 fullname = this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
120 this.nspace = String.Empty;
122 setup_internal_class (this);
125 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
128 this.parent = parent;
130 this.class_size = type_size;
131 this.packing_size = packing_size;
132 this.nesting_type = nesting_type;
134 check_name ("fullname", name);
136 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
137 throw new InvalidOperationException ("Interface must be declared abstract.");
139 sep_index = name.LastIndexOf('.');
140 if (sep_index != -1) {
141 this.tname = name.Substring (sep_index + 1);
142 this.nspace = name.Substring (0, sep_index);
145 this.nspace = String.Empty;
147 if (interfaces != null) {
148 this.interfaces = new Type[interfaces.Length];
149 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
153 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
154 this.parent = typeof (object);
157 table_idx = mb.get_next_table_index (this, 0x02, true);
158 setup_internal_class (this);
159 fullname = GetFullName ();
162 public override Assembly Assembly {
163 get {return pmodule.Assembly;}
166 public override string AssemblyQualifiedName {
168 return fullname + ", " + Assembly.FullName;
172 public override Type BaseType {
178 public override Type DeclaringType {
179 get { return nesting_type; }
183 public override bool IsSubclassOf (Type c)
199 public override Type UnderlyingSystemType {
202 return created.UnderlyingSystemType;
205 if (underlying_type != null)
206 return underlying_type;
207 throw new InvalidOperationException (
208 "Enumeration type is not defined.");
215 string GetFullName ()
217 if (nesting_type != null)
218 return String.Concat (nesting_type.FullName, "+", tname);
219 if ((nspace != null) && (nspace.Length > 0))
220 return String.Concat (nspace, ".", tname);
224 public override string FullName {
230 public override Guid GUID {
237 public override Module Module {
238 get {return pmodule;}
241 public override string Name {
245 public override string Namespace {
249 public PackingSize PackingSize {
250 get {return packing_size;}
254 get { return class_size; }
257 public override Type ReflectedType {
258 get { return nesting_type; }
261 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
265 throw new ArgumentNullException ("pset");
266 if ((action == SecurityAction.RequestMinimum) ||
267 (action == SecurityAction.RequestOptional) ||
268 (action == SecurityAction.RequestRefuse))
269 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
271 check_not_created ();
273 if (permissions != null) {
274 /* Check duplicate actions */
275 foreach (RefEmitPermissionSet set in permissions)
276 if (set.action == action)
277 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
279 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
280 permissions.CopyTo (new_array, 0);
281 permissions = new_array;
284 permissions = new RefEmitPermissionSet [1];
286 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
287 attrs |= TypeAttributes.HasSecurity;
292 public void AddInterfaceImplementation (Type interfaceType)
294 if (interfaceType == null)
295 throw new ArgumentNullException ("interfaceType");
296 check_not_created ();
298 if (interfaces != null) {
299 // Check for duplicates
300 foreach (Type t in interfaces)
301 if (t == interfaceType)
304 Type[] ifnew = new Type [interfaces.Length + 1];
305 interfaces.CopyTo (ifnew, 0);
306 ifnew [interfaces.Length] = interfaceType;
309 interfaces = new Type [1];
310 interfaces [0] = interfaceType;
314 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
315 CallingConventions callConvention, Type[] types,
316 ParameterModifier[] modifiers)
320 if (created == typeof (object)) {
322 * This happens when building corlib. Calling created.GetConstructor
323 * would return constructors from the real mscorlib, instead of the
330 ConstructorBuilder found = null;
333 foreach (ConstructorBuilder cb in ctors) {
334 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
344 throw new AmbiguousMatchException ();
347 MethodBase[] match = new MethodBase [count];
352 foreach (ConstructorInfo m in ctors) {
353 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
359 binder = Binder.DefaultBinder;
360 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
364 return created.GetConstructor (bindingAttr, binder,
365 callConvention, types, modifiers);
368 public override bool IsDefined (Type attributeType, bool inherit)
371 throw new NotSupportedException ();
373 * MS throws NotSupported here, but we can't because some corlib
374 * classes make calls to IsDefined.
376 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
379 public override object[] GetCustomAttributes(bool inherit)
383 return created.GetCustomAttributes (inherit);
386 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
390 return created.GetCustomAttributes (attributeType, inherit);
393 public TypeBuilder DefineNestedType (string name)
395 return DefineNestedType (name, TypeAttributes.NestedPrivate,
396 pmodule.assemblyb.corlib_object_type, null);
399 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
401 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
404 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
406 return DefineNestedType (name, attr, parent, null);
409 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
410 PackingSize packSize, int typeSize)
412 // Visibility must be NestedXXX
414 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
415 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
416 throw new ArgumentException ("attr", "Bad type flags for nested type.");
418 if (interfaces != null)
419 foreach (Type iface in interfaces)
421 throw new ArgumentNullException ("interfaces");
423 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
424 res.fullname = res.GetFullName ();
425 pmodule.RegisterTypeName (res, res.fullname);
426 if (subtypes != null) {
427 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
428 System.Array.Copy (subtypes, new_types, subtypes.Length);
429 new_types [subtypes.Length] = res;
430 subtypes = new_types;
432 subtypes = new TypeBuilder [1];
439 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
441 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
444 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
446 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
449 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
451 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
455 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
457 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
461 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
463 check_not_created ();
464 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
465 callingConvention, parameterTypes, requiredCustomModifiers,
466 optionalCustomModifiers);
468 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
469 System.Array.Copy (ctors, new_ctors, ctors.Length);
470 new_ctors [ctors.Length] = cb;
473 ctors = new ConstructorBuilder [1];
480 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
482 Type parent_type, old_parent_type;
485 parent_type = parent;
487 parent_type = pmodule.assemblyb.corlib_object_type;
489 old_parent_type = parent_type;
490 parent_type = parent_type.InternalResolve ();
491 /*This avoids corlib to have self references.*/
492 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
493 parent_type = old_parent_type;
495 ConstructorInfo parent_constructor =
496 parent_type.GetConstructor (
497 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
498 null, Type.EmptyTypes, null);
499 if (parent_constructor == null) {
500 throw new NotSupportedException ("Parent does"
501 + " not have a default constructor."
502 + " The default constructor must be"
503 + " explicitly defined.");
506 ConstructorBuilder cb = DefineConstructor (attributes,
507 CallingConventions.Standard, Type.EmptyTypes);
508 ILGenerator ig = cb.GetILGenerator ();
509 ig.Emit (OpCodes.Ldarg_0);
510 ig.Emit (OpCodes.Call, parent_constructor);
511 ig.Emit (OpCodes.Ret);
515 private void append_method (MethodBuilder mb)
517 if (methods != null) {
518 if (methods.Length == num_methods) {
519 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
520 System.Array.Copy (methods, new_methods, num_methods);
521 methods = new_methods;
524 methods = new MethodBuilder [1];
526 methods [num_methods] = mb;
530 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
532 return DefineMethod (name, attributes, CallingConventions.Standard,
533 returnType, parameterTypes);
536 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
538 return DefineMethod (name, attributes, callingConvention, returnType,
539 null, null, parameterTypes, null, null);
542 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
544 check_name ("name", name);
545 check_not_created ();
547 !((attributes & MethodAttributes.Abstract) != 0) ||
548 !((attributes & MethodAttributes.Virtual) != 0)) &&
549 !(((attributes & MethodAttributes.Static) != 0)))
550 throw new ArgumentException ("Interface method must be abstract and virtual.");
552 if (returnType == null)
553 returnType = pmodule.assemblyb.corlib_void_type;
554 MethodBuilder res = new MethodBuilder (this, name, attributes,
555 callingConvention, returnType,
556 returnTypeRequiredCustomModifiers,
557 returnTypeOptionalCustomModifiers, parameterTypes,
558 parameterTypeRequiredCustomModifiers,
559 parameterTypeOptionalCustomModifiers);
564 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
566 return DefinePInvokeMethod (name, dllName, entryName, attributes,
567 callingConvention, returnType, null, null, parameterTypes,
568 null, null, nativeCallConv, nativeCharSet);
571 public MethodBuilder DefinePInvokeMethod (
574 string entryName, MethodAttributes attributes,
575 CallingConventions callingConvention,
577 Type[] returnTypeRequiredCustomModifiers,
578 Type[] returnTypeOptionalCustomModifiers,
579 Type[] parameterTypes,
580 Type[][] parameterTypeRequiredCustomModifiers,
581 Type[][] parameterTypeOptionalCustomModifiers,
582 CallingConvention nativeCallConv,
583 CharSet nativeCharSet)
585 check_name ("name", name);
586 check_name ("dllName", dllName);
587 check_name ("entryName", entryName);
588 if ((attributes & MethodAttributes.Abstract) != 0)
589 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
591 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
592 check_not_created ();
595 = new MethodBuilder (
601 returnTypeRequiredCustomModifiers,
602 returnTypeOptionalCustomModifiers,
604 parameterTypeRequiredCustomModifiers,
605 parameterTypeOptionalCustomModifiers,
614 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
615 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
616 nativeCallConv, nativeCharSet);
619 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
621 return DefineMethod (name, attributes, CallingConventions.Standard);
624 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
626 return DefineMethod (name, attributes, callingConvention, null, null);
629 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
631 if (methodInfoBody == null)
632 throw new ArgumentNullException ("methodInfoBody");
633 if (methodInfoDeclaration == null)
634 throw new ArgumentNullException ("methodInfoDeclaration");
635 check_not_created ();
636 if (methodInfoBody.DeclaringType != this)
637 throw new ArgumentException ("method body must belong to this type");
639 if (methodInfoBody is MethodBuilder) {
640 MethodBuilder mb = (MethodBuilder)methodInfoBody;
641 mb.set_override (methodInfoDeclaration);
645 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
647 return DefineField (fieldName, type, null, null, attributes);
650 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
652 check_name ("fieldName", fieldName);
653 if (type == typeof (void))
654 throw new ArgumentException ("Bad field type in defining field.");
655 check_not_created ();
657 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
658 if (fields != null) {
659 if (fields.Length == num_fields) {
660 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
661 System.Array.Copy (fields, new_fields, num_fields);
664 fields [num_fields] = res;
667 fields = new FieldBuilder [1];
670 create_internal_class (this);
674 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
675 underlying_type = type;
681 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
683 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
687 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
689 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
693 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
695 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
698 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
700 check_name ("name", name);
701 if (parameterTypes != null)
702 foreach (Type param in parameterTypes)
704 throw new ArgumentNullException ("parameterTypes");
705 check_not_created ();
707 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
709 if (properties != null) {
710 Array.Resize (ref properties, properties.Length + 1);
711 properties [properties.Length - 1] = res;
713 properties = new PropertyBuilder [1] { res };
719 public ConstructorBuilder DefineTypeInitializer()
721 return DefineConstructor (MethodAttributes.Public |
722 MethodAttributes.Static | MethodAttributes.SpecialName |
723 MethodAttributes.RTSpecialName, CallingConventions.Standard,
727 [MethodImplAttribute(MethodImplOptions.InternalCall)]
728 private extern TypeInfo create_runtime_class (TypeBuilder tb);
730 private bool is_nested_in (Type t)
741 // Return whenever this type has a ctor defined using DefineMethod ()
742 private bool has_ctor_method () {
743 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
745 for (int i = 0; i < num_methods; ++i) {
746 MethodBuilder mb = (MethodBuilder)(methods[i]);
748 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
755 public Type CreateType ()
757 return CreateTypeInfo ();
763 TypeInfo CreateTypeInfo ()
765 /* handle nesting_type */
766 if (createTypeCalled)
769 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
770 SetParent (pmodule.assemblyb.corlib_object_type);
773 create_generic_class ();
775 // Fire TypeResolve events for fields whose type is an unfinished
777 if (fields != null) {
778 foreach (FieldBuilder fb in fields) {
781 Type ft = fb.FieldType;
782 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
783 TypeBuilder tb = (TypeBuilder)ft;
784 if (!tb.is_created) {
785 AppDomain.CurrentDomain.DoTypeResolve (tb);
786 if (!tb.is_created) {
787 // FIXME: We should throw an exception here,
788 // but mcs expects that the type is created
789 // even if the exception is thrown
790 //throw new TypeLoadException ("Could not load type " + tb);
798 // On classes, define a default constructor if not provided
800 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
801 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
802 DefineDefaultConstructor (MethodAttributes.Public);
804 createTypeCalled = true;
806 if ((parent != null) && parent.IsSealed)
807 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
809 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
810 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
812 if (methods != null) {
813 bool is_concrete = !IsAbstract;
814 for (int i = 0; i < num_methods; ++i) {
815 MethodBuilder mb = (MethodBuilder)(methods[i]);
816 if (is_concrete && mb.IsAbstract)
817 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
818 mb.check_override ();
824 foreach (ConstructorBuilder ctor in ctors)
828 created = create_runtime_class (this);
834 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
836 symbolWriter.OpenNamespace (this.Namespace);
838 if (methods != null) {
839 for (int i = 0; i < num_methods; ++i) {
840 MethodBuilder metb = (MethodBuilder) methods[i];
841 metb.GenerateDebugInfo (symbolWriter);
846 foreach (ConstructorBuilder ctor in ctors)
847 ctor.GenerateDebugInfo (symbolWriter);
850 symbolWriter.CloseNamespace ();
852 if (subtypes != null) {
853 for (int i = 0; i < subtypes.Length; ++i)
854 subtypes [i].GenerateDebugInfo (symbolWriter);
859 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
862 return created.GetConstructors (bindingAttr);
864 throw new NotSupportedException ();
867 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
870 return new ConstructorInfo [0];
871 ArrayList l = new ArrayList ();
873 MethodAttributes mattrs;
875 foreach (ConstructorBuilder c in ctors) {
877 mattrs = c.Attributes;
878 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
879 if ((bindingAttr & BindingFlags.Public) != 0)
882 if ((bindingAttr & BindingFlags.NonPublic) != 0)
888 if ((mattrs & MethodAttributes.Static) != 0) {
889 if ((bindingAttr & BindingFlags.Static) != 0)
892 if ((bindingAttr & BindingFlags.Instance) != 0)
899 ConstructorInfo[] result = new ConstructorInfo [l.Count];
904 public override Type GetElementType ()
906 throw new NotSupportedException ();
909 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
912 return created.GetEvent (name, bindingAttr);
915 /* Needed to keep signature compatibility with MS.NET */
916 public override EventInfo[] GetEvents ()
918 return GetEvents (DefaultBindingFlags);
921 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
924 return created.GetEvents (bindingAttr);
925 throw new NotSupportedException ();
928 // This is only used from MonoGenericInst.initialize().
929 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
932 return new EventInfo [0];
933 ArrayList l = new ArrayList ();
935 MethodAttributes mattrs;
938 foreach (EventBuilder eb in events) {
941 EventInfo c = get_event_info (eb);
943 accessor = c.GetAddMethod (true);
944 if (accessor == null)
945 accessor = c.GetRemoveMethod (true);
946 if (accessor == null)
948 mattrs = accessor.Attributes;
949 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
950 if ((bindingAttr & BindingFlags.Public) != 0)
953 if ((bindingAttr & BindingFlags.NonPublic) != 0)
959 if ((mattrs & MethodAttributes.Static) != 0) {
960 if ((bindingAttr & BindingFlags.Static) != 0)
963 if ((bindingAttr & BindingFlags.Instance) != 0)
970 EventInfo[] result = new EventInfo [l.Count];
975 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
978 return created.GetField (name, bindingAttr);
984 FieldAttributes mattrs;
986 foreach (FieldInfo c in fields) {
992 mattrs = c.Attributes;
993 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
994 if ((bindingAttr & BindingFlags.Public) != 0)
997 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1003 if ((mattrs & FieldAttributes.Static) != 0) {
1004 if ((bindingAttr & BindingFlags.Static) != 0)
1007 if ((bindingAttr & BindingFlags.Instance) != 0)
1017 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1019 if (created != null)
1020 return created.GetFields (bindingAttr);
1023 return new FieldInfo [0];
1024 ArrayList l = new ArrayList ();
1026 FieldAttributes mattrs;
1028 foreach (FieldInfo c in fields) {
1032 mattrs = c.Attributes;
1033 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1034 if ((bindingAttr & BindingFlags.Public) != 0)
1037 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1043 if ((mattrs & FieldAttributes.Static) != 0) {
1044 if ((bindingAttr & BindingFlags.Static) != 0)
1047 if ((bindingAttr & BindingFlags.Instance) != 0)
1054 FieldInfo[] result = new FieldInfo [l.Count];
1059 public override Type GetInterface (string name, bool ignoreCase)
1062 return created.GetInterface (name, ignoreCase);
1065 public override Type[] GetInterfaces ()
1068 return created.GetInterfaces ();
1070 if (interfaces != null) {
1071 Type[] ret = new Type [interfaces.Length];
1072 interfaces.CopyTo (ret, 0);
1075 return Type.EmptyTypes;
1079 public override MemberInfo[] GetMember (string name, MemberTypes type,
1080 BindingFlags bindingAttr)
1083 return created.GetMember (name, type, bindingAttr);
1086 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1089 return created.GetMembers (bindingAttr);
1092 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1094 MethodInfo[] candidates;
1096 MethodAttributes mattrs;
1098 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1099 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1100 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1102 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1104 for (int i = 0; i < parent_methods.Length; i++) {
1105 MethodInfo m = parent_methods [i];
1107 mattrs = m.Attributes;
1109 if (m.IsStatic && !flatten)
1112 switch (mattrs & MethodAttributes.MemberAccessMask) {
1113 case MethodAttributes.Public:
1114 match = (bindingAttr & BindingFlags.Public) != 0;
1116 case MethodAttributes.Assembly:
1117 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1119 case MethodAttributes.Private:
1123 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1128 parent_candidates.Add (m);
1131 if (methods == null) {
1132 candidates = new MethodInfo [parent_candidates.Count];
1133 parent_candidates.CopyTo (candidates);
1135 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1136 parent_candidates.CopyTo (candidates, 0);
1137 methods.CopyTo (candidates, parent_candidates.Count);
1141 candidates = methods;
1143 if (candidates == null)
1144 return new MethodInfo [0];
1146 ArrayList l = new ArrayList ();
1148 foreach (MethodInfo c in candidates) {
1152 if (String.Compare (c.Name, name, ignoreCase) != 0)
1156 mattrs = c.Attributes;
1157 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1158 if ((bindingAttr & BindingFlags.Public) != 0)
1161 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1167 if ((mattrs & MethodAttributes.Static) != 0) {
1168 if ((bindingAttr & BindingFlags.Static) != 0)
1171 if ((bindingAttr & BindingFlags.Instance) != 0)
1179 MethodInfo[] result = new MethodInfo [l.Count];
1184 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1186 return GetMethodsByName (null, bindingAttr, false, this);
1189 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1191 CallingConventions callConvention,
1192 Type[] types, ParameterModifier[] modifiers)
1196 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
1197 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
1198 MethodInfo found = null;
1200 int typesLen = (types != null) ? types.Length : 0;
1203 foreach (MethodInfo m in methods) {
1204 // Under MS.NET, Standard|HasThis matches Standard...
1205 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1214 if (count == 1 && typesLen == 0)
1217 match = new MethodBase [count];
1222 foreach (MethodInfo m in methods) {
1223 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1225 match [count++] = m;
1230 return (MethodInfo) Binder.FindMostDerivedMatch (match);
1233 binder = Binder.DefaultBinder;
1235 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
1238 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1242 if (subtypes == null)
1245 foreach (TypeBuilder t in subtypes) {
1248 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1249 if ((bindingAttr & BindingFlags.Public) == 0)
1252 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1262 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1265 throw new NotSupportedException ();
1268 ArrayList result = new ArrayList ();
1270 if (subtypes == null)
1271 return Type.EmptyTypes;
1272 foreach (TypeBuilder t in subtypes) {
1274 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1275 if ((bindingAttr & BindingFlags.Public) != 0)
1278 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1285 Type[] r = new Type [result.Count];
1290 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1293 return created.GetProperties (bindingAttr);
1295 if (properties == null)
1296 return new PropertyInfo [0];
1297 ArrayList l = new ArrayList ();
1299 MethodAttributes mattrs;
1300 MethodInfo accessor;
1302 foreach (PropertyInfo c in properties) {
1304 accessor = c.GetGetMethod (true);
1305 if (accessor == null)
1306 accessor = c.GetSetMethod (true);
1307 if (accessor == null)
1309 mattrs = accessor.Attributes;
1310 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1311 if ((bindingAttr & BindingFlags.Public) != 0)
1314 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1320 if ((mattrs & MethodAttributes.Static) != 0) {
1321 if ((bindingAttr & BindingFlags.Static) != 0)
1324 if ((bindingAttr & BindingFlags.Instance) != 0)
1331 PropertyInfo[] result = new PropertyInfo [l.Count];
1336 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1338 throw not_supported ();
1341 protected override bool HasElementTypeImpl ()
1343 // a TypeBuilder can never represent an array, pointer
1347 return created.HasElementType;
1350 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1353 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1356 protected override bool IsArrayImpl ()
1358 return false; /*A TypeBuilder never represents a non typedef type.*/
1361 protected override bool IsByRefImpl ()
1363 return false; /*A TypeBuilder never represents a non typedef type.*/
1366 protected override bool IsCOMObjectImpl ()
1368 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1371 protected override bool IsPointerImpl ()
1373 return false; /*A TypeBuilder never represents a non typedef type.*/
1376 protected override bool IsPrimitiveImpl ()
1382 // FIXME: I doubt just removing this still works.
1383 protected override bool IsValueTypeImpl ()
1385 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1387 Type parent_type = parent;
1388 while (parent_type != null) {
1389 if (parent_type == pmodule.assemblyb.corlib_value_type)
1391 parent_type = parent_type.BaseType;
1396 public override Type MakeArrayType ()
1398 return new ArrayType (this, 0);
1401 public override Type MakeArrayType (int rank)
1404 throw new IndexOutOfRangeException ();
1405 return new ArrayType (this, rank);
1408 public override Type MakeByRefType ()
1410 return new ByRefType (this);
1413 public override Type MakeGenericType (params Type [] typeArguments)
1415 //return base.MakeGenericType (typeArguments);
1417 if (!IsGenericTypeDefinition)
1418 throw new InvalidOperationException ("not a generic type definition");
1419 if (typeArguments == null)
1420 throw new ArgumentNullException ("typeArguments");
1422 if (generic_params.Length != typeArguments.Length)
1423 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");
1425 foreach (Type t in typeArguments) {
1427 throw new ArgumentNullException ("typeArguments");
1430 Type[] copy = new Type [typeArguments.Length];
1431 typeArguments.CopyTo (copy, 0);
1432 return pmodule.assemblyb.MakeGenericType (this, copy);
1435 public override Type MakePointerType ()
1437 return new PointerType (this);
1440 public override RuntimeTypeHandle TypeHandle {
1443 return created.TypeHandle;
1447 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1449 if (customBuilder == null)
1450 throw new ArgumentNullException ("customBuilder");
1452 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1453 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1454 byte[] data = customBuilder.Data;
1455 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1456 layout_kind = (int)data [2];
1457 layout_kind |= ((int)data [3]) << 8;
1458 attrs &= ~TypeAttributes.LayoutMask;
1459 switch ((LayoutKind)layout_kind) {
1460 case LayoutKind.Auto:
1461 attrs |= TypeAttributes.AutoLayout;
1463 case LayoutKind.Explicit:
1464 attrs |= TypeAttributes.ExplicitLayout;
1466 case LayoutKind.Sequential:
1467 attrs |= TypeAttributes.SequentialLayout;
1470 // we should ignore it since it can be any value anyway...
1471 throw new Exception ("Error in customattr");
1474 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1476 if (ctor_type.FullName == "System.Int16")
1478 int nnamed = (int)data [pos++];
1479 nnamed |= ((int)data [pos++]) << 8;
1480 for (int i = 0; i < nnamed; ++i) {
1481 //byte named_type = data [pos++];
1483 byte type = data [pos++];
1488 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1489 //string named_typename =
1490 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1492 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1493 // See related code/FIXME in mono/mono/metadata/reflection.c
1496 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1497 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1499 /* all the fields are integers in StructLayout */
1500 int value = (int)data [pos++];
1501 value |= ((int)data [pos++]) << 8;
1502 value |= ((int)data [pos++]) << 16;
1503 value |= ((int)data [pos++]) << 24;
1504 switch (named_name) {
1506 switch ((CharSet)value) {
1509 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1511 case CharSet.Unicode:
1512 attrs &= ~TypeAttributes.AutoClass;
1513 attrs |= TypeAttributes.UnicodeClass;
1516 attrs &= ~TypeAttributes.UnicodeClass;
1517 attrs |= TypeAttributes.AutoClass;
1520 break; // error out...
1524 packing_size = (PackingSize)value;
1530 break; // error out...
1534 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1535 attrs |= TypeAttributes.SpecialName;
1537 } else if (attrname == "System.SerializableAttribute") {
1538 attrs |= TypeAttributes.Serializable;
1540 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1541 attrs |= TypeAttributes.Import;
1543 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1544 attrs |= TypeAttributes.HasSecurity;
1547 if (cattrs != null) {
1548 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1549 cattrs.CopyTo (new_array, 0);
1550 new_array [cattrs.Length] = customBuilder;
1553 cattrs = new CustomAttributeBuilder [1];
1554 cattrs [0] = customBuilder;
1559 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1561 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1564 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1566 check_name ("name", name);
1567 if (eventtype == null)
1568 throw new ArgumentNullException ("type");
1569 check_not_created ();
1571 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1572 if (events != null) {
1573 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1574 System.Array.Copy (events, new_events, events.Length);
1575 new_events [events.Length] = res;
1576 events = new_events;
1578 events = new EventBuilder [1];
1584 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1586 throw new ArgumentNullException ("data");
1588 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1589 res.SetRVAData (data);
1593 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1596 throw new ArgumentNullException ("name");
1597 if (name.Length == 0)
1598 throw new ArgumentException ("Empty name is not legal", "name");
1599 if ((size <= 0) || (size > 0x3f0000))
1600 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1601 check_not_created ();
1603 string typeName = "$ArrayType$" + size;
1604 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1605 if (datablobtype == null) {
1606 TypeBuilder tb = DefineNestedType (typeName,
1607 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1608 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1612 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1615 public TypeToken TypeToken {
1617 return new TypeToken (0x02000000 | table_idx);
1621 public void SetParent (Type parent)
1623 check_not_created ();
1625 if (parent == null) {
1626 if ((attrs & TypeAttributes.Interface) != 0) {
1627 if ((attrs & TypeAttributes.Abstract) == 0)
1628 throw new InvalidOperationException ("Interface must be declared abstract.");
1631 this.parent = typeof (object);
1634 this.parent = parent;
1637 // will just set the parent-related bits if called a second time
1638 setup_internal_class (this);
1641 internal int get_next_table_index (object obj, int table, bool inc) {
1642 return pmodule.get_next_table_index (obj, table, inc);
1646 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1648 if (created == null)
1649 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1651 return created.GetInterfaceMap (interfaceType);
1654 internal override Type InternalResolve ()
1660 internal bool is_created {
1662 return createTypeCalled;
1666 private Exception not_supported ()
1668 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1671 private void check_not_created ()
1674 throw new InvalidOperationException ("Unable to change after type has been created.");
1677 private void check_created ()
1680 throw not_supported ();
1683 private void check_name (string argName, string name)
1686 throw new ArgumentNullException (argName);
1687 if (name.Length == 0)
1688 throw new ArgumentException ("Empty name is not legal", argName);
1689 if (name [0] == ((char)0))
1690 throw new ArgumentException ("Illegal name", argName);
1693 public override String ToString ()
1699 public override bool IsAssignableFrom (Type c)
1701 return base.IsAssignableFrom (c);
1704 [MonoTODO ("arrays")]
1705 internal bool IsAssignableTo (Type c)
1710 if (c.IsInterface) {
1711 if (parent != null && is_created) {
1712 if (c.IsAssignableFrom (parent))
1716 if (interfaces == null)
1718 foreach (Type t in interfaces)
1719 if (c.IsAssignableFrom (t))
1726 return c == typeof (object);
1728 return c.IsAssignableFrom (parent);
1731 public bool IsCreated ()
1736 public override Type[] GetGenericArguments ()
1738 if (generic_params == null)
1740 Type[] args = new Type [generic_params.Length];
1741 generic_params.CopyTo (args, 0);
1745 public override Type GetGenericTypeDefinition ()
1747 if (generic_params == null)
1748 throw new InvalidOperationException ("Type is not generic");
1752 public override bool ContainsGenericParameters {
1754 return generic_params != null;
1758 public extern override bool IsGenericParameter {
1759 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1763 public override GenericParameterAttributes GenericParameterAttributes {
1764 get { return GenericParameterAttributes.None; }
1767 public override bool IsGenericTypeDefinition {
1769 return generic_params != null;
1773 public override bool IsGenericType {
1774 get { return IsGenericTypeDefinition; }
1778 public override int GenericParameterPosition {
1784 public override MethodBase DeclaringMethod {
1790 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1793 throw new ArgumentNullException ("names");
1794 if (names.Length == 0)
1795 throw new ArgumentException ("names");
1797 setup_generic_class ();
1799 generic_params = new GenericTypeParameterBuilder [names.Length];
1800 for (int i = 0; i < names.Length; i++) {
1801 string item = names [i];
1803 throw new ArgumentNullException ("names");
1804 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1807 return generic_params;
1810 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1812 /*FIXME I would expect the same checks of GetMethod here*/
1814 throw new ArgumentException ("Type is not generic", "type");
1816 if (!type.IsGenericType)
1817 throw new ArgumentException ("Type is not a generic type", "type");
1819 if (type.IsGenericTypeDefinition)
1820 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1822 if (constructor == null)
1823 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1825 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1826 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1827 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1828 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1830 ConstructorInfo res = type.GetConstructor (constructor);
1832 throw new ArgumentException ("constructor not found");
1837 static bool IsValidGetMethodType (Type type)
1839 if (type is TypeBuilder || type is MonoGenericClass)
1841 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1842 if (type.Module is ModuleBuilder)
1844 if (type.IsGenericParameter)
1847 Type[] inst = type.GetGenericArguments ();
1850 for (int i = 0; i < inst.Length; ++i) {
1851 if (IsValidGetMethodType (inst [i]))
1857 public static MethodInfo GetMethod (Type type, MethodInfo method)
1859 if (!IsValidGetMethodType (type))
1860 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1862 if (type is TypeBuilder && type.ContainsGenericParameters)
1863 type = type.MakeGenericType (type.GetGenericArguments ());
1865 if (!type.IsGenericType)
1866 throw new ArgumentException ("type is not a generic type", "type");
1868 if (!method.DeclaringType.IsGenericTypeDefinition)
1869 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1870 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1871 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1873 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1875 MethodInfo res = type.GetMethod (method);
1877 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1882 public static FieldInfo GetField (Type type, FieldInfo field)
1884 if (!type.IsGenericType)
1885 throw new ArgumentException ("Type is not a generic type", "type");
1887 if (type.IsGenericTypeDefinition)
1888 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1890 if (field is FieldOnTypeBuilderInst)
1891 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1893 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1894 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1896 FieldInfo res = type.GetField (field);
1898 throw new System.Exception ("field not found");
1903 internal TypeCode GetTypeCodeInternal () {
1904 if (parent == pmodule.assemblyb.corlib_enum_type) {
1905 for (int i = 0; i < num_fields; ++i) {
1906 FieldBuilder f = fields [i];
1908 return Type.GetTypeCode (f.FieldType);
1910 throw new InvalidOperationException ("Enum basetype field not defined");
1912 return Type.GetTypeCodeInternal (this);
1917 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1919 throw new NotImplementedException ();
1922 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1924 throw new NotImplementedException ();
1927 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1929 throw new NotImplementedException ();
1932 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1934 throw new NotImplementedException ();
1937 internal override bool IsUserType {
1944 public override bool IsConstructedGenericType {
1945 get { return false; }
1948 public override bool IsAssignableFrom (TypeInfo typeInfo)
1950 return base.IsAssignableFrom (typeInfo);