2 // System.Reflection.Emit.TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41 using System.Globalization;
42 using System.Collections;
43 using System.Security;
44 using System.Security.Permissions;
45 using System.Diagnostics.SymbolStore;
48 namespace System.Reflection.Emit
51 [ComDefaultInterface (typeof (_TypeBuilder))]
52 [ClassInterface (ClassInterfaceType.None)]
53 [StructLayout (LayoutKind.Sequential)]
54 public sealed class TypeBuilder : TypeInfo, _TypeBuilder
56 #pragma warning disable 169
57 #region Sync with reflection.h
59 private string nspace;
61 private Type nesting_type;
62 internal Type[] interfaces;
63 internal int num_methods;
64 internal MethodBuilder[] methods;
65 internal ConstructorBuilder[] ctors;
66 internal PropertyBuilder[] properties;
67 internal int num_fields;
68 internal FieldBuilder[] fields;
69 internal EventBuilder[] events;
70 private CustomAttributeBuilder[] cattrs;
71 internal TypeBuilder[] subtypes;
72 internal TypeAttributes attrs;
73 private int table_idx;
74 private ModuleBuilder pmodule;
75 private int class_size;
76 private PackingSize packing_size;
77 private IntPtr generic_container;
78 private GenericTypeParameterBuilder[] generic_params;
79 private RefEmitPermissionSet[] permissions;
80 private TypeInfo created;
82 #pragma warning restore 169
85 bool createTypeCalled;
86 private Type underlying_type;
88 public const int UnspecifiedTypeSize = 0;
90 protected override TypeAttributes GetAttributeFlagsImpl ()
95 [MethodImplAttribute(MethodImplOptions.InternalCall)]
96 private extern void setup_internal_class (TypeBuilder tb);
98 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99 private extern void create_internal_class (TypeBuilder tb);
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 private extern void setup_generic_class ();
104 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105 private extern void create_generic_class ();
107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108 private extern EventInfo get_event_info (EventBuilder eb);
110 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr, int table_idx)
114 this.class_size = UnspecifiedTypeSize;
115 this.table_idx = table_idx;
116 fullname = this.tname = table_idx == 1 ? "<Module>" : "type_" + table_idx.ToString ();
117 this.nspace = String.Empty;
119 setup_internal_class (this);
122 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type)
125 this.parent = parent;
127 this.class_size = type_size;
128 this.packing_size = packing_size;
129 this.nesting_type = nesting_type;
131 check_name ("fullname", name);
133 if (parent == null && (attr & TypeAttributes.Interface) != 0 && (attr & TypeAttributes.Abstract) == 0)
134 throw new InvalidOperationException ("Interface must be declared abstract.");
136 sep_index = name.LastIndexOf('.');
137 if (sep_index != -1) {
138 this.tname = name.Substring (sep_index + 1);
139 this.nspace = name.Substring (0, sep_index);
142 this.nspace = String.Empty;
144 if (interfaces != null) {
145 this.interfaces = new Type[interfaces.Length];
146 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
150 if (((attr & TypeAttributes.Interface) == 0) && (parent == null))
151 this.parent = typeof (object);
154 table_idx = mb.get_next_table_index (this, 0x02, true);
155 setup_internal_class (this);
156 fullname = GetFullName ();
159 public override Assembly Assembly {
160 get {return pmodule.Assembly;}
163 public override string AssemblyQualifiedName {
165 return fullname + ", " + Assembly.FullName;
169 public override Type BaseType {
175 public override Type DeclaringType {
176 get { return nesting_type; }
180 public override bool IsSubclassOf (Type c)
196 public override Type UnderlyingSystemType {
199 return created.UnderlyingSystemType;
202 if (underlying_type != null)
203 return underlying_type;
204 throw new InvalidOperationException (
205 "Enumeration type is not defined.");
212 string GetFullName ()
214 if (nesting_type != null)
215 return String.Concat (nesting_type.FullName, "+", tname);
216 if ((nspace != null) && (nspace.Length > 0))
217 return String.Concat (nspace, ".", tname);
221 public override string FullName {
227 public override Guid GUID {
234 public override Module Module {
235 get {return pmodule;}
238 public override string Name {
242 public override string Namespace {
246 public PackingSize PackingSize {
247 get {return packing_size;}
251 get { return class_size; }
254 public override Type ReflectedType {
255 get { return nesting_type; }
258 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
262 throw new ArgumentNullException ("pset");
263 if ((action == SecurityAction.RequestMinimum) ||
264 (action == SecurityAction.RequestOptional) ||
265 (action == SecurityAction.RequestRefuse))
266 throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
268 check_not_created ();
270 if (permissions != null) {
271 /* Check duplicate actions */
272 foreach (RefEmitPermissionSet set in permissions)
273 if (set.action == action)
274 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
276 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
277 permissions.CopyTo (new_array, 0);
278 permissions = new_array;
281 permissions = new RefEmitPermissionSet [1];
283 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
284 attrs |= TypeAttributes.HasSecurity;
289 public void AddInterfaceImplementation (Type interfaceType)
291 if (interfaceType == null)
292 throw new ArgumentNullException ("interfaceType");
293 check_not_created ();
295 if (interfaces != null) {
296 // Check for duplicates
297 foreach (Type t in interfaces)
298 if (t == interfaceType)
301 Type[] ifnew = new Type [interfaces.Length + 1];
302 interfaces.CopyTo (ifnew, 0);
303 ifnew [interfaces.Length] = interfaceType;
306 interfaces = new Type [1];
307 interfaces [0] = interfaceType;
311 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
312 CallingConventions callConvention, Type[] types,
313 ParameterModifier[] modifiers)
317 if (created == typeof (object)) {
319 * This happens when building corlib. Calling created.GetConstructor
320 * would return constructors from the real mscorlib, instead of the
327 ConstructorBuilder found = null;
330 foreach (ConstructorBuilder cb in ctors) {
331 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
341 throw new AmbiguousMatchException ();
344 MethodBase[] match = new MethodBase [count];
349 foreach (ConstructorInfo m in ctors) {
350 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
356 binder = DefaultBinder;
357 return (ConstructorInfo) binder.SelectMethod (bindingAttr, match,
361 return created.GetConstructor (bindingAttr, binder,
362 callConvention, types, modifiers);
365 public override bool IsDefined (Type attributeType, bool inherit)
368 throw new NotSupportedException ();
370 * MS throws NotSupported here, but we can't because some corlib
371 * classes make calls to IsDefined.
373 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
376 public override object[] GetCustomAttributes(bool inherit)
380 return created.GetCustomAttributes (inherit);
383 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
387 return created.GetCustomAttributes (attributeType, inherit);
390 public TypeBuilder DefineNestedType (string name)
392 return DefineNestedType (name, TypeAttributes.NestedPrivate,
393 pmodule.assemblyb.corlib_object_type, null);
396 public TypeBuilder DefineNestedType (string name, TypeAttributes attr)
398 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
401 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent)
403 return DefineNestedType (name, attr, parent, null);
406 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
407 PackingSize packSize, int typeSize)
409 // Visibility must be NestedXXX
411 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
412 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
413 throw new ArgumentException ("attr", "Bad type flags for nested type.");
415 if (interfaces != null)
416 foreach (Type iface in interfaces)
418 throw new ArgumentNullException ("interfaces");
420 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packSize, typeSize, this);
421 res.fullname = res.GetFullName ();
422 pmodule.RegisterTypeName (res, res.fullname);
423 if (subtypes != null) {
424 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
425 System.Array.Copy (subtypes, new_types, subtypes.Length);
426 new_types [subtypes.Length] = res;
427 subtypes = new_types;
429 subtypes = new TypeBuilder [1];
436 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces)
438 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
441 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typeSize)
443 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typeSize);
446 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize)
448 return DefineNestedType (name, attr, parent, null, packSize, UnspecifiedTypeSize);
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];
667 create_internal_class (this);
671 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
672 underlying_type = type;
678 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
680 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
683 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
685 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
688 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
690 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
693 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
695 check_name ("name", name);
696 if (parameterTypes != null)
697 foreach (Type param in parameterTypes)
699 throw new ArgumentNullException ("parameterTypes");
700 check_not_created ();
702 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
704 if (properties != null) {
705 Array.Resize (ref properties, properties.Length + 1);
706 properties [properties.Length - 1] = res;
708 properties = new PropertyBuilder [1] { res };
714 public ConstructorBuilder DefineTypeInitializer()
716 return DefineConstructor (MethodAttributes.Public |
717 MethodAttributes.Static | MethodAttributes.SpecialName |
718 MethodAttributes.RTSpecialName, CallingConventions.Standard,
722 [MethodImplAttribute(MethodImplOptions.InternalCall)]
723 private extern TypeInfo create_runtime_class (TypeBuilder tb);
725 private bool is_nested_in (Type t)
736 // Return whenever this type has a ctor defined using DefineMethod ()
737 private bool has_ctor_method () {
738 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
740 for (int i = 0; i < num_methods; ++i) {
741 MethodBuilder mb = (MethodBuilder)(methods[i]);
743 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
750 public Type CreateType ()
752 return CreateTypeInfo ();
756 TypeInfo CreateTypeInfo ()
758 /* handle nesting_type */
759 if (createTypeCalled)
762 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
763 SetParent (pmodule.assemblyb.corlib_object_type);
766 create_generic_class ();
768 // Fire TypeResolve events for fields whose type is an unfinished
770 if (fields != null) {
771 foreach (FieldBuilder fb in fields) {
774 Type ft = fb.FieldType;
775 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
776 TypeBuilder tb = (TypeBuilder)ft;
777 if (!tb.is_created) {
778 AppDomain.CurrentDomain.DoTypeResolve (tb);
779 if (!tb.is_created) {
780 // FIXME: We should throw an exception here,
781 // but mcs expects that the type is created
782 // even if the exception is thrown
783 //throw new TypeLoadException ("Could not load type " + tb);
791 // On classes, define a default constructor if not provided
793 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
794 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
795 DefineDefaultConstructor (MethodAttributes.Public);
797 createTypeCalled = true;
799 if ((parent != null) && parent.IsSealed)
800 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
802 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
803 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
804 if (interfaces != null) {
805 foreach (var iface in interfaces) {
806 if (iface.IsNestedPrivate && iface.Assembly != Assembly)
807 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is implements the inaccessible interface '" + iface.FullName + "'.");
811 if (methods != null) {
812 bool is_concrete = !IsAbstract;
813 for (int i = 0; i < num_methods; ++i) {
814 MethodBuilder mb = (MethodBuilder)(methods[i]);
815 if (is_concrete && mb.IsAbstract)
816 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
817 mb.check_override ();
823 foreach (ConstructorBuilder ctor in ctors)
827 created = create_runtime_class (this);
833 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
835 symbolWriter.OpenNamespace (this.Namespace);
837 if (methods != null) {
838 for (int i = 0; i < num_methods; ++i) {
839 MethodBuilder metb = (MethodBuilder) methods[i];
840 metb.GenerateDebugInfo (symbolWriter);
845 foreach (ConstructorBuilder ctor in ctors)
846 ctor.GenerateDebugInfo (symbolWriter);
849 symbolWriter.CloseNamespace ();
851 if (subtypes != null) {
852 for (int i = 0; i < subtypes.Length; ++i)
853 subtypes [i].GenerateDebugInfo (symbolWriter);
858 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
861 return created.GetConstructors (bindingAttr);
863 throw new NotSupportedException ();
866 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
869 return new ConstructorInfo [0];
870 ArrayList l = new ArrayList ();
872 MethodAttributes mattrs;
874 foreach (ConstructorBuilder c in ctors) {
876 mattrs = c.Attributes;
877 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
878 if ((bindingAttr & BindingFlags.Public) != 0)
881 if ((bindingAttr & BindingFlags.NonPublic) != 0)
887 if ((mattrs & MethodAttributes.Static) != 0) {
888 if ((bindingAttr & BindingFlags.Static) != 0)
891 if ((bindingAttr & BindingFlags.Instance) != 0)
898 ConstructorInfo[] result = new ConstructorInfo [l.Count];
903 public override Type GetElementType ()
905 throw new NotSupportedException ();
908 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
911 return created.GetEvent (name, bindingAttr);
914 /* Needed to keep signature compatibility with MS.NET */
915 public override EventInfo[] GetEvents ()
917 const BindingFlags DefaultBindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
918 return GetEvents (DefaultBindingFlags);
921 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
924 return created.GetEvents (bindingAttr);
925 throw new NotSupportedException ();
928 // This is only used from MonoGenericInst.initialize().
929 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
932 return new EventInfo [0];
933 ArrayList l = new ArrayList ();
935 MethodAttributes mattrs;
938 foreach (EventBuilder eb in events) {
941 EventInfo c = get_event_info (eb);
943 accessor = c.GetAddMethod (true);
944 if (accessor == null)
945 accessor = c.GetRemoveMethod (true);
946 if (accessor == null)
948 mattrs = accessor.Attributes;
949 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
950 if ((bindingAttr & BindingFlags.Public) != 0)
953 if ((bindingAttr & BindingFlags.NonPublic) != 0)
959 if ((mattrs & MethodAttributes.Static) != 0) {
960 if ((bindingAttr & BindingFlags.Static) != 0)
963 if ((bindingAttr & BindingFlags.Instance) != 0)
970 EventInfo[] result = new EventInfo [l.Count];
975 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
978 return created.GetField (name, bindingAttr);
984 FieldAttributes mattrs;
986 foreach (FieldInfo c in fields) {
992 mattrs = c.Attributes;
993 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
994 if ((bindingAttr & BindingFlags.Public) != 0)
997 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1003 if ((mattrs & FieldAttributes.Static) != 0) {
1004 if ((bindingAttr & BindingFlags.Static) != 0)
1007 if ((bindingAttr & BindingFlags.Instance) != 0)
1017 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1019 if (created != null)
1020 return created.GetFields (bindingAttr);
1023 return new FieldInfo [0];
1024 ArrayList l = new ArrayList ();
1026 FieldAttributes mattrs;
1028 foreach (FieldInfo c in fields) {
1032 mattrs = c.Attributes;
1033 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1034 if ((bindingAttr & BindingFlags.Public) != 0)
1037 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1043 if ((mattrs & FieldAttributes.Static) != 0) {
1044 if ((bindingAttr & BindingFlags.Static) != 0)
1047 if ((bindingAttr & BindingFlags.Instance) != 0)
1054 FieldInfo[] result = new FieldInfo [l.Count];
1059 public override Type GetInterface (string name, bool ignoreCase)
1062 return created.GetInterface (name, ignoreCase);
1065 public override Type[] GetInterfaces ()
1068 return created.GetInterfaces ();
1070 if (interfaces != null) {
1071 Type[] ret = new Type [interfaces.Length];
1072 interfaces.CopyTo (ret, 0);
1075 return Type.EmptyTypes;
1079 public override MemberInfo[] GetMember (string name, MemberTypes type,
1080 BindingFlags bindingAttr)
1083 return created.GetMember (name, type, bindingAttr);
1086 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1089 return created.GetMembers (bindingAttr);
1092 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1094 MethodInfo[] candidates;
1096 MethodAttributes mattrs;
1098 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1099 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1100 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1102 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1104 for (int i = 0; i < parent_methods.Length; i++) {
1105 MethodInfo m = parent_methods [i];
1107 mattrs = m.Attributes;
1109 if (m.IsStatic && !flatten)
1112 switch (mattrs & MethodAttributes.MemberAccessMask) {
1113 case MethodAttributes.Public:
1114 match = (bindingAttr & BindingFlags.Public) != 0;
1116 case MethodAttributes.Assembly:
1117 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1119 case MethodAttributes.Private:
1123 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1128 parent_candidates.Add (m);
1131 if (methods == null) {
1132 candidates = new MethodInfo [parent_candidates.Count];
1133 parent_candidates.CopyTo (candidates);
1135 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1136 parent_candidates.CopyTo (candidates, 0);
1137 methods.CopyTo (candidates, parent_candidates.Count);
1141 candidates = methods;
1143 if (candidates == null)
1144 return new MethodInfo [0];
1146 ArrayList l = new ArrayList ();
1148 foreach (MethodInfo c in candidates) {
1152 if (String.Compare (c.Name, name, ignoreCase) != 0)
1156 mattrs = c.Attributes;
1157 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1158 if ((bindingAttr & BindingFlags.Public) != 0)
1161 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1167 if ((mattrs & MethodAttributes.Static) != 0) {
1168 if ((bindingAttr & BindingFlags.Static) != 0)
1171 if ((bindingAttr & BindingFlags.Instance) != 0)
1179 MethodInfo[] result = new MethodInfo [l.Count];
1184 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1186 return GetMethodsByName (null, bindingAttr, false, this);
1189 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1191 CallingConventions callConvention,
1192 Type[] types, ParameterModifier[] modifiers)
1197 return created.GetMethod (name, bindingAttr);
1199 return created.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
1202 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1206 if (subtypes == null)
1209 foreach (TypeBuilder t in subtypes) {
1212 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1213 if ((bindingAttr & BindingFlags.Public) == 0)
1216 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1226 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1229 throw new NotSupportedException ();
1232 ArrayList result = new ArrayList ();
1234 if (subtypes == null)
1235 return Type.EmptyTypes;
1236 foreach (TypeBuilder t in subtypes) {
1238 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1239 if ((bindingAttr & BindingFlags.Public) != 0)
1242 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1249 Type[] r = new Type [result.Count];
1254 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1257 return created.GetProperties (bindingAttr);
1259 if (properties == null)
1260 return new PropertyInfo [0];
1261 ArrayList l = new ArrayList ();
1263 MethodAttributes mattrs;
1264 MethodInfo accessor;
1266 foreach (PropertyInfo c in properties) {
1268 accessor = c.GetGetMethod (true);
1269 if (accessor == null)
1270 accessor = c.GetSetMethod (true);
1271 if (accessor == null)
1273 mattrs = accessor.Attributes;
1274 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1275 if ((bindingAttr & BindingFlags.Public) != 0)
1278 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1284 if ((mattrs & MethodAttributes.Static) != 0) {
1285 if ((bindingAttr & BindingFlags.Static) != 0)
1288 if ((bindingAttr & BindingFlags.Instance) != 0)
1295 PropertyInfo[] result = new PropertyInfo [l.Count];
1300 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1302 throw not_supported ();
1305 protected override bool HasElementTypeImpl ()
1307 // a TypeBuilder can never represent an array, pointer
1311 return created.HasElementType;
1314 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1317 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1320 protected override bool IsArrayImpl ()
1322 return false; /*A TypeBuilder never represents a non typedef type.*/
1325 protected override bool IsByRefImpl ()
1327 return false; /*A TypeBuilder never represents a non typedef type.*/
1330 protected override bool IsCOMObjectImpl ()
1332 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1335 protected override bool IsPointerImpl ()
1337 return false; /*A TypeBuilder never represents a non typedef type.*/
1340 protected override bool IsPrimitiveImpl ()
1346 // FIXME: I doubt just removing this still works.
1347 protected override bool IsValueTypeImpl ()
1349 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1351 Type parent_type = parent;
1352 while (parent_type != null) {
1353 if (parent_type == pmodule.assemblyb.corlib_value_type)
1355 parent_type = parent_type.BaseType;
1360 public override Type MakeArrayType ()
1362 return new ArrayType (this, 0);
1365 public override Type MakeArrayType (int rank)
1368 throw new IndexOutOfRangeException ();
1369 return new ArrayType (this, rank);
1372 public override Type MakeByRefType ()
1374 return new ByRefType (this);
1377 public override Type MakeGenericType (params Type [] typeArguments)
1379 //return base.MakeGenericType (typeArguments);
1381 if (!IsGenericTypeDefinition)
1382 throw new InvalidOperationException ("not a generic type definition");
1383 if (typeArguments == null)
1384 throw new ArgumentNullException ("typeArguments");
1386 if (generic_params.Length != typeArguments.Length)
1387 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");
1389 foreach (Type t in typeArguments) {
1391 throw new ArgumentNullException ("typeArguments");
1394 Type[] copy = new Type [typeArguments.Length];
1395 typeArguments.CopyTo (copy, 0);
1396 return pmodule.assemblyb.MakeGenericType (this, copy);
1399 public override Type MakePointerType ()
1401 return new PointerType (this);
1404 public override RuntimeTypeHandle TypeHandle {
1407 return created.TypeHandle;
1411 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1413 if (customBuilder == null)
1414 throw new ArgumentNullException ("customBuilder");
1416 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1417 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1418 byte[] data = customBuilder.Data;
1419 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1420 layout_kind = (int)data [2];
1421 layout_kind |= ((int)data [3]) << 8;
1422 attrs &= ~TypeAttributes.LayoutMask;
1423 switch ((LayoutKind)layout_kind) {
1424 case LayoutKind.Auto:
1425 attrs |= TypeAttributes.AutoLayout;
1427 case LayoutKind.Explicit:
1428 attrs |= TypeAttributes.ExplicitLayout;
1430 case LayoutKind.Sequential:
1431 attrs |= TypeAttributes.SequentialLayout;
1434 // we should ignore it since it can be any value anyway...
1435 throw new Exception ("Error in customattr");
1438 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1440 if (ctor_type.FullName == "System.Int16")
1442 int nnamed = (int)data [pos++];
1443 nnamed |= ((int)data [pos++]) << 8;
1444 for (int i = 0; i < nnamed; ++i) {
1445 //byte named_type = data [pos++];
1447 byte type = data [pos++];
1452 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1453 //string named_typename =
1454 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1456 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1457 // See related code/FIXME in mono/mono/metadata/reflection.c
1460 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1461 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1463 /* all the fields are integers in StructLayout */
1464 int value = (int)data [pos++];
1465 value |= ((int)data [pos++]) << 8;
1466 value |= ((int)data [pos++]) << 16;
1467 value |= ((int)data [pos++]) << 24;
1468 switch (named_name) {
1470 switch ((CharSet)value) {
1473 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1475 case CharSet.Unicode:
1476 attrs &= ~TypeAttributes.AutoClass;
1477 attrs |= TypeAttributes.UnicodeClass;
1480 attrs &= ~TypeAttributes.UnicodeClass;
1481 attrs |= TypeAttributes.AutoClass;
1484 break; // error out...
1488 packing_size = (PackingSize)value;
1494 break; // error out...
1498 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1499 attrs |= TypeAttributes.SpecialName;
1501 } else if (attrname == "System.SerializableAttribute") {
1502 attrs |= TypeAttributes.Serializable;
1504 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1505 attrs |= TypeAttributes.Import;
1507 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1508 attrs |= TypeAttributes.HasSecurity;
1511 if (cattrs != null) {
1512 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1513 cattrs.CopyTo (new_array, 0);
1514 new_array [cattrs.Length] = customBuilder;
1517 cattrs = new CustomAttributeBuilder [1];
1518 cattrs [0] = customBuilder;
1523 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1525 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1528 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1530 check_name ("name", name);
1531 if (eventtype == null)
1532 throw new ArgumentNullException ("type");
1533 check_not_created ();
1535 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1536 if (events != null) {
1537 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1538 System.Array.Copy (events, new_events, events.Length);
1539 new_events [events.Length] = res;
1540 events = new_events;
1542 events = new EventBuilder [1];
1548 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1550 throw new ArgumentNullException ("data");
1552 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1553 res.SetRVAData (data);
1557 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1560 throw new ArgumentNullException ("name");
1561 if (name.Length == 0)
1562 throw new ArgumentException ("Empty name is not legal", "name");
1563 if ((size <= 0) || (size > 0x3f0000))
1564 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1565 check_not_created ();
1567 string typeName = "$ArrayType$" + size;
1568 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
1569 if (datablobtype == null) {
1570 TypeBuilder tb = DefineNestedType (typeName,
1571 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1572 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1576 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1579 public TypeToken TypeToken {
1581 return new TypeToken (0x02000000 | table_idx);
1585 public void SetParent (Type parent)
1587 check_not_created ();
1589 if (parent == null) {
1590 if ((attrs & TypeAttributes.Interface) != 0) {
1591 if ((attrs & TypeAttributes.Abstract) == 0)
1592 throw new InvalidOperationException ("Interface must be declared abstract.");
1595 this.parent = typeof (object);
1598 this.parent = parent;
1601 // will just set the parent-related bits if called a second time
1602 setup_internal_class (this);
1605 internal int get_next_table_index (object obj, int table, bool inc) {
1606 return pmodule.get_next_table_index (obj, table, inc);
1610 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1612 if (created == null)
1613 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1615 return created.GetInterfaceMap (interfaceType);
1618 internal override Type InternalResolve ()
1624 internal bool is_created {
1626 return createTypeCalled;
1630 private Exception not_supported ()
1632 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1635 private void check_not_created ()
1638 throw new InvalidOperationException ("Unable to change after type has been created.");
1641 private void check_created ()
1644 throw not_supported ();
1647 private void check_name (string argName, string name)
1650 throw new ArgumentNullException (argName);
1651 if (name.Length == 0)
1652 throw new ArgumentException ("Empty name is not legal", argName);
1653 if (name [0] == ((char)0))
1654 throw new ArgumentException ("Illegal name", argName);
1657 public override String ToString ()
1663 public override bool IsAssignableFrom (Type c)
1665 return base.IsAssignableFrom (c);
1668 [MonoTODO ("arrays")]
1669 internal bool IsAssignableTo (Type c)
1674 if (c.IsInterface) {
1675 if (parent != null && is_created) {
1676 if (c.IsAssignableFrom (parent))
1680 if (interfaces == null)
1682 foreach (Type t in interfaces)
1683 if (c.IsAssignableFrom (t))
1690 return c == typeof (object);
1692 return c.IsAssignableFrom (parent);
1695 public bool IsCreated ()
1700 public override Type[] GetGenericArguments ()
1702 if (generic_params == null)
1704 Type[] args = new Type [generic_params.Length];
1705 generic_params.CopyTo (args, 0);
1709 public override Type GetGenericTypeDefinition ()
1711 if (generic_params == null)
1712 throw new InvalidOperationException ("Type is not generic");
1716 public override bool ContainsGenericParameters {
1718 return generic_params != null;
1722 public extern override bool IsGenericParameter {
1723 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1727 public override GenericParameterAttributes GenericParameterAttributes {
1728 get { return GenericParameterAttributes.None; }
1731 public override bool IsGenericTypeDefinition {
1733 return generic_params != null;
1737 public override bool IsGenericType {
1738 get { return IsGenericTypeDefinition; }
1742 public override int GenericParameterPosition {
1748 public override MethodBase DeclaringMethod {
1754 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1757 throw new ArgumentNullException ("names");
1758 if (names.Length == 0)
1759 throw new ArgumentException ("names");
1761 setup_generic_class ();
1763 generic_params = new GenericTypeParameterBuilder [names.Length];
1764 for (int i = 0; i < names.Length; i++) {
1765 string item = names [i];
1767 throw new ArgumentNullException ("names");
1768 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1771 return generic_params;
1774 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1776 /*FIXME I would expect the same checks of GetMethod here*/
1778 throw new ArgumentException ("Type is not generic", "type");
1780 if (!type.IsGenericType)
1781 throw new ArgumentException ("Type is not a generic type", "type");
1783 if (type.IsGenericTypeDefinition)
1784 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1786 if (constructor == null)
1787 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1789 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1790 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1791 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1792 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1794 ConstructorInfo res = type.GetConstructor (constructor);
1796 throw new ArgumentException ("constructor not found");
1801 static bool IsValidGetMethodType (Type type)
1803 if (type is TypeBuilder || type is MonoGenericClass)
1805 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1806 if (type.Module is ModuleBuilder)
1808 if (type.IsGenericParameter)
1811 Type[] inst = type.GetGenericArguments ();
1814 for (int i = 0; i < inst.Length; ++i) {
1815 if (IsValidGetMethodType (inst [i]))
1821 public static MethodInfo GetMethod (Type type, MethodInfo method)
1823 if (!IsValidGetMethodType (type))
1824 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1826 if (type is TypeBuilder && type.ContainsGenericParameters)
1827 type = type.MakeGenericType (type.GetGenericArguments ());
1829 if (!type.IsGenericType)
1830 throw new ArgumentException ("type is not a generic type", "type");
1832 if (!method.DeclaringType.IsGenericTypeDefinition)
1833 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1834 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1835 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1837 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1839 MethodInfo res = type.GetMethod (method);
1841 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1846 public static FieldInfo GetField (Type type, FieldInfo field)
1848 if (!type.IsGenericType)
1849 throw new ArgumentException ("Type is not a generic type", "type");
1851 if (type.IsGenericTypeDefinition)
1852 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1854 if (field is FieldOnTypeBuilderInst)
1855 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1857 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1858 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1860 FieldInfo res = type.GetField (field);
1862 throw new System.Exception ("field not found");
1868 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1870 throw new NotImplementedException ();
1873 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1875 throw new NotImplementedException ();
1878 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1880 throw new NotImplementedException ();
1883 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1885 throw new NotImplementedException ();
1888 internal override bool IsUserType {
1894 public override bool IsConstructedGenericType {
1895 get { return false; }
1898 public override bool IsAssignableFrom (TypeInfo typeInfo)
1900 return base.IsAssignableFrom (typeInfo);