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.Collections.Generic;
44 using System.Security;
45 using System.Security.Permissions;
46 using System.Diagnostics.SymbolStore;
49 namespace System.Reflection.Emit
52 [ComDefaultInterface (typeof (_TypeBuilder))]
53 [ClassInterface (ClassInterfaceType.None)]
54 [StructLayout (LayoutKind.Sequential)]
55 public sealed class TypeBuilder : TypeInfo, _TypeBuilder
57 #pragma warning disable 169
58 #region Sync with reflection.h
59 private string tname; // name in internal form
60 private string nspace; // namespace in internal form
62 private Type nesting_type;
63 internal Type[] interfaces;
64 internal int num_methods;
65 internal MethodBuilder[] methods;
66 internal ConstructorBuilder[] ctors;
67 internal PropertyBuilder[] properties;
68 internal int num_fields;
69 internal FieldBuilder[] fields;
70 internal EventBuilder[] events;
71 private CustomAttributeBuilder[] cattrs;
72 internal TypeBuilder[] subtypes;
73 internal TypeAttributes attrs;
74 private int table_idx;
75 private ModuleBuilder pmodule;
76 private int class_size;
77 private PackingSize packing_size;
78 private IntPtr generic_container;
79 private GenericTypeParameterBuilder[] generic_params;
80 private RefEmitPermissionSet[] permissions;
81 private TypeInfo created;
83 #pragma warning restore 169
86 bool createTypeCalled;
87 private Type underlying_type;
89 public const int UnspecifiedTypeSize = 0;
91 protected override TypeAttributes GetAttributeFlagsImpl ()
96 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97 private extern void setup_internal_class ();
99 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100 private extern void create_generic_class ();
102 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
106 this.class_size = UnspecifiedTypeSize;
107 this.table_idx = table_idx;
108 this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
109 this.nspace = String.Empty;
110 this.fullname = TypeIdentifiers.WithoutEscape(this.tname);
112 setup_internal_class ();
115 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
118 this.parent = ResolveUserType (parent);
120 this.class_size = type_size;
121 this.packing_size = packing_size;
122 this.nesting_type = nesting_type;
124 check_name ("fullname", name);
126 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
127 throw new InvalidOperationException ("Interface must be declared abstract.");
129 sep_index = name.LastIndexOf('.');
130 if (sep_index != -1) {
131 this.tname = name.Substring (sep_index + 1);
132 this.nspace = name.Substring (0, sep_index);
135 this.nspace = String.Empty;
137 if (interfaces != null) {
138 this.interfaces = new Type[interfaces.Length];
139 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
143 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
144 this.parent = typeof (object);
147 table_idx = mb.get_next_table_index (this, 0x02, true);
148 setup_internal_class ();
149 fullname = GetFullName ();
152 public override Assembly Assembly {
153 get {return pmodule.Assembly;}
156 public override string AssemblyQualifiedName {
158 return fullname.DisplayName + ", " + Assembly.FullName;
162 public override Type BaseType {
168 public override Type DeclaringType {
169 get { return nesting_type; }
173 public override bool IsSubclassOf (Type c)
189 public override Type UnderlyingSystemType {
192 return created.UnderlyingSystemType;
195 if (underlying_type != null)
196 return underlying_type;
197 throw new InvalidOperationException (
198 "Enumeration type is not defined.");
205 TypeName GetFullName ()
207 TypeIdentifier ident = TypeIdentifiers.FromInternal (tname);
208 if (nesting_type != null)
209 return TypeNames.FromDisplay (nesting_type.FullName).NestedName (ident);
210 if ((nspace != null) && (nspace.Length > 0))
211 return TypeIdentifiers.FromInternal (nspace, ident);
215 public override string FullName {
217 return fullname.DisplayName;
221 public override Guid GUID {
228 public override Module Module {
229 get {return pmodule;}
232 public override string Name {
236 public override string Namespace {
240 public PackingSize PackingSize {
241 get {return packing_size;}
245 get { return class_size; }
248 public override Type ReflectedType {
249 get { return nesting_type; }
252 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
256 throw new ArgumentNullException ("pset");
257 if ((action == SecurityAction.RequestMinimum) ||
258 (action == SecurityAction.RequestOptional) ||
259 (action == SecurityAction.RequestRefuse))
260 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
262 check_not_created ();
264 if (permissions != null) {
265 /* Check duplicate actions */
266 foreach (RefEmitPermissionSet set in permissions)
267 if (set.action == action)
268 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
270 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
271 permissions.CopyTo (new_array, 0);
272 permissions = new_array;
275 permissions = new RefEmitPermissionSet [1];
277 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
278 attrs |= TypeAttributes.HasSecurity;
283 public void AddInterfaceImplementation (Type interfaceType)
285 if (interfaceType == null)
286 throw new ArgumentNullException ("interfaceType");
287 check_not_created ();
289 if (interfaces != null) {
290 // Check for duplicates
291 foreach (Type t in interfaces)
292 if (t == interfaceType)
295 Type[] ifnew = new Type [interfaces.Length + 1];
296 interfaces.CopyTo (ifnew, 0);
297 ifnew [interfaces.Length] = interfaceType;
300 interfaces = new Type [1];
301 interfaces [0] = interfaceType;
305 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
306 CallingConventions callConvention, Type[] types,
307 ParameterModifier[] modifiers)
311 if (created == typeof (object)) {
313 * This happens when building corlib. Calling created.GetConstructor
314 * would return constructors from the real mscorlib, instead of the
321 ConstructorBuilder found = null;
324 foreach (ConstructorBuilder cb in ctors) {
325 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
335 throw new AmbiguousMatchException ();
338 MethodBase[] match = new MethodBase [count];
343 foreach (ConstructorInfo m in ctors) {
344 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
350 binder = DefaultBinder;
351 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
355 return created.GetConstructor (bindingAttr, binder,
356 callConvention, types, modifiers);
359 public override bool IsDefined (Type attributeType, bool inherit)
362 throw new NotSupportedException ();
364 * MS throws NotSupported here, but we can't because some corlib
365 * classes make calls to IsDefined.
367 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
370 public override object[] GetCustomAttributes(bool inherit)
374 return created.GetCustomAttributes (inherit);
377 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
381 return created.GetCustomAttributes (attributeType, inherit);
384 public TypeBuilder DefineNestedType (string name)
386 return DefineNestedType (name, TypeAttributes.NestedPrivate,
387 pmodule.assemblyb.corlib_object_type, null);
390 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
392 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
395 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
397 return DefineNestedType (name, attr, parent, null);
400 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
401 PackingSize packSize, int typeSize)
403 // Visibility must be NestedXXX
405 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
406 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
407 throw new ArgumentException ("attr", "Bad type flags for nested type.");
409 if (interfaces != null)
410 foreach (Type iface in interfaces)
412 throw new ArgumentNullException ("interfaces");
414 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
415 res.fullname = res.GetFullName ();
416 pmodule.RegisterTypeName (res, res.fullname);
417 if (subtypes != null) {
418 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
419 System.Array.Copy (subtypes, new_types, subtypes.Length);
420 new_types [subtypes.Length] = res;
421 subtypes = new_types;
423 subtypes = new TypeBuilder [1];
430 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
432 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
435 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
437 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
440 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
442 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
445 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize,
448 return DefineNestedType (name, attr, parent, null, packSize, typeSize);
452 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
454 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
458 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
460 check_not_created ();
461 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
462 callingConvention, parameterTypes, requiredCustomModifiers,
463 optionalCustomModifiers);
465 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
466 System.Array.Copy (ctors, new_ctors, ctors.Length);
467 new_ctors [ctors.Length] = cb;
470 ctors = new ConstructorBuilder [1];
477 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
479 Type parent_type, old_parent_type;
482 parent_type = parent;
484 parent_type = pmodule.assemblyb.corlib_object_type;
486 old_parent_type = parent_type;
487 parent_type = parent_type.InternalResolve ();
488 /*This avoids corlib to have self references.*/
489 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
490 parent_type = old_parent_type;
492 ConstructorInfo parent_constructor =
493 parent_type.GetConstructor (
494 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
495 null, Type.EmptyTypes, null);
496 if (parent_constructor == null) {
497 throw new NotSupportedException ("Parent does"
498 + " not have a default constructor."
499 + " The default constructor must be"
500 + " explicitly defined.");
503 ConstructorBuilder cb = DefineConstructor (attributes,
504 CallingConventions.Standard, Type.EmptyTypes);
505 ILGenerator ig = cb.GetILGenerator ();
506 ig.Emit (OpCodes.Ldarg_0);
507 ig.Emit (OpCodes.Call, parent_constructor);
508 ig.Emit (OpCodes.Ret);
512 private void append_method (MethodBuilder mb)
514 if (methods != null) {
515 if (methods.Length == num_methods) {
516 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
517 System.Array.Copy (methods, new_methods, num_methods);
518 methods = new_methods;
521 methods = new MethodBuilder [1];
523 methods [num_methods] = mb;
527 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
529 return DefineMethod (name, attributes, CallingConventions.Standard,
530 returnType, parameterTypes);
533 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
535 return DefineMethod (name, attributes, callingConvention, returnType,
536 null, null, parameterTypes, null, null);
539 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
541 check_name ("name", name);
542 check_not_created ();
544 !((attributes & MethodAttributes.Abstract) != 0) ||
545 !((attributes & MethodAttributes.Virtual) != 0)) &&
546 !(((attributes & MethodAttributes.Static) != 0)))
547 throw new ArgumentException ("Interface method must be abstract and virtual.");
549 if (returnType == null)
550 returnType = pmodule.assemblyb.corlib_void_type;
551 MethodBuilder res = new MethodBuilder (this, name, attributes,
552 callingConvention, returnType,
553 returnTypeRequiredCustomModifiers,
554 returnTypeOptionalCustomModifiers, parameterTypes,
555 parameterTypeRequiredCustomModifiers,
556 parameterTypeOptionalCustomModifiers);
561 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
563 return DefinePInvokeMethod (name, dllName, entryName, attributes,
564 callingConvention, returnType, null, null, parameterTypes,
565 null, null, nativeCallConv, nativeCharSet);
568 public MethodBuilder DefinePInvokeMethod (
571 string entryName, MethodAttributes attributes,
572 CallingConventions callingConvention,
574 Type[] returnTypeRequiredCustomModifiers,
575 Type[] returnTypeOptionalCustomModifiers,
576 Type[] parameterTypes,
577 Type[][] parameterTypeRequiredCustomModifiers,
578 Type[][] parameterTypeOptionalCustomModifiers,
579 CallingConvention nativeCallConv,
580 CharSet nativeCharSet)
582 check_name ("name", name);
583 check_name ("dllName", dllName);
584 check_name ("entryName", entryName);
585 if ((attributes & MethodAttributes.Abstract) != 0)
586 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
588 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
589 check_not_created ();
592 = new MethodBuilder (
598 returnTypeRequiredCustomModifiers,
599 returnTypeOptionalCustomModifiers,
601 parameterTypeRequiredCustomModifiers,
602 parameterTypeOptionalCustomModifiers,
611 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
612 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
613 nativeCallConv, nativeCharSet);
616 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
618 return DefineMethod (name, attributes, CallingConventions.Standard);
621 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
623 return DefineMethod (name, attributes, callingConvention, null, null);
626 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
628 if (methodInfoBody == null)
629 throw new ArgumentNullException ("methodInfoBody");
630 if (methodInfoDeclaration == null)
631 throw new ArgumentNullException ("methodInfoDeclaration");
632 check_not_created ();
633 if (methodInfoBody.DeclaringType != this)
634 throw new ArgumentException ("method body must belong to this type");
636 if (methodInfoBody is MethodBuilder) {
637 MethodBuilder mb = (MethodBuilder)methodInfoBody;
638 mb.set_override (methodInfoDeclaration);
642 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
644 return DefineField (fieldName, type, null, null, attributes);
647 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
649 check_name ("fieldName", fieldName);
650 if (type == typeof (void))
651 throw new ArgumentException ("Bad field type in defining field.");
652 check_not_created ();
654 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
655 if (fields != null) {
656 if (fields.Length == num_fields) {
657 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
658 System.Array.Copy (fields, new_fields, num_fields);
661 fields [num_fields] = res;
664 fields = new FieldBuilder [1];
670 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
671 underlying_type = type;
677 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
679 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
682 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
684 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
687 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
689 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
692 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
694 check_name ("name", name);
695 if (parameterTypes != null)
696 foreach (Type param in parameterTypes)
698 throw new ArgumentNullException ("parameterTypes");
699 check_not_created ();
701 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
703 if (properties != null) {
704 Array.Resize (ref properties, properties.Length + 1);
705 properties [properties.Length - 1] = res;
707 properties = new PropertyBuilder [1] { res };
713 public ConstructorBuilder DefineTypeInitializer()
715 return DefineConstructor (MethodAttributes.Public |
716 MethodAttributes.Static | MethodAttributes.SpecialName |
717 MethodAttributes.RTSpecialName, CallingConventions.Standard,
721 [MethodImplAttribute(MethodImplOptions.InternalCall)]
722 private extern TypeInfo create_runtime_class ();
724 private bool is_nested_in (Type t)
735 // Return whenever this type has a ctor defined using DefineMethod ()
736 private bool has_ctor_method () {
737 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
739 for (int i = 0; i < num_methods; ++i) {
740 MethodBuilder mb = (MethodBuilder)(methods[i]);
742 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
749 public Type CreateType ()
751 return CreateTypeInfo ();
755 TypeInfo CreateTypeInfo ()
757 /* handle nesting_type */
758 if (createTypeCalled)
761 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
762 SetParent (pmodule.assemblyb.corlib_object_type);
765 create_generic_class ();
767 // Fire TypeResolve events for fields whose type is an unfinished
769 if (fields != null) {
770 foreach (FieldBuilder fb in fields) {
773 Type ft = fb.FieldType;
774 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
775 TypeBuilder tb = (TypeBuilder)ft;
776 if (!tb.is_created) {
777 AppDomain.CurrentDomain.DoTypeResolve (tb);
778 if (!tb.is_created) {
779 // FIXME: We should throw an exception here,
780 // but mcs expects that the type is created
781 // even if the exception is thrown
782 //throw new TypeLoadException ("Could not load type " + tb);
790 // On classes, define a default constructor if not provided
792 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
793 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
794 DefineDefaultConstructor (MethodAttributes.Public);
796 createTypeCalled = true;
798 if ((parent != null) && parent.IsSealed)
799 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
801 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
802 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
803 if (interfaces != null) {
804 foreach (var iface in interfaces) {
805 if (iface.IsNestedPrivate && iface.Assembly != Assembly)
806 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is implements the inaccessible interface '" + iface.FullName + "'.");
810 if (methods != null) {
811 bool is_concrete = !IsAbstract;
812 for (int i = 0; i < num_methods; ++i) {
813 MethodBuilder mb = (MethodBuilder)(methods[i]);
814 if (is_concrete && mb.IsAbstract)
815 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
816 mb.check_override ();
822 foreach (ConstructorBuilder ctor in ctors)
828 created = create_runtime_class ();
834 void ResolveUserTypes () {
835 parent = ResolveUserType (parent);
836 ResolveUserTypes (interfaces);
837 if (fields != null) {
838 foreach (var fb in fields) {
840 fb.ResolveUserTypes ();
843 if (methods != null) {
844 foreach (var mb in methods) {
846 mb.ResolveUserTypes ();
850 foreach (var cb in ctors) {
852 cb.ResolveUserTypes ();
857 static internal void ResolveUserTypes (Type[] types) {
859 for (int i = 0; i < types.Length; ++i)
860 types [i] = ResolveUserType (types [i]);
863 static internal Type ResolveUserType (Type t) {
864 if (t != null && ((t.GetType ().Assembly != typeof (int).Assembly) || (t is TypeDelegator))) {
865 t = t.UnderlyingSystemType;
866 if (t != null && ((t.GetType ().Assembly != typeof (int).Assembly) || (t is TypeDelegator)))
867 throw new NotSupportedException ("User defined subclasses of System.Type are not yet supported.");
874 internal void FixupTokens (Dictionary<int, int> token_map, Dictionary<int, MemberInfo> member_map) {
875 if (methods != null) {
876 for (int i = 0; i < num_methods; ++i)
877 methods[i].FixupTokens (token_map, member_map);
880 foreach (var cb in ctors)
881 cb.FixupTokens (token_map, member_map);
885 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
887 symbolWriter.OpenNamespace (this.Namespace);
889 if (methods != null) {
890 for (int i = 0; i < num_methods; ++i) {
891 MethodBuilder metb = (MethodBuilder) methods[i];
892 metb.GenerateDebugInfo (symbolWriter);
897 foreach (ConstructorBuilder ctor in ctors)
898 ctor.GenerateDebugInfo (symbolWriter);
901 symbolWriter.CloseNamespace ();
903 if (subtypes != null) {
904 for (int i = 0; i < subtypes.Length; ++i)
905 subtypes [i].GenerateDebugInfo (symbolWriter);
910 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
913 return created.GetConstructors (bindingAttr);
915 throw new NotSupportedException ();
918 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
921 return new ConstructorInfo [0];
922 ArrayList l = new ArrayList ();
924 MethodAttributes mattrs;
926 foreach (ConstructorBuilder c in ctors) {
928 mattrs = c.Attributes;
929 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
930 if ((bindingAttr & BindingFlags.Public) != 0)
933 if ((bindingAttr & BindingFlags.NonPublic) != 0)
939 if ((mattrs & MethodAttributes.Static) != 0) {
940 if ((bindingAttr & BindingFlags.Static) != 0)
943 if ((bindingAttr & BindingFlags.Instance) != 0)
950 ConstructorInfo[] result = new ConstructorInfo [l.Count];
955 public override Type GetElementType ()
957 throw new NotSupportedException ();
960 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
963 return created.GetEvent (name, bindingAttr);
966 /* Needed to keep signature compatibility with MS.NET */
967 public override EventInfo[] GetEvents ()
969 const BindingFlags DefaultBindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
970 return GetEvents (DefaultBindingFlags);
973 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
976 return created.GetEvents (bindingAttr);
977 throw new NotSupportedException ();
980 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
983 return created.GetField (name, bindingAttr);
989 FieldAttributes mattrs;
991 foreach (FieldInfo c in fields) {
997 mattrs = c.Attributes;
998 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
999 if ((bindingAttr & BindingFlags.Public) != 0)
1002 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1008 if ((mattrs & FieldAttributes.Static) != 0) {
1009 if ((bindingAttr & BindingFlags.Static) != 0)
1012 if ((bindingAttr & BindingFlags.Instance) != 0)
1022 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1024 if (created != null)
1025 return created.GetFields (bindingAttr);
1028 return new FieldInfo [0];
1029 ArrayList l = new ArrayList ();
1031 FieldAttributes mattrs;
1033 foreach (FieldInfo c in fields) {
1037 mattrs = c.Attributes;
1038 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1039 if ((bindingAttr & BindingFlags.Public) != 0)
1042 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1048 if ((mattrs & FieldAttributes.Static) != 0) {
1049 if ((bindingAttr & BindingFlags.Static) != 0)
1052 if ((bindingAttr & BindingFlags.Instance) != 0)
1059 FieldInfo[] result = new FieldInfo [l.Count];
1064 public override Type GetInterface (string name, bool ignoreCase)
1067 return created.GetInterface (name, ignoreCase);
1070 public override Type[] GetInterfaces ()
1073 return created.GetInterfaces ();
1075 if (interfaces != null) {
1076 Type[] ret = new Type [interfaces.Length];
1077 interfaces.CopyTo (ret, 0);
1080 return Type.EmptyTypes;
1084 public override MemberInfo[] GetMember (string name, MemberTypes type,
1085 BindingFlags bindingAttr)
1088 return created.GetMember (name, type, bindingAttr);
1091 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1094 return created.GetMembers (bindingAttr);
1097 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1099 MethodInfo[] candidates;
1101 MethodAttributes mattrs;
1103 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1104 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1105 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1107 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1109 for (int i = 0; i < parent_methods.Length; i++) {
1110 MethodInfo m = parent_methods [i];
1112 mattrs = m.Attributes;
1114 if (m.IsStatic && !flatten)
1117 switch (mattrs & MethodAttributes.MemberAccessMask) {
1118 case MethodAttributes.Public:
1119 match = (bindingAttr & BindingFlags.Public) != 0;
1121 case MethodAttributes.Assembly:
1122 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1124 case MethodAttributes.Private:
1128 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1133 parent_candidates.Add (m);
1136 if (methods == null) {
1137 candidates = new MethodInfo [parent_candidates.Count];
1138 parent_candidates.CopyTo (candidates);
1140 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1141 parent_candidates.CopyTo (candidates, 0);
1142 methods.CopyTo (candidates, parent_candidates.Count);
1146 candidates = methods;
1148 if (candidates == null)
1149 return new MethodInfo [0];
1151 ArrayList l = new ArrayList ();
1153 foreach (MethodInfo c in candidates) {
1157 if (String.Compare (c.Name, name, ignoreCase) != 0)
1161 mattrs = c.Attributes;
1162 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1163 if ((bindingAttr & BindingFlags.Public) != 0)
1166 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1172 if ((mattrs & MethodAttributes.Static) != 0) {
1173 if ((bindingAttr & BindingFlags.Static) != 0)
1176 if ((bindingAttr & BindingFlags.Instance) != 0)
1184 MethodInfo[] result = new MethodInfo [l.Count];
1189 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1191 return GetMethodsByName (null, bindingAttr, false, this);
1194 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1196 CallingConventions callConvention,
1197 Type[] types, ParameterModifier[] modifiers)
1202 return created.GetMethod (name, bindingAttr);
1204 return created.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
1207 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1211 if (subtypes == null)
1214 foreach (TypeBuilder t in subtypes) {
1217 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1218 if ((bindingAttr & BindingFlags.Public) == 0)
1221 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1231 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1234 throw new NotSupportedException ();
1237 ArrayList result = new ArrayList ();
1239 if (subtypes == null)
1240 return Type.EmptyTypes;
1241 foreach (TypeBuilder t in subtypes) {
1243 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1244 if ((bindingAttr & BindingFlags.Public) != 0)
1247 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1254 Type[] r = new Type [result.Count];
1259 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1262 return created.GetProperties (bindingAttr);
1264 if (properties == null)
1265 return new PropertyInfo [0];
1266 ArrayList l = new ArrayList ();
1268 MethodAttributes mattrs;
1269 MethodInfo accessor;
1271 foreach (PropertyInfo c in properties) {
1273 accessor = c.GetGetMethod (true);
1274 if (accessor == null)
1275 accessor = c.GetSetMethod (true);
1276 if (accessor == null)
1278 mattrs = accessor.Attributes;
1279 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1280 if ((bindingAttr & BindingFlags.Public) != 0)
1283 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1289 if ((mattrs & MethodAttributes.Static) != 0) {
1290 if ((bindingAttr & BindingFlags.Static) != 0)
1293 if ((bindingAttr & BindingFlags.Instance) != 0)
1300 PropertyInfo[] result = new PropertyInfo [l.Count];
1305 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1307 throw not_supported ();
1310 protected override bool HasElementTypeImpl ()
1312 // a TypeBuilder can never represent an array, pointer
1316 return created.HasElementType;
1319 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1322 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1325 protected override bool IsArrayImpl ()
1327 return false; /*A TypeBuilder never represents a non typedef type.*/
1330 protected override bool IsByRefImpl ()
1332 return false; /*A TypeBuilder never represents a non typedef type.*/
1335 protected override bool IsCOMObjectImpl ()
1337 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1340 protected override bool IsPointerImpl ()
1342 return false; /*A TypeBuilder never represents a non typedef type.*/
1345 protected override bool IsPrimitiveImpl ()
1351 // FIXME: I doubt just removing this still works.
1352 protected override bool IsValueTypeImpl ()
1354 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1356 Type parent_type = parent;
1357 while (parent_type != null) {
1358 if (parent_type == pmodule.assemblyb.corlib_value_type)
1360 parent_type = parent_type.BaseType;
1365 public override Type MakeArrayType ()
1367 return new ArrayType (this, 0);
1370 public override Type MakeArrayType (int rank)
1373 throw new IndexOutOfRangeException ();
1374 return new ArrayType (this, rank);
1377 public override Type MakeByRefType ()
1379 return new ByRefType (this);
1382 public override Type MakeGenericType (params Type [] typeArguments)
1384 //return base.MakeGenericType (typeArguments);
1386 if (!IsGenericTypeDefinition)
1387 throw new InvalidOperationException ("not a generic type definition");
1388 if (typeArguments == null)
1389 throw new ArgumentNullException ("typeArguments");
1391 if (generic_params.Length != typeArguments.Length)
1392 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");
1394 foreach (Type t in typeArguments) {
1396 throw new ArgumentNullException ("typeArguments");
1399 Type[] copy = new Type [typeArguments.Length];
1400 typeArguments.CopyTo (copy, 0);
1401 return pmodule.assemblyb.MakeGenericType (this, copy);
1404 public override Type MakePointerType ()
1406 return new PointerType (this);
1409 public override RuntimeTypeHandle TypeHandle {
1412 return created.TypeHandle;
1416 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1418 if (customBuilder == null)
1419 throw new ArgumentNullException ("customBuilder");
1421 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1422 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1423 byte[] data = customBuilder.Data;
1424 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1425 layout_kind = (int)data [2];
1426 layout_kind |= ((int)data [3]) << 8;
1427 attrs &= ~TypeAttributes.LayoutMask;
1428 switch ((LayoutKind)layout_kind) {
1429 case LayoutKind.Auto:
1430 attrs |= TypeAttributes.AutoLayout;
1432 case LayoutKind.Explicit:
1433 attrs |= TypeAttributes.ExplicitLayout;
1435 case LayoutKind.Sequential:
1436 attrs |= TypeAttributes.SequentialLayout;
1439 // we should ignore it since it can be any value anyway...
1440 throw new Exception ("Error in customattr");
1443 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1445 if (ctor_type.FullName == "System.Int16")
1447 int nnamed = (int)data [pos++];
1448 nnamed |= ((int)data [pos++]) << 8;
1449 for (int i = 0; i < nnamed; ++i) {
1450 //byte named_type = data [pos++];
1452 byte type = data [pos++];
1457 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1458 //string named_typename =
1459 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1461 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1462 // See related code/FIXME in mono/mono/metadata/reflection.c
1465 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1466 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1468 /* all the fields are integers in StructLayout */
1469 int value = (int)data [pos++];
1470 value |= ((int)data [pos++]) << 8;
1471 value |= ((int)data [pos++]) << 16;
1472 value |= ((int)data [pos++]) << 24;
1473 switch (named_name) {
1475 switch ((CharSet)value) {
1478 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1480 case CharSet.Unicode:
1481 attrs &= ~TypeAttributes.AutoClass;
1482 attrs |= TypeAttributes.UnicodeClass;
1485 attrs &= ~TypeAttributes.UnicodeClass;
1486 attrs |= TypeAttributes.AutoClass;
1489 break; // error out...
1493 packing_size = (PackingSize)value;
1499 break; // error out...
1503 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1504 attrs |= TypeAttributes.SpecialName;
1506 } else if (attrname == "System.SerializableAttribute") {
1507 attrs |= TypeAttributes.Serializable;
1509 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1510 attrs |= TypeAttributes.Import;
1512 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1513 attrs |= TypeAttributes.HasSecurity;
1516 if (cattrs != null) {
1517 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1518 cattrs.CopyTo (new_array, 0);
1519 new_array [cattrs.Length] = customBuilder;
1522 cattrs = new CustomAttributeBuilder [1];
1523 cattrs [0] = customBuilder;
1528 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1530 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1533 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1535 check_name ("name", name);
1536 if (eventtype == null)
1537 throw new ArgumentNullException ("type");
1538 check_not_created ();
1540 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1541 if (events != null) {
1542 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1543 System.Array.Copy (events, new_events, events.Length);
1544 new_events [events.Length] = res;
1545 events = new_events;
1547 events = new EventBuilder [1];
1553 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1555 throw new ArgumentNullException ("data");
1557 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1558 res.SetRVAData (data);
1562 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1565 throw new ArgumentNullException ("name");
1566 if (name.Length == 0)
1567 throw new ArgumentException ("Empty name is not legal", "name");
1568 if ((size <= 0) || (size > 0x3f0000))
1569 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1570 check_not_created ();
1572 string typeName = "$ArrayType$" + size;
1573 TypeIdentifier ident = TypeIdentifiers.WithoutEscape (typeName);
1574 Type datablobtype = pmodule.GetRegisteredType (fullname.NestedName(ident));
1575 if (datablobtype == null) {
1576 TypeBuilder tb = DefineNestedType (typeName,
1577 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1578 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1582 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1585 public TypeToken TypeToken {
1587 return new TypeToken (0x02000000 | table_idx);
1591 public void SetParent (Type parent)
1593 check_not_created ();
1595 if (parent == null) {
1596 if ((attrs & TypeAttributes.Interface) != 0) {
1597 if ((attrs & TypeAttributes.Abstract) == 0)
1598 throw new InvalidOperationException ("Interface must be declared abstract.");
1601 this.parent = typeof (object);
1604 this.parent = parent;
1606 this.parent = ResolveUserType (this.parent);
1608 // will just set the parent-related bits if called a second time
1609 setup_internal_class ();
1612 internal int get_next_table_index (object obj, int table, bool inc) {
1613 return pmodule.get_next_table_index (obj, table, inc);
1617 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1619 if (created == null)
1620 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1622 return created.GetInterfaceMap (interfaceType);
1625 internal override Type InternalResolve ()
1631 internal override Type RuntimeResolve ()
1637 internal bool is_created {
1639 return createTypeCalled;
1643 private Exception not_supported ()
1645 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1648 private void check_not_created ()
1651 throw new InvalidOperationException ("Unable to change after type has been created.");
1654 private void check_created ()
1657 throw not_supported ();
1660 private void check_name (string argName, string name)
1663 throw new ArgumentNullException (argName);
1664 if (name.Length == 0)
1665 throw new ArgumentException ("Empty name is not legal", argName);
1666 if (name [0] == ((char)0))
1667 throw new ArgumentException ("Illegal name", argName);
1670 public override String ToString ()
1676 public override bool IsAssignableFrom (Type c)
1678 return base.IsAssignableFrom (c);
1681 [MonoTODO ("arrays")]
1682 internal bool IsAssignableTo (Type c)
1687 if (c.IsInterface) {
1688 if (parent != null && is_created) {
1689 if (c.IsAssignableFrom (parent))
1693 if (interfaces == null)
1695 foreach (Type t in interfaces)
1696 if (c.IsAssignableFrom (t))
1703 return c == typeof (object);
1705 return c.IsAssignableFrom (parent);
1708 public bool IsCreated ()
1713 public override Type[] GetGenericArguments ()
1715 if (generic_params == null)
1717 Type[] args = new Type [generic_params.Length];
1718 generic_params.CopyTo (args, 0);
1722 public override Type GetGenericTypeDefinition ()
1724 if (generic_params == null)
1725 throw new InvalidOperationException ("Type is not generic");
1729 public override bool ContainsGenericParameters {
1731 return generic_params != null;
1735 public override bool IsGenericParameter {
1741 public override GenericParameterAttributes GenericParameterAttributes {
1742 get { return GenericParameterAttributes.None; }
1745 public override bool IsGenericTypeDefinition {
1747 return generic_params != null;
1751 public override bool IsGenericType {
1752 get { return IsGenericTypeDefinition; }
1756 public override int GenericParameterPosition {
1762 public override MethodBase DeclaringMethod {
1768 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1771 throw new ArgumentNullException ("names");
1772 if (names.Length == 0)
1773 throw new ArgumentException ("names");
1775 generic_params = new GenericTypeParameterBuilder [names.Length];
1776 for (int i = 0; i < names.Length; i++) {
1777 string item = names [i];
1779 throw new ArgumentNullException ("names");
1780 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1783 return generic_params;
1786 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1788 /*FIXME I would expect the same checks of GetMethod here*/
1790 throw new ArgumentException ("Type is not generic", "type");
1792 if (!type.IsGenericType)
1793 throw new ArgumentException ("Type is not a generic type", "type");
1795 if (type.IsGenericTypeDefinition)
1796 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1798 if (constructor == null)
1799 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1801 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1802 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1803 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1804 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1806 ConstructorInfo res = type.GetConstructor (constructor);
1808 throw new ArgumentException ("constructor not found");
1813 static bool IsValidGetMethodType (Type type)
1815 if (type is TypeBuilder || type is MonoGenericClass)
1817 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1818 if (type.Module is ModuleBuilder)
1820 if (type.IsGenericParameter)
1823 Type[] inst = type.GetGenericArguments ();
1826 for (int i = 0; i < inst.Length; ++i) {
1827 if (IsValidGetMethodType (inst [i]))
1833 public static MethodInfo GetMethod (Type type, MethodInfo method)
1835 if (!IsValidGetMethodType (type))
1836 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1838 if (type is TypeBuilder && type.ContainsGenericParameters)
1839 type = type.MakeGenericType (type.GetGenericArguments ());
1841 if (!type.IsGenericType)
1842 throw new ArgumentException ("type is not a generic type", "type");
1844 if (!method.DeclaringType.IsGenericTypeDefinition)
1845 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1846 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1847 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1849 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1851 MethodInfo res = type.GetMethod (method);
1853 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1858 public static FieldInfo GetField (Type type, FieldInfo field)
1860 if (!type.IsGenericType)
1861 throw new ArgumentException ("Type is not a generic type", "type");
1863 if (type.IsGenericTypeDefinition)
1864 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1866 if (field is FieldOnTypeBuilderInst)
1867 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1869 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1870 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1872 FieldInfo res = type.GetField (field);
1874 throw new System.Exception ("field not found");
1880 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1882 throw new NotImplementedException ();
1885 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1887 throw new NotImplementedException ();
1890 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1892 throw new NotImplementedException ();
1895 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1897 throw new NotImplementedException ();
1900 internal override bool IsUserType {
1906 public override bool IsConstructedGenericType {
1907 get { return false; }
1910 public override bool IsAssignableFrom (TypeInfo typeInfo)
1912 return base.IsAssignableFrom (typeInfo);