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 namespace System.Reflection.Emit
51 [ComDefaultInterface (typeof (_TypeBuilder))]
52 [ClassInterface (ClassInterfaceType.None)]
53 [StructLayout (LayoutKind.Sequential)]
54 public sealed class TypeBuilder : TypeInfo, _TypeBuilder
56 #pragma warning disable 169
57 #region Sync with reflection.h
58 private string tname; // name in internal form
59 private string nspace; // namespace in internal form
61 private Type nesting_type;
62 internal Type[] interfaces;
63 internal int num_methods;
64 internal MethodBuilder[] methods;
65 internal ConstructorBuilder[] ctors;
66 internal PropertyBuilder[] properties;
67 internal int num_fields;
68 internal FieldBuilder[] fields;
69 internal EventBuilder[] events;
70 private CustomAttributeBuilder[] cattrs;
71 internal TypeBuilder[] subtypes;
72 internal TypeAttributes attrs;
73 private int table_idx;
74 private ModuleBuilder pmodule;
75 private int class_size;
76 private PackingSize packing_size;
77 private IntPtr generic_container;
78 private GenericTypeParameterBuilder[] generic_params;
79 private RefEmitPermissionSet[] permissions;
80 private TypeInfo created;
82 #pragma warning restore 169
85 bool createTypeCalled;
86 private Type underlying_type;
88 public const int UnspecifiedTypeSize = 0;
90 protected override TypeAttributes GetAttributeFlagsImpl ()
95 [MethodImplAttribute(MethodImplOptions.InternalCall)]
96 private extern void setup_internal_class ();
98 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99 private extern void create_generic_class ();
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 private extern EventInfo get_event_info (EventBuilder eb);
104 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
108 this.class_size = UnspecifiedTypeSize;
109 this.table_idx = table_idx;
110 this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
111 this.nspace = String.Empty;
112 this.fullname = TypeIdentifiers.WithoutEscape(this.tname);
114 setup_internal_class ();
117 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
120 this.parent = ResolveUserType (parent);
122 this.class_size = type_size;
123 this.packing_size = packing_size;
124 this.nesting_type = nesting_type;
126 check_name ("fullname", name);
128 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
129 throw new InvalidOperationException ("Interface must be declared abstract.");
131 sep_index = name.LastIndexOf('.');
132 if (sep_index != -1) {
133 this.tname = name.Substring (sep_index + 1);
134 this.nspace = name.Substring (0, sep_index);
137 this.nspace = String.Empty;
139 if (interfaces != null) {
140 this.interfaces = new Type[interfaces.Length];
141 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
145 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
146 this.parent = typeof (object);
149 table_idx = mb.get_next_table_index (this, 0x02, true);
150 setup_internal_class ();
151 fullname = GetFullName ();
154 public override Assembly Assembly {
155 get {return pmodule.Assembly;}
158 public override string AssemblyQualifiedName {
160 return fullname.DisplayName + ", " + Assembly.FullName;
164 public override Type BaseType {
170 public override Type DeclaringType {
171 get { return nesting_type; }
175 public override bool IsSubclassOf (Type c)
191 public override Type UnderlyingSystemType {
194 return created.UnderlyingSystemType;
197 if (underlying_type != null)
198 return underlying_type;
199 throw new InvalidOperationException (
200 "Enumeration type is not defined.");
207 TypeName GetFullName ()
209 TypeIdentifier ident = TypeIdentifiers.FromInternal (tname);
210 if (nesting_type != null)
211 return TypeNames.FromDisplay (nesting_type.FullName).NestedName (ident);
212 if ((nspace != null) && (nspace.Length > 0))
213 return TypeIdentifiers.FromInternal (nspace, ident);
217 public override string FullName {
219 return fullname.DisplayName;
223 public override Guid GUID {
230 public override Module Module {
231 get {return pmodule;}
234 public override string Name {
238 public override string Namespace {
242 public PackingSize PackingSize {
243 get {return packing_size;}
247 get { return class_size; }
250 public override Type ReflectedType {
251 get { return nesting_type; }
254 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
258 throw new ArgumentNullException ("pset");
259 if ((action == SecurityAction.RequestMinimum) ||
260 (action == SecurityAction.RequestOptional) ||
261 (action == SecurityAction.RequestRefuse))
262 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
264 check_not_created ();
266 if (permissions != null) {
267 /* Check duplicate actions */
268 foreach (RefEmitPermissionSet set in permissions)
269 if (set.action == action)
270 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
272 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
273 permissions.CopyTo (new_array, 0);
274 permissions = new_array;
277 permissions = new RefEmitPermissionSet [1];
279 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
280 attrs |= TypeAttributes.HasSecurity;
285 public void AddInterfaceImplementation (Type interfaceType)
287 if (interfaceType == null)
288 throw new ArgumentNullException ("interfaceType");
289 check_not_created ();
291 if (interfaces != null) {
292 // Check for duplicates
293 foreach (Type t in interfaces)
294 if (t == interfaceType)
297 Type[] ifnew = new Type [interfaces.Length + 1];
298 interfaces.CopyTo (ifnew, 0);
299 ifnew [interfaces.Length] = interfaceType;
302 interfaces = new Type [1];
303 interfaces [0] = interfaceType;
307 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
308 CallingConventions callConvention, Type[] types,
309 ParameterModifier[] modifiers)
313 if (created == typeof (object)) {
315 * This happens when building corlib. Calling created.GetConstructor
316 * would return constructors from the real mscorlib, instead of the
323 ConstructorBuilder found = null;
326 foreach (ConstructorBuilder cb in ctors) {
327 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
337 throw new AmbiguousMatchException ();
340 MethodBase[] match = new MethodBase [count];
345 foreach (ConstructorInfo m in ctors) {
346 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
352 binder = DefaultBinder;
353 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
357 return created.GetConstructor (bindingAttr, binder,
358 callConvention, types, modifiers);
361 public override bool IsDefined (Type attributeType, bool inherit)
364 throw new NotSupportedException ();
366 * MS throws NotSupported here, but we can't because some corlib
367 * classes make calls to IsDefined.
369 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
372 public override object[] GetCustomAttributes(bool inherit)
376 return created.GetCustomAttributes (inherit);
379 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
383 return created.GetCustomAttributes (attributeType, inherit);
386 public TypeBuilder DefineNestedType (string name)
388 return DefineNestedType (name, TypeAttributes.NestedPrivate,
389 pmodule.assemblyb.corlib_object_type, null);
392 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
394 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
397 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
399 return DefineNestedType (name, attr, parent, null);
402 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
403 PackingSize packSize, int typeSize)
405 // Visibility must be NestedXXX
407 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
408 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
409 throw new ArgumentException ("attr", "Bad type flags for nested type.");
411 if (interfaces != null)
412 foreach (Type iface in interfaces)
414 throw new ArgumentNullException ("interfaces");
416 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
417 res.fullname = res.GetFullName ();
418 pmodule.RegisterTypeName (res, res.fullname);
419 if (subtypes != null) {
420 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
421 System.Array.Copy (subtypes, new_types, subtypes.Length);
422 new_types [subtypes.Length] = res;
423 subtypes = new_types;
425 subtypes = new TypeBuilder [1];
432 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
434 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
437 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
439 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
442 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
444 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
447 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize,
450 return DefineNestedType (name, attr, parent, null, packSize, typeSize);
454 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
456 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
460 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
462 check_not_created ();
463 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
464 callingConvention, parameterTypes, requiredCustomModifiers,
465 optionalCustomModifiers);
467 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
468 System.Array.Copy (ctors, new_ctors, ctors.Length);
469 new_ctors [ctors.Length] = cb;
472 ctors = new ConstructorBuilder [1];
479 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
481 Type parent_type, old_parent_type;
484 parent_type = parent;
486 parent_type = pmodule.assemblyb.corlib_object_type;
488 old_parent_type = parent_type;
489 parent_type = parent_type.InternalResolve ();
490 /*This avoids corlib to have self references.*/
491 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
492 parent_type = old_parent_type;
494 ConstructorInfo parent_constructor =
495 parent_type.GetConstructor (
496 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
497 null, Type.EmptyTypes, null);
498 if (parent_constructor == null) {
499 throw new NotSupportedException ("Parent does"
500 + " not have a default constructor."
501 + " The default constructor must be"
502 + " explicitly defined.");
505 ConstructorBuilder cb = DefineConstructor (attributes,
506 CallingConventions.Standard, Type.EmptyTypes);
507 ILGenerator ig = cb.GetILGenerator ();
508 ig.Emit (OpCodes.Ldarg_0);
509 ig.Emit (OpCodes.Call, parent_constructor);
510 ig.Emit (OpCodes.Ret);
514 private void append_method (MethodBuilder mb)
516 if (methods != null) {
517 if (methods.Length == num_methods) {
518 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
519 System.Array.Copy (methods, new_methods, num_methods);
520 methods = new_methods;
523 methods = new MethodBuilder [1];
525 methods [num_methods] = mb;
529 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
531 return DefineMethod (name, attributes, CallingConventions.Standard,
532 returnType, parameterTypes);
535 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
537 return DefineMethod (name, attributes, callingConvention, returnType,
538 null, null, parameterTypes, null, null);
541 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
543 check_name ("name", name);
544 check_not_created ();
546 !((attributes & MethodAttributes.Abstract) != 0) ||
547 !((attributes & MethodAttributes.Virtual) != 0)) &&
548 !(((attributes & MethodAttributes.Static) != 0)))
549 throw new ArgumentException ("Interface method must be abstract and virtual.");
551 if (returnType == null)
552 returnType = pmodule.assemblyb.corlib_void_type;
553 MethodBuilder res = new MethodBuilder (this, name, attributes,
554 callingConvention, returnType,
555 returnTypeRequiredCustomModifiers,
556 returnTypeOptionalCustomModifiers, parameterTypes,
557 parameterTypeRequiredCustomModifiers,
558 parameterTypeOptionalCustomModifiers);
563 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
565 return DefinePInvokeMethod (name, dllName, entryName, attributes,
566 callingConvention, returnType, null, null, parameterTypes,
567 null, null, nativeCallConv, nativeCharSet);
570 public MethodBuilder DefinePInvokeMethod (
573 string entryName, MethodAttributes attributes,
574 CallingConventions callingConvention,
576 Type[] returnTypeRequiredCustomModifiers,
577 Type[] returnTypeOptionalCustomModifiers,
578 Type[] parameterTypes,
579 Type[][] parameterTypeRequiredCustomModifiers,
580 Type[][] parameterTypeOptionalCustomModifiers,
581 CallingConvention nativeCallConv,
582 CharSet nativeCharSet)
584 check_name ("name", name);
585 check_name ("dllName", dllName);
586 check_name ("entryName", entryName);
587 if ((attributes & MethodAttributes.Abstract) != 0)
588 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
590 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
591 check_not_created ();
594 = new MethodBuilder (
600 returnTypeRequiredCustomModifiers,
601 returnTypeOptionalCustomModifiers,
603 parameterTypeRequiredCustomModifiers,
604 parameterTypeOptionalCustomModifiers,
613 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
614 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
615 nativeCallConv, nativeCharSet);
618 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
620 return DefineMethod (name, attributes, CallingConventions.Standard);
623 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
625 return DefineMethod (name, attributes, callingConvention, null, null);
628 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
630 if (methodInfoBody == null)
631 throw new ArgumentNullException ("methodInfoBody");
632 if (methodInfoDeclaration == null)
633 throw new ArgumentNullException ("methodInfoDeclaration");
634 check_not_created ();
635 if (methodInfoBody.DeclaringType != this)
636 throw new ArgumentException ("method body must belong to this type");
638 if (methodInfoBody is MethodBuilder) {
639 MethodBuilder mb = (MethodBuilder)methodInfoBody;
640 mb.set_override (methodInfoDeclaration);
644 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
646 return DefineField (fieldName, type, null, null, attributes);
649 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
651 check_name ("fieldName", fieldName);
652 if (type == typeof (void))
653 throw new ArgumentException ("Bad field type in defining field.");
654 check_not_created ();
656 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
657 if (fields != null) {
658 if (fields.Length == num_fields) {
659 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
660 System.Array.Copy (fields, new_fields, num_fields);
663 fields [num_fields] = res;
666 fields = new FieldBuilder [1];
672 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
673 underlying_type = type;
679 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
681 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
684 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
686 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
689 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
691 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
694 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
696 check_name ("name", name);
697 if (parameterTypes != null)
698 foreach (Type param in parameterTypes)
700 throw new ArgumentNullException ("parameterTypes");
701 check_not_created ();
703 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
705 if (properties != null) {
706 Array.Resize (ref properties, properties.Length + 1);
707 properties [properties.Length - 1] = res;
709 properties = new PropertyBuilder [1] { res };
715 public ConstructorBuilder DefineTypeInitializer()
717 return DefineConstructor (MethodAttributes.Public |
718 MethodAttributes.Static | MethodAttributes.SpecialName |
719 MethodAttributes.RTSpecialName, CallingConventions.Standard,
723 [MethodImplAttribute(MethodImplOptions.InternalCall)]
724 private extern TypeInfo create_runtime_class ();
726 private bool is_nested_in (Type t)
737 // Return whenever this type has a ctor defined using DefineMethod ()
738 private bool has_ctor_method () {
739 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
741 for (int i = 0; i < num_methods; ++i) {
742 MethodBuilder mb = (MethodBuilder)(methods[i]);
744 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
751 public Type CreateType ()
753 return CreateTypeInfo ();
757 TypeInfo CreateTypeInfo ()
759 /* handle nesting_type */
760 if (createTypeCalled)
763 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
764 SetParent (pmodule.assemblyb.corlib_object_type);
767 create_generic_class ();
769 // Fire TypeResolve events for fields whose type is an unfinished
771 if (fields != null) {
772 foreach (FieldBuilder fb in fields) {
775 Type ft = fb.FieldType;
776 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
777 TypeBuilder tb = (TypeBuilder)ft;
778 if (!tb.is_created) {
779 AppDomain.CurrentDomain.DoTypeResolve (tb);
780 if (!tb.is_created) {
781 // FIXME: We should throw an exception here,
782 // but mcs expects that the type is created
783 // even if the exception is thrown
784 //throw new TypeLoadException ("Could not load type " + tb);
792 // On classes, define a default constructor if not provided
794 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
795 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
796 DefineDefaultConstructor (MethodAttributes.Public);
798 createTypeCalled = true;
800 if ((parent != null) && parent.IsSealed)
801 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
803 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
804 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
805 if (interfaces != null) {
806 foreach (var iface in interfaces) {
807 if (iface.IsNestedPrivate && iface.Assembly != Assembly)
808 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is implements the inaccessible interface '" + iface.FullName + "'.");
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)
830 created = create_runtime_class ();
836 void ResolveUserTypes () {
837 parent = ResolveUserType (parent);
838 ResolveUserTypes (interfaces);
839 if (fields != null) {
840 foreach (var fb in fields) {
842 fb.ResolveUserTypes ();
845 if (methods != null) {
846 foreach (var mb in methods) {
848 mb.ResolveUserTypes ();
852 foreach (var cb in ctors) {
854 cb.ResolveUserTypes ();
859 static internal void ResolveUserTypes (Type[] types) {
861 for (int i = 0; i < types.Length; ++i)
862 types [i] = ResolveUserType (types [i]);
865 static internal Type ResolveUserType (Type t) {
866 if (t != null && ((t.GetType ().Assembly != typeof (int).Assembly) || (t is TypeDelegator))) {
867 t = t.UnderlyingSystemType;
868 if (t != null && ((t.GetType ().Assembly != typeof (int).Assembly) || (t is TypeDelegator)))
869 throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
876 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
878 symbolWriter.OpenNamespace (this.Namespace);
880 if (methods != null) {
881 for (int i = 0; i < num_methods; ++i) {
882 MethodBuilder metb = (MethodBuilder) methods[i];
883 metb.GenerateDebugInfo (symbolWriter);
888 foreach (ConstructorBuilder ctor in ctors)
889 ctor.GenerateDebugInfo (symbolWriter);
892 symbolWriter.CloseNamespace ();
894 if (subtypes != null) {
895 for (int i = 0; i < subtypes.Length; ++i)
896 subtypes [i].GenerateDebugInfo (symbolWriter);
901 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
904 return created.GetConstructors (bindingAttr);
906 throw new NotSupportedException ();
909 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
912 return new ConstructorInfo [0];
913 ArrayList l = new ArrayList ();
915 MethodAttributes mattrs;
917 foreach (ConstructorBuilder c in ctors) {
919 mattrs = c.Attributes;
920 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
921 if ((bindingAttr & BindingFlags.Public) != 0)
924 if ((bindingAttr & BindingFlags.NonPublic) != 0)
930 if ((mattrs & MethodAttributes.Static) != 0) {
931 if ((bindingAttr & BindingFlags.Static) != 0)
934 if ((bindingAttr & BindingFlags.Instance) != 0)
941 ConstructorInfo[] result = new ConstructorInfo [l.Count];
946 public override Type GetElementType ()
948 throw new NotSupportedException ();
951 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
954 return created.GetEvent (name, bindingAttr);
957 /* Needed to keep signature compatibility with MS.NET */
958 public override EventInfo[] GetEvents ()
960 const BindingFlags DefaultBindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
961 return GetEvents (DefaultBindingFlags);
964 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
967 return created.GetEvents (bindingAttr);
968 throw new NotSupportedException ();
971 // This is only used from MonoGenericInst.initialize().
972 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
975 return new EventInfo [0];
976 ArrayList l = new ArrayList ();
978 MethodAttributes mattrs;
981 foreach (EventBuilder eb in events) {
984 EventInfo c = get_event_info (eb);
986 accessor = c.GetAddMethod (true);
987 if (accessor == null)
988 accessor = c.GetRemoveMethod (true);
989 if (accessor == null)
991 mattrs = accessor.Attributes;
992 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
993 if ((bindingAttr & BindingFlags.Public) != 0)
996 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1002 if ((mattrs & MethodAttributes.Static) != 0) {
1003 if ((bindingAttr & BindingFlags.Static) != 0)
1006 if ((bindingAttr & BindingFlags.Instance) != 0)
1013 EventInfo[] result = new EventInfo [l.Count];
1018 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
1020 if (created != null)
1021 return created.GetField (name, bindingAttr);
1027 FieldAttributes mattrs;
1029 foreach (FieldInfo c in fields) {
1035 mattrs = c.Attributes;
1036 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1037 if ((bindingAttr & BindingFlags.Public) != 0)
1040 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1046 if ((mattrs & FieldAttributes.Static) != 0) {
1047 if ((bindingAttr & BindingFlags.Static) != 0)
1050 if ((bindingAttr & BindingFlags.Instance) != 0)
1060 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1062 if (created != null)
1063 return created.GetFields (bindingAttr);
1066 return new FieldInfo [0];
1067 ArrayList l = new ArrayList ();
1069 FieldAttributes mattrs;
1071 foreach (FieldInfo c in fields) {
1075 mattrs = c.Attributes;
1076 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1077 if ((bindingAttr & BindingFlags.Public) != 0)
1080 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1086 if ((mattrs & FieldAttributes.Static) != 0) {
1087 if ((bindingAttr & BindingFlags.Static) != 0)
1090 if ((bindingAttr & BindingFlags.Instance) != 0)
1097 FieldInfo[] result = new FieldInfo [l.Count];
1102 public override Type GetInterface (string name, bool ignoreCase)
1105 return created.GetInterface (name, ignoreCase);
1108 public override Type[] GetInterfaces ()
1111 return created.GetInterfaces ();
1113 if (interfaces != null) {
1114 Type[] ret = new Type [interfaces.Length];
1115 interfaces.CopyTo (ret, 0);
1118 return Type.EmptyTypes;
1122 public override MemberInfo[] GetMember (string name, MemberTypes type,
1123 BindingFlags bindingAttr)
1126 return created.GetMember (name, type, bindingAttr);
1129 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1132 return created.GetMembers (bindingAttr);
1135 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1137 MethodInfo[] candidates;
1139 MethodAttributes mattrs;
1141 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1142 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1143 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1145 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1147 for (int i = 0; i < parent_methods.Length; i++) {
1148 MethodInfo m = parent_methods [i];
1150 mattrs = m.Attributes;
1152 if (m.IsStatic && !flatten)
1155 switch (mattrs & MethodAttributes.MemberAccessMask) {
1156 case MethodAttributes.Public:
1157 match = (bindingAttr & BindingFlags.Public) != 0;
1159 case MethodAttributes.Assembly:
1160 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1162 case MethodAttributes.Private:
1166 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1171 parent_candidates.Add (m);
1174 if (methods == null) {
1175 candidates = new MethodInfo [parent_candidates.Count];
1176 parent_candidates.CopyTo (candidates);
1178 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1179 parent_candidates.CopyTo (candidates, 0);
1180 methods.CopyTo (candidates, parent_candidates.Count);
1184 candidates = methods;
1186 if (candidates == null)
1187 return new MethodInfo [0];
1189 ArrayList l = new ArrayList ();
1191 foreach (MethodInfo c in candidates) {
1195 if (String.Compare (c.Name, name, ignoreCase) != 0)
1199 mattrs = c.Attributes;
1200 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1201 if ((bindingAttr & BindingFlags.Public) != 0)
1204 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1210 if ((mattrs & MethodAttributes.Static) != 0) {
1211 if ((bindingAttr & BindingFlags.Static) != 0)
1214 if ((bindingAttr & BindingFlags.Instance) != 0)
1222 MethodInfo[] result = new MethodInfo [l.Count];
1227 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1229 return GetMethodsByName (null, bindingAttr, false, this);
1232 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1234 CallingConventions callConvention,
1235 Type[] types, ParameterModifier[] modifiers)
1240 return created.GetMethod (name, bindingAttr);
1242 return created.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
1245 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1249 if (subtypes == null)
1252 foreach (TypeBuilder t in subtypes) {
1255 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1256 if ((bindingAttr & BindingFlags.Public) == 0)
1259 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1269 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1272 throw new NotSupportedException ();
1275 ArrayList result = new ArrayList ();
1277 if (subtypes == null)
1278 return Type.EmptyTypes;
1279 foreach (TypeBuilder t in subtypes) {
1281 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1282 if ((bindingAttr & BindingFlags.Public) != 0)
1285 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1292 Type[] r = new Type [result.Count];
1297 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1300 return created.GetProperties (bindingAttr);
1302 if (properties == null)
1303 return new PropertyInfo [0];
1304 ArrayList l = new ArrayList ();
1306 MethodAttributes mattrs;
1307 MethodInfo accessor;
1309 foreach (PropertyInfo c in properties) {
1311 accessor = c.GetGetMethod (true);
1312 if (accessor == null)
1313 accessor = c.GetSetMethod (true);
1314 if (accessor == null)
1316 mattrs = accessor.Attributes;
1317 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1318 if ((bindingAttr & BindingFlags.Public) != 0)
1321 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1327 if ((mattrs & MethodAttributes.Static) != 0) {
1328 if ((bindingAttr & BindingFlags.Static) != 0)
1331 if ((bindingAttr & BindingFlags.Instance) != 0)
1338 PropertyInfo[] result = new PropertyInfo [l.Count];
1343 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1345 throw not_supported ();
1348 protected override bool HasElementTypeImpl ()
1350 // a TypeBuilder can never represent an array, pointer
1354 return created.HasElementType;
1357 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1360 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1363 protected override bool IsArrayImpl ()
1365 return false; /*A TypeBuilder never represents a non typedef type.*/
1368 protected override bool IsByRefImpl ()
1370 return false; /*A TypeBuilder never represents a non typedef type.*/
1373 protected override bool IsCOMObjectImpl ()
1375 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1378 protected override bool IsPointerImpl ()
1380 return false; /*A TypeBuilder never represents a non typedef type.*/
1383 protected override bool IsPrimitiveImpl ()
1389 // FIXME: I doubt just removing this still works.
1390 protected override bool IsValueTypeImpl ()
1392 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1394 Type parent_type = parent;
1395 while (parent_type != null) {
1396 if (parent_type == pmodule.assemblyb.corlib_value_type)
1398 parent_type = parent_type.BaseType;
1403 public override Type MakeArrayType ()
1405 return new ArrayType (this, 0);
1408 public override Type MakeArrayType (int rank)
1411 throw new IndexOutOfRangeException ();
1412 return new ArrayType (this, rank);
1415 public override Type MakeByRefType ()
1417 return new ByRefType (this);
1420 public override Type MakeGenericType (params Type [] typeArguments)
1422 //return base.MakeGenericType (typeArguments);
1424 if (!IsGenericTypeDefinition)
1425 throw new InvalidOperationException ("not a generic type definition");
1426 if (typeArguments == null)
1427 throw new ArgumentNullException ("typeArguments");
1429 if (generic_params.Length != typeArguments.Length)
1430 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");
1432 foreach (Type t in typeArguments) {
1434 throw new ArgumentNullException ("typeArguments");
1437 Type[] copy = new Type [typeArguments.Length];
1438 typeArguments.CopyTo (copy, 0);
1439 return pmodule.assemblyb.MakeGenericType (this, copy);
1442 public override Type MakePointerType ()
1444 return new PointerType (this);
1447 public override RuntimeTypeHandle TypeHandle {
1450 return created.TypeHandle;
1454 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1456 if (customBuilder == null)
1457 throw new ArgumentNullException ("customBuilder");
1459 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1460 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1461 byte[] data = customBuilder.Data;
1462 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1463 layout_kind = (int)data [2];
1464 layout_kind |= ((int)data [3]) << 8;
1465 attrs &= ~TypeAttributes.LayoutMask;
1466 switch ((LayoutKind)layout_kind) {
1467 case LayoutKind.Auto:
1468 attrs |= TypeAttributes.AutoLayout;
1470 case LayoutKind.Explicit:
1471 attrs |= TypeAttributes.ExplicitLayout;
1473 case LayoutKind.Sequential:
1474 attrs |= TypeAttributes.SequentialLayout;
1477 // we should ignore it since it can be any value anyway...
1478 throw new Exception ("Error in customattr");
1481 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1483 if (ctor_type.FullName == "System.Int16")
1485 int nnamed = (int)data [pos++];
1486 nnamed |= ((int)data [pos++]) << 8;
1487 for (int i = 0; i < nnamed; ++i) {
1488 //byte named_type = data [pos++];
1490 byte type = data [pos++];
1495 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1496 //string named_typename =
1497 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1499 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1500 // See related code/FIXME in mono/mono/metadata/reflection.c
1503 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1504 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1506 /* all the fields are integers in StructLayout */
1507 int value = (int)data [pos++];
1508 value |= ((int)data [pos++]) << 8;
1509 value |= ((int)data [pos++]) << 16;
1510 value |= ((int)data [pos++]) << 24;
1511 switch (named_name) {
1513 switch ((CharSet)value) {
1516 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1518 case CharSet.Unicode:
1519 attrs &= ~TypeAttributes.AutoClass;
1520 attrs |= TypeAttributes.UnicodeClass;
1523 attrs &= ~TypeAttributes.UnicodeClass;
1524 attrs |= TypeAttributes.AutoClass;
1527 break; // error out...
1531 packing_size = (PackingSize)value;
1537 break; // error out...
1541 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1542 attrs |= TypeAttributes.SpecialName;
1544 } else if (attrname == "System.SerializableAttribute") {
1545 attrs |= TypeAttributes.Serializable;
1547 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1548 attrs |= TypeAttributes.Import;
1550 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1551 attrs |= TypeAttributes.HasSecurity;
1554 if (cattrs != null) {
1555 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1556 cattrs.CopyTo (new_array, 0);
1557 new_array [cattrs.Length] = customBuilder;
1560 cattrs = new CustomAttributeBuilder [1];
1561 cattrs [0] = customBuilder;
1566 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1568 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1571 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1573 check_name ("name", name);
1574 if (eventtype == null)
1575 throw new ArgumentNullException ("type");
1576 check_not_created ();
1578 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1579 if (events != null) {
1580 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1581 System.Array.Copy (events, new_events, events.Length);
1582 new_events [events.Length] = res;
1583 events = new_events;
1585 events = new EventBuilder [1];
1591 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1593 throw new ArgumentNullException ("data");
1595 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1596 res.SetRVAData (data);
1600 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1603 throw new ArgumentNullException ("name");
1604 if (name.Length == 0)
1605 throw new ArgumentException ("Empty name is not legal", "name");
1606 if ((size <= 0) || (size > 0x3f0000))
1607 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1608 check_not_created ();
1610 string typeName = "$ArrayType$" + size;
1611 TypeIdentifier ident = TypeIdentifiers.WithoutEscape (typeName);
1612 Type datablobtype = pmodule.GetRegisteredType (fullname.NestedName(ident));
1613 if (datablobtype == null) {
1614 TypeBuilder tb = DefineNestedType (typeName,
1615 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1616 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1620 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1623 public TypeToken TypeToken {
1625 return new TypeToken (0x02000000 | table_idx);
1629 public void SetParent (Type parent)
1631 check_not_created ();
1633 if (parent == null) {
1634 if ((attrs & TypeAttributes.Interface) != 0) {
1635 if ((attrs & TypeAttributes.Abstract) == 0)
1636 throw new InvalidOperationException ("Interface must be declared abstract.");
1639 this.parent = typeof (object);
1642 this.parent = parent;
1644 this.parent = ResolveUserType (this.parent);
1646 // will just set the parent-related bits if called a second time
1647 setup_internal_class ();
1650 internal int get_next_table_index (object obj, int table, bool inc) {
1651 return pmodule.get_next_table_index (obj, table, inc);
1655 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1657 if (created == null)
1658 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1660 return created.GetInterfaceMap (interfaceType);
1663 internal override Type InternalResolve ()
1669 internal bool is_created {
1671 return createTypeCalled;
1675 private Exception not_supported ()
1677 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1680 private void check_not_created ()
1683 throw new InvalidOperationException ("Unable to change after type has been created.");
1686 private void check_created ()
1689 throw not_supported ();
1692 private void check_name (string argName, string name)
1695 throw new ArgumentNullException (argName);
1696 if (name.Length == 0)
1697 throw new ArgumentException ("Empty name is not legal", argName);
1698 if (name [0] == ((char)0))
1699 throw new ArgumentException ("Illegal name", argName);
1702 public override String ToString ()
1708 public override bool IsAssignableFrom (Type c)
1710 return base.IsAssignableFrom (c);
1713 [MonoTODO ("arrays")]
1714 internal bool IsAssignableTo (Type c)
1719 if (c.IsInterface) {
1720 if (parent != null && is_created) {
1721 if (c.IsAssignableFrom (parent))
1725 if (interfaces == null)
1727 foreach (Type t in interfaces)
1728 if (c.IsAssignableFrom (t))
1735 return c == typeof (object);
1737 return c.IsAssignableFrom (parent);
1740 public bool IsCreated ()
1745 public override Type[] GetGenericArguments ()
1747 if (generic_params == null)
1749 Type[] args = new Type [generic_params.Length];
1750 generic_params.CopyTo (args, 0);
1754 public override Type GetGenericTypeDefinition ()
1756 if (generic_params == null)
1757 throw new InvalidOperationException ("Type is not generic");
1761 public override bool ContainsGenericParameters {
1763 return generic_params != null;
1767 public extern override bool IsGenericParameter {
1768 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1772 public override GenericParameterAttributes GenericParameterAttributes {
1773 get { return GenericParameterAttributes.None; }
1776 public override bool IsGenericTypeDefinition {
1778 return generic_params != null;
1782 public override bool IsGenericType {
1783 get { return IsGenericTypeDefinition; }
1787 public override int GenericParameterPosition {
1793 public override MethodBase DeclaringMethod {
1799 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1802 throw new ArgumentNullException ("names");
1803 if (names.Length == 0)
1804 throw new ArgumentException ("names");
1806 generic_params = new GenericTypeParameterBuilder [names.Length];
1807 for (int i = 0; i < names.Length; i++) {
1808 string item = names [i];
1810 throw new ArgumentNullException ("names");
1811 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1814 return generic_params;
1817 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1819 /*FIXME I would expect the same checks of GetMethod here*/
1821 throw new ArgumentException ("Type is not generic", "type");
1823 if (!type.IsGenericType)
1824 throw new ArgumentException ("Type is not a generic type", "type");
1826 if (type.IsGenericTypeDefinition)
1827 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1829 if (constructor == null)
1830 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1832 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1833 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1834 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1835 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1837 ConstructorInfo res = type.GetConstructor (constructor);
1839 throw new ArgumentException ("constructor not found");
1844 static bool IsValidGetMethodType (Type type)
1846 if (type is TypeBuilder || type is MonoGenericClass)
1848 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1849 if (type.Module is ModuleBuilder)
1851 if (type.IsGenericParameter)
1854 Type[] inst = type.GetGenericArguments ();
1857 for (int i = 0; i < inst.Length; ++i) {
1858 if (IsValidGetMethodType (inst [i]))
1864 public static MethodInfo GetMethod (Type type, MethodInfo method)
1866 if (!IsValidGetMethodType (type))
1867 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1869 if (type is TypeBuilder && type.ContainsGenericParameters)
1870 type = type.MakeGenericType (type.GetGenericArguments ());
1872 if (!type.IsGenericType)
1873 throw new ArgumentException ("type is not a generic type", "type");
1875 if (!method.DeclaringType.IsGenericTypeDefinition)
1876 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1877 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1878 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1880 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1882 MethodInfo res = type.GetMethod (method);
1884 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1889 public static FieldInfo GetField (Type type, FieldInfo field)
1891 if (!type.IsGenericType)
1892 throw new ArgumentException ("Type is not a generic type", "type");
1894 if (type.IsGenericTypeDefinition)
1895 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1897 if (field is FieldOnTypeBuilderInst)
1898 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1900 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1901 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1903 FieldInfo res = type.GetField (field);
1905 throw new System.Exception ("field not found");
1911 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1913 throw new NotImplementedException ();
1916 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1918 throw new NotImplementedException ();
1921 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1923 throw new NotImplementedException ();
1926 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1928 throw new NotImplementedException ();
1931 internal override bool IsUserType {
1937 public override bool IsConstructedGenericType {
1938 get { return false; }
1941 public override bool IsAssignableFrom (TypeInfo typeInfo)
1943 return base.IsAssignableFrom (typeInfo);