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.");
811 if (interfaces != null) {
812 foreach (var iface in interfaces) {
813 if (iface.IsNestedPrivate && iface.Assembly != Assembly)
814 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is implements the inaccessible interface '" + iface.FullName + "'.");
818 if (methods != null) {
819 bool is_concrete = !IsAbstract;
820 for (int i = 0; i < num_methods; ++i) {
821 MethodBuilder mb = (MethodBuilder)(methods[i]);
822 if (is_concrete && mb.IsAbstract)
823 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
824 mb.check_override ();
830 foreach (ConstructorBuilder ctor in ctors)
834 created = create_runtime_class (this);
840 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
842 symbolWriter.OpenNamespace (this.Namespace);
844 if (methods != null) {
845 for (int i = 0; i < num_methods; ++i) {
846 MethodBuilder metb = (MethodBuilder) methods[i];
847 metb.GenerateDebugInfo (symbolWriter);
852 foreach (ConstructorBuilder ctor in ctors)
853 ctor.GenerateDebugInfo (symbolWriter);
856 symbolWriter.CloseNamespace ();
858 if (subtypes != null) {
859 for (int i = 0; i < subtypes.Length; ++i)
860 subtypes [i].GenerateDebugInfo (symbolWriter);
865 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
868 return created.GetConstructors (bindingAttr);
870 throw new NotSupportedException ();
873 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
876 return new ConstructorInfo [0];
877 ArrayList l = new ArrayList ();
879 MethodAttributes mattrs;
881 foreach (ConstructorBuilder c in ctors) {
883 mattrs = c.Attributes;
884 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
885 if ((bindingAttr & BindingFlags.Public) != 0)
888 if ((bindingAttr & BindingFlags.NonPublic) != 0)
894 if ((mattrs & MethodAttributes.Static) != 0) {
895 if ((bindingAttr & BindingFlags.Static) != 0)
898 if ((bindingAttr & BindingFlags.Instance) != 0)
905 ConstructorInfo[] result = new ConstructorInfo [l.Count];
910 public override Type GetElementType ()
912 throw new NotSupportedException ();
915 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
918 return created.GetEvent (name, bindingAttr);
921 /* Needed to keep signature compatibility with MS.NET */
922 public override EventInfo[] GetEvents ()
924 return GetEvents (DefaultBindingFlags);
927 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
930 return created.GetEvents (bindingAttr);
931 throw new NotSupportedException ();
934 // This is only used from MonoGenericInst.initialize().
935 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
938 return new EventInfo [0];
939 ArrayList l = new ArrayList ();
941 MethodAttributes mattrs;
944 foreach (EventBuilder eb in events) {
947 EventInfo c = get_event_info (eb);
949 accessor = c.GetAddMethod (true);
950 if (accessor == null)
951 accessor = c.GetRemoveMethod (true);
952 if (accessor == null)
954 mattrs = accessor.Attributes;
955 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
956 if ((bindingAttr & BindingFlags.Public) != 0)
959 if ((bindingAttr & BindingFlags.NonPublic) != 0)
965 if ((mattrs & MethodAttributes.Static) != 0) {
966 if ((bindingAttr & BindingFlags.Static) != 0)
969 if ((bindingAttr & BindingFlags.Instance) != 0)
976 EventInfo[] result = new EventInfo [l.Count];
981 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
984 return created.GetField (name, bindingAttr);
990 FieldAttributes mattrs;
992 foreach (FieldInfo c in fields) {
998 mattrs = c.Attributes;
999 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1000 if ((bindingAttr & BindingFlags.Public) != 0)
1003 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1009 if ((mattrs & FieldAttributes.Static) != 0) {
1010 if ((bindingAttr & BindingFlags.Static) != 0)
1013 if ((bindingAttr & BindingFlags.Instance) != 0)
1023 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1025 if (created != null)
1026 return created.GetFields (bindingAttr);
1029 return new FieldInfo [0];
1030 ArrayList l = new ArrayList ();
1032 FieldAttributes mattrs;
1034 foreach (FieldInfo c in fields) {
1038 mattrs = c.Attributes;
1039 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1040 if ((bindingAttr & BindingFlags.Public) != 0)
1043 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1049 if ((mattrs & FieldAttributes.Static) != 0) {
1050 if ((bindingAttr & BindingFlags.Static) != 0)
1053 if ((bindingAttr & BindingFlags.Instance) != 0)
1060 FieldInfo[] result = new FieldInfo [l.Count];
1065 public override Type GetInterface (string name, bool ignoreCase)
1068 return created.GetInterface (name, ignoreCase);
1071 public override Type[] GetInterfaces ()
1074 return created.GetInterfaces ();
1076 if (interfaces != null) {
1077 Type[] ret = new Type [interfaces.Length];
1078 interfaces.CopyTo (ret, 0);
1081 return Type.EmptyTypes;
1085 public override MemberInfo[] GetMember (string name, MemberTypes type,
1086 BindingFlags bindingAttr)
1089 return created.GetMember (name, type, bindingAttr);
1092 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1095 return created.GetMembers (bindingAttr);
1098 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1100 MethodInfo[] candidates;
1102 MethodAttributes mattrs;
1104 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1105 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1106 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1108 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1110 for (int i = 0; i < parent_methods.Length; i++) {
1111 MethodInfo m = parent_methods [i];
1113 mattrs = m.Attributes;
1115 if (m.IsStatic && !flatten)
1118 switch (mattrs & MethodAttributes.MemberAccessMask) {
1119 case MethodAttributes.Public:
1120 match = (bindingAttr & BindingFlags.Public) != 0;
1122 case MethodAttributes.Assembly:
1123 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1125 case MethodAttributes.Private:
1129 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1134 parent_candidates.Add (m);
1137 if (methods == null) {
1138 candidates = new MethodInfo [parent_candidates.Count];
1139 parent_candidates.CopyTo (candidates);
1141 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1142 parent_candidates.CopyTo (candidates, 0);
1143 methods.CopyTo (candidates, parent_candidates.Count);
1147 candidates = methods;
1149 if (candidates == null)
1150 return new MethodInfo [0];
1152 ArrayList l = new ArrayList ();
1154 foreach (MethodInfo c in candidates) {
1158 if (String.Compare (c.Name, name, ignoreCase) != 0)
1162 mattrs = c.Attributes;
1163 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1164 if ((bindingAttr & BindingFlags.Public) != 0)
1167 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1173 if ((mattrs & MethodAttributes.Static) != 0) {
1174 if ((bindingAttr & BindingFlags.Static) != 0)
1177 if ((bindingAttr & BindingFlags.Instance) != 0)
1185 MethodInfo[] result = new MethodInfo [l.Count];
1190 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1192 return GetMethodsByName (null, bindingAttr, false, this);
1195 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1197 CallingConventions callConvention,
1198 Type[] types, ParameterModifier[] modifiers)
1202 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
1203 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
1204 MethodInfo found = null;
1206 int typesLen = (types != null) ? types.Length : 0;
1209 foreach (MethodInfo m in methods) {
1210 // Under MS.NET, Standard|HasThis matches Standard...
1211 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1220 if (count == 1 && typesLen == 0)
1223 match = new MethodBase [count];
1228 foreach (MethodInfo m in methods) {
1229 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
1231 match [count++] = m;
1236 return (MethodInfo) Binder.FindMostDerivedMatch (match);
1239 binder = Binder.DefaultBinder;
1241 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
1244 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1248 if (subtypes == null)
1251 foreach (TypeBuilder t in subtypes) {
1254 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1255 if ((bindingAttr & BindingFlags.Public) == 0)
1258 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1268 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1271 throw new NotSupportedException ();
1274 ArrayList result = new ArrayList ();
1276 if (subtypes == null)
1277 return Type.EmptyTypes;
1278 foreach (TypeBuilder t in subtypes) {
1280 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1281 if ((bindingAttr & BindingFlags.Public) != 0)
1284 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1291 Type[] r = new Type [result.Count];
1296 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1299 return created.GetProperties (bindingAttr);
1301 if (properties == null)
1302 return new PropertyInfo [0];
1303 ArrayList l = new ArrayList ();
1305 MethodAttributes mattrs;
1306 MethodInfo accessor;
1308 foreach (PropertyInfo c in properties) {
1310 accessor = c.GetGetMethod (true);
1311 if (accessor == null)
1312 accessor = c.GetSetMethod (true);
1313 if (accessor == null)
1315 mattrs = accessor.Attributes;
1316 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1317 if ((bindingAttr & BindingFlags.Public) != 0)
1320 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1326 if ((mattrs & MethodAttributes.Static) != 0) {
1327 if ((bindingAttr & BindingFlags.Static) != 0)
1330 if ((bindingAttr & BindingFlags.Instance) != 0)
1337 PropertyInfo[] result = new PropertyInfo [l.Count];
1342 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1344 throw not_supported ();
1347 protected override bool HasElementTypeImpl ()
1349 // a TypeBuilder can never represent an array, pointer
1353 return created.HasElementType;
1356 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1359 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1362 protected override bool IsArrayImpl ()
1364 return false; /*A TypeBuilder never represents a non typedef type.*/
1367 protected override bool IsByRefImpl ()
1369 return false; /*A TypeBuilder never represents a non typedef type.*/
1372 protected override bool IsCOMObjectImpl ()
1374 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1377 protected override bool IsPointerImpl ()
1379 return false; /*A TypeBuilder never represents a non typedef type.*/
1382 protected override bool IsPrimitiveImpl ()
1388 // FIXME: I doubt just removing this still works.
1389 protected override bool IsValueTypeImpl ()
1391 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1393 Type parent_type = parent;
1394 while (parent_type != null) {
1395 if (parent_type == pmodule.assemblyb.corlib_value_type)
1397 parent_type = parent_type.BaseType;
1402 public override Type MakeArrayType ()
1404 return new ArrayType (this, 0);
1407 public override Type MakeArrayType (int rank)
1410 throw new IndexOutOfRangeException ();
1411 return new ArrayType (this, rank);
1414 public override Type MakeByRefType ()
1416 return new ByRefType (this);
1419 public override Type MakeGenericType (params Type [] typeArguments)
1421 //return base.MakeGenericType (typeArguments);
1423 if (!IsGenericTypeDefinition)
1424 throw new InvalidOperationException ("not a generic type definition");
1425 if (typeArguments == null)
1426 throw new ArgumentNullException ("typeArguments");
1428 if (generic_params.Length != typeArguments.Length)
1429 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");
1431 foreach (Type t in typeArguments) {
1433 throw new ArgumentNullException ("typeArguments");
1436 Type[] copy = new Type [typeArguments.Length];
1437 typeArguments.CopyTo (copy, 0);
1438 return pmodule.assemblyb.MakeGenericType (this, copy);
1441 public override Type MakePointerType ()
1443 return new PointerType (this);
1446 public override RuntimeTypeHandle TypeHandle {
1449 return created.TypeHandle;
1453 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1455 if (customBuilder == null)
1456 throw new ArgumentNullException ("customBuilder");
1458 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1459 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1460 byte[] data = customBuilder.Data;
1461 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1462 layout_kind = (int)data [2];
1463 layout_kind |= ((int)data [3]) << 8;
1464 attrs &= ~TypeAttributes.LayoutMask;
1465 switch ((LayoutKind)layout_kind) {
1466 case LayoutKind.Auto:
1467 attrs |= TypeAttributes.AutoLayout;
1469 case LayoutKind.Explicit:
1470 attrs |= TypeAttributes.ExplicitLayout;
1472 case LayoutKind.Sequential:
1473 attrs |= TypeAttributes.SequentialLayout;
1476 // we should ignore it since it can be any value anyway...
1477 throw new Exception ("Error in customattr");
1480 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1482 if (ctor_type.FullName == "System.Int16")
1484 int nnamed = (int)data [pos++];
1485 nnamed |= ((int)data [pos++]) << 8;
1486 for (int i = 0; i < nnamed; ++i) {
1487 //byte named_type = data [pos++];
1489 byte type = data [pos++];
1494 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1495 //string named_typename =
1496 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1498 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1499 // See related code/FIXME in mono/mono/metadata/reflection.c
1502 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1503 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1505 /* all the fields are integers in StructLayout */
1506 int value = (int)data [pos++];
1507 value |= ((int)data [pos++]) << 8;
1508 value |= ((int)data [pos++]) << 16;
1509 value |= ((int)data [pos++]) << 24;
1510 switch (named_name) {
1512 switch ((CharSet)value) {
1515 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1517 case CharSet.Unicode:
1518 attrs &= ~TypeAttributes.AutoClass;
1519 attrs |= TypeAttributes.UnicodeClass;
1522 attrs &= ~TypeAttributes.UnicodeClass;
1523 attrs |= TypeAttributes.AutoClass;
1526 break; // error out...
1530 packing_size = (PackingSize)value;
1536 break; // error out...
1540 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1541 attrs |= TypeAttributes.SpecialName;
1543 } else if (attrname == "System.SerializableAttribute") {
1544 attrs |= TypeAttributes.Serializable;
1546 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1547 attrs |= TypeAttributes.Import;
1549 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1550 attrs |= TypeAttributes.HasSecurity;
1553 if (cattrs != null) {
1554 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1555 cattrs.CopyTo (new_array, 0);
1556 new_array [cattrs.Length] = customBuilder;
1559 cattrs = new CustomAttributeBuilder [1];
1560 cattrs [0] = customBuilder;
1565 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1567 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1570 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1572 check_name ("name", name);
1573 if (eventtype == null)
1574 throw new ArgumentNullException ("type");
1575 check_not_created ();
1577 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1578 if (events != null) {
1579 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1580 System.Array.Copy (events, new_events, events.Length);
1581 new_events [events.Length] = res;
1582 events = new_events;
1584 events = new EventBuilder [1];
1590 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1592 throw new ArgumentNullException ("data");
1594 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1595 res.SetRVAData (data);
1599 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1602 throw new ArgumentNullException ("name");
1603 if (name.Length == 0)
1604 throw new ArgumentException ("Empty name is not legal", "name");
1605 if ((size <= 0) || (size > 0x3f0000))
1606 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1607 check_not_created ();
1609 string typeName = "$ArrayType$" + size;
1610 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1611 if (datablobtype == null) {
1612 TypeBuilder tb = DefineNestedType (typeName,
1613 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1614 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1618 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1621 public TypeToken TypeToken {
1623 return new TypeToken (0x02000000 | table_idx);
1627 public void SetParent (Type parent)
1629 check_not_created ();
1631 if (parent == null) {
1632 if ((attrs & TypeAttributes.Interface) != 0) {
1633 if ((attrs & TypeAttributes.Abstract) == 0)
1634 throw new InvalidOperationException ("Interface must be declared abstract.");
1637 this.parent = typeof (object);
1640 this.parent = parent;
1643 // will just set the parent-related bits if called a second time
1644 setup_internal_class (this);
1647 internal int get_next_table_index (object obj, int table, bool inc) {
1648 return pmodule.get_next_table_index (obj, table, inc);
1652 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1654 if (created == null)
1655 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1657 return created.GetInterfaceMap (interfaceType);
1660 internal override Type InternalResolve ()
1666 internal bool is_created {
1668 return createTypeCalled;
1672 private Exception not_supported ()
1674 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1677 private void check_not_created ()
1680 throw new InvalidOperationException ("Unable to change after type has been created.");
1683 private void check_created ()
1686 throw not_supported ();
1689 private void check_name (string argName, string name)
1692 throw new ArgumentNullException (argName);
1693 if (name.Length == 0)
1694 throw new ArgumentException ("Empty name is not legal", argName);
1695 if (name [0] == ((char)0))
1696 throw new ArgumentException ("Illegal name", argName);
1699 public override String ToString ()
1705 public override bool IsAssignableFrom (Type c)
1707 return base.IsAssignableFrom (c);
1710 [MonoTODO ("arrays")]
1711 internal bool IsAssignableTo (Type c)
1716 if (c.IsInterface) {
1717 if (parent != null && is_created) {
1718 if (c.IsAssignableFrom (parent))
1722 if (interfaces == null)
1724 foreach (Type t in interfaces)
1725 if (c.IsAssignableFrom (t))
1732 return c == typeof (object);
1734 return c.IsAssignableFrom (parent);
1737 public bool IsCreated ()
1742 public override Type[] GetGenericArguments ()
1744 if (generic_params == null)
1746 Type[] args = new Type [generic_params.Length];
1747 generic_params.CopyTo (args, 0);
1751 public override Type GetGenericTypeDefinition ()
1753 if (generic_params == null)
1754 throw new InvalidOperationException ("Type is not generic");
1758 public override bool ContainsGenericParameters {
1760 return generic_params != null;
1764 public extern override bool IsGenericParameter {
1765 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1769 public override GenericParameterAttributes GenericParameterAttributes {
1770 get { return GenericParameterAttributes.None; }
1773 public override bool IsGenericTypeDefinition {
1775 return generic_params != null;
1779 public override bool IsGenericType {
1780 get { return IsGenericTypeDefinition; }
1784 public override int GenericParameterPosition {
1790 public override MethodBase DeclaringMethod {
1796 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1799 throw new ArgumentNullException ("names");
1800 if (names.Length == 0)
1801 throw new ArgumentException ("names");
1803 setup_generic_class ();
1805 generic_params = new GenericTypeParameterBuilder [names.Length];
1806 for (int i = 0; i < names.Length; i++) {
1807 string item = names [i];
1809 throw new ArgumentNullException ("names");
1810 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1813 return generic_params;
1816 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1818 /*FIXME I would expect the same checks of GetMethod here*/
1820 throw new ArgumentException ("Type is not generic", "type");
1822 if (!type.IsGenericType)
1823 throw new ArgumentException ("Type is not a generic type", "type");
1825 if (type.IsGenericTypeDefinition)
1826 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1828 if (constructor == null)
1829 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1831 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1832 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1833 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1834 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1836 ConstructorInfo res = type.GetConstructor (constructor);
1838 throw new ArgumentException ("constructor not found");
1843 static bool IsValidGetMethodType (Type type)
1845 if (type is TypeBuilder || type is MonoGenericClass)
1847 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1848 if (type.Module is ModuleBuilder)
1850 if (type.IsGenericParameter)
1853 Type[] inst = type.GetGenericArguments ();
1856 for (int i = 0; i < inst.Length; ++i) {
1857 if (IsValidGetMethodType (inst [i]))
1863 public static MethodInfo GetMethod (Type type, MethodInfo method)
1865 if (!IsValidGetMethodType (type))
1866 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1868 if (type is TypeBuilder && type.ContainsGenericParameters)
1869 type = type.MakeGenericType (type.GetGenericArguments ());
1871 if (!type.IsGenericType)
1872 throw new ArgumentException ("type is not a generic type", "type");
1874 if (!method.DeclaringType.IsGenericTypeDefinition)
1875 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1876 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1877 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1879 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1881 MethodInfo res = type.GetMethod (method);
1883 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1888 public static FieldInfo GetField (Type type, FieldInfo field)
1890 if (!type.IsGenericType)
1891 throw new ArgumentException ("Type is not a generic type", "type");
1893 if (type.IsGenericTypeDefinition)
1894 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1896 if (field is FieldOnTypeBuilderInst)
1897 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1899 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1900 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1902 FieldInfo res = type.GetField (field);
1904 throw new System.Exception ("field not found");
1909 internal TypeCode GetTypeCodeInternal () {
1910 if (parent == pmodule.assemblyb.corlib_enum_type) {
1911 for (int i = 0; i < num_fields; ++i) {
1912 FieldBuilder f = fields [i];
1914 return Type.GetTypeCode (f.FieldType);
1916 throw new InvalidOperationException ("Enum basetype field not defined");
1918 return Type.GetTypeCodeInternal (this);
1923 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1925 throw new NotImplementedException ();
1928 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1930 throw new NotImplementedException ();
1933 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1935 throw new NotImplementedException ();
1938 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1940 throw new NotImplementedException ();
1943 internal override bool IsUserType {
1950 public override bool IsConstructedGenericType {
1951 get { return false; }
1954 public override bool IsAssignableFrom (TypeInfo typeInfo)
1956 return base.IsAssignableFrom (typeInfo);