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);
451 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packSize,
454 return DefineNestedType (name, attr, parent, null, packSize, typeSize);
458 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes)
460 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
464 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
466 check_not_created ();
467 ConstructorBuilder cb = new ConstructorBuilder (this, attributes,
468 callingConvention, parameterTypes, requiredCustomModifiers,
469 optionalCustomModifiers);
471 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
472 System.Array.Copy (ctors, new_ctors, ctors.Length);
473 new_ctors [ctors.Length] = cb;
476 ctors = new ConstructorBuilder [1];
483 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
485 Type parent_type, old_parent_type;
488 parent_type = parent;
490 parent_type = pmodule.assemblyb.corlib_object_type;
492 old_parent_type = parent_type;
493 parent_type = parent_type.InternalResolve ();
494 /*This avoids corlib to have self references.*/
495 if (parent_type == typeof (object) || parent_type == typeof (ValueType))
496 parent_type = old_parent_type;
498 ConstructorInfo parent_constructor =
499 parent_type.GetConstructor (
500 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
501 null, Type.EmptyTypes, null);
502 if (parent_constructor == null) {
503 throw new NotSupportedException ("Parent does"
504 + " not have a default constructor."
505 + " The default constructor must be"
506 + " explicitly defined.");
509 ConstructorBuilder cb = DefineConstructor (attributes,
510 CallingConventions.Standard, Type.EmptyTypes);
511 ILGenerator ig = cb.GetILGenerator ();
512 ig.Emit (OpCodes.Ldarg_0);
513 ig.Emit (OpCodes.Call, parent_constructor);
514 ig.Emit (OpCodes.Ret);
518 private void append_method (MethodBuilder mb)
520 if (methods != null) {
521 if (methods.Length == num_methods) {
522 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
523 System.Array.Copy (methods, new_methods, num_methods);
524 methods = new_methods;
527 methods = new MethodBuilder [1];
529 methods [num_methods] = mb;
533 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes)
535 return DefineMethod (name, attributes, CallingConventions.Standard,
536 returnType, parameterTypes);
539 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
541 return DefineMethod (name, attributes, callingConvention, returnType,
542 null, null, parameterTypes, null, null);
545 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
547 check_name ("name", name);
548 check_not_created ();
550 !((attributes & MethodAttributes.Abstract) != 0) ||
551 !((attributes & MethodAttributes.Virtual) != 0)) &&
552 !(((attributes & MethodAttributes.Static) != 0)))
553 throw new ArgumentException ("Interface method must be abstract and virtual.");
555 if (returnType == null)
556 returnType = pmodule.assemblyb.corlib_void_type;
557 MethodBuilder res = new MethodBuilder (this, name, attributes,
558 callingConvention, returnType,
559 returnTypeRequiredCustomModifiers,
560 returnTypeOptionalCustomModifiers, parameterTypes,
561 parameterTypeRequiredCustomModifiers,
562 parameterTypeOptionalCustomModifiers);
567 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet)
569 return DefinePInvokeMethod (name, dllName, entryName, attributes,
570 callingConvention, returnType, null, null, parameterTypes,
571 null, null, nativeCallConv, nativeCharSet);
574 public MethodBuilder DefinePInvokeMethod (
577 string entryName, MethodAttributes attributes,
578 CallingConventions callingConvention,
580 Type[] returnTypeRequiredCustomModifiers,
581 Type[] returnTypeOptionalCustomModifiers,
582 Type[] parameterTypes,
583 Type[][] parameterTypeRequiredCustomModifiers,
584 Type[][] parameterTypeOptionalCustomModifiers,
585 CallingConvention nativeCallConv,
586 CharSet nativeCharSet)
588 check_name ("name", name);
589 check_name ("dllName", dllName);
590 check_name ("entryName", entryName);
591 if ((attributes & MethodAttributes.Abstract) != 0)
592 throw new ArgumentException ("PInvoke methods must be static and native and cannot be abstract.");
594 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
595 check_not_created ();
598 = new MethodBuilder (
604 returnTypeRequiredCustomModifiers,
605 returnTypeOptionalCustomModifiers,
607 parameterTypeRequiredCustomModifiers,
608 parameterTypeOptionalCustomModifiers,
617 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
618 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
619 nativeCallConv, nativeCharSet);
622 public MethodBuilder DefineMethod (string name, MethodAttributes attributes)
624 return DefineMethod (name, attributes, CallingConventions.Standard);
627 public MethodBuilder DefineMethod (string name, MethodAttributes attributes, CallingConventions callingConvention)
629 return DefineMethod (name, attributes, callingConvention, null, null);
632 public void DefineMethodOverride (MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
634 if (methodInfoBody == null)
635 throw new ArgumentNullException ("methodInfoBody");
636 if (methodInfoDeclaration == null)
637 throw new ArgumentNullException ("methodInfoDeclaration");
638 check_not_created ();
639 if (methodInfoBody.DeclaringType != this)
640 throw new ArgumentException ("method body must belong to this type");
642 if (methodInfoBody is MethodBuilder) {
643 MethodBuilder mb = (MethodBuilder)methodInfoBody;
644 mb.set_override (methodInfoDeclaration);
648 public FieldBuilder DefineField (string fieldName, Type type, FieldAttributes attributes)
650 return DefineField (fieldName, type, null, null, attributes);
653 public FieldBuilder DefineField (string fieldName, Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers, FieldAttributes attributes)
655 check_name ("fieldName", fieldName);
656 if (type == typeof (void))
657 throw new ArgumentException ("Bad field type in defining field.");
658 check_not_created ();
660 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomModifiers, optionalCustomModifiers);
661 if (fields != null) {
662 if (fields.Length == num_fields) {
663 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
664 System.Array.Copy (fields, new_fields, num_fields);
667 fields [num_fields] = res;
670 fields = new FieldBuilder [1];
673 create_internal_class (this);
677 if (underlying_type == null && (attributes & FieldAttributes.Static) == 0)
678 underlying_type = type;
684 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes)
686 return DefineProperty (name, attributes, 0, returnType, null, null, parameterTypes, null, null);
689 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
691 return DefineProperty (name, attributes, callingConvention, returnType , null, null, parameterTypes, null, null);
694 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
696 return DefineProperty (name, attributes, 0, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
699 public PropertyBuilder DefineProperty (string name, PropertyAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
701 check_name ("name", name);
702 if (parameterTypes != null)
703 foreach (Type param in parameterTypes)
705 throw new ArgumentNullException ("parameterTypes");
706 check_not_created ();
708 PropertyBuilder res = new PropertyBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
710 if (properties != null) {
711 Array.Resize (ref properties, properties.Length + 1);
712 properties [properties.Length - 1] = res;
714 properties = new PropertyBuilder [1] { res };
720 public ConstructorBuilder DefineTypeInitializer()
722 return DefineConstructor (MethodAttributes.Public |
723 MethodAttributes.Static | MethodAttributes.SpecialName |
724 MethodAttributes.RTSpecialName, CallingConventions.Standard,
728 [MethodImplAttribute(MethodImplOptions.InternalCall)]
729 private extern TypeInfo create_runtime_class (TypeBuilder tb);
731 private bool is_nested_in (Type t)
742 // Return whenever this type has a ctor defined using DefineMethod ()
743 private bool has_ctor_method () {
744 MethodAttributes ctor_attrs = MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
746 for (int i = 0; i < num_methods; ++i) {
747 MethodBuilder mb = (MethodBuilder)(methods[i]);
749 if (mb.Name == ConstructorInfo.ConstructorName && (mb.Attributes & ctor_attrs) == ctor_attrs)
756 public Type CreateType ()
758 return CreateTypeInfo ();
762 TypeInfo CreateTypeInfo ()
764 /* handle nesting_type */
765 if (createTypeCalled)
768 if (!IsInterface && (parent == null) && (this != pmodule.assemblyb.corlib_object_type) && (FullName != "<Module>")) {
769 SetParent (pmodule.assemblyb.corlib_object_type);
772 create_generic_class ();
774 // Fire TypeResolve events for fields whose type is an unfinished
776 if (fields != null) {
777 foreach (FieldBuilder fb in fields) {
780 Type ft = fb.FieldType;
781 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
782 TypeBuilder tb = (TypeBuilder)ft;
783 if (!tb.is_created) {
784 AppDomain.CurrentDomain.DoTypeResolve (tb);
785 if (!tb.is_created) {
786 // FIXME: We should throw an exception here,
787 // but mcs expects that the type is created
788 // even if the exception is thrown
789 //throw new TypeLoadException ("Could not load type " + tb);
797 // On classes, define a default constructor if not provided
799 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
800 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed) && !has_ctor_method ())
801 DefineDefaultConstructor (MethodAttributes.Public);
803 createTypeCalled = true;
805 if ((parent != null) && parent.IsSealed)
806 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because the parent type is sealed.");
808 if (parent == pmodule.assemblyb.corlib_enum_type && methods != null)
809 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is an enum with methods.");
810 if (interfaces != null) {
811 foreach (var iface in interfaces) {
812 if (iface.IsNestedPrivate && iface.Assembly != Assembly)
813 throw new TypeLoadException ("Could not load type '" + FullName + "' from assembly '" + Assembly + "' because it is implements the inaccessible interface '" + iface.FullName + "'.");
817 if (methods != null) {
818 bool is_concrete = !IsAbstract;
819 for (int i = 0; i < num_methods; ++i) {
820 MethodBuilder mb = (MethodBuilder)(methods[i]);
821 if (is_concrete && mb.IsAbstract)
822 throw new InvalidOperationException ("Type is concrete but has abstract method " + mb);
823 mb.check_override ();
829 foreach (ConstructorBuilder ctor in ctors)
833 created = create_runtime_class (this);
839 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
841 symbolWriter.OpenNamespace (this.Namespace);
843 if (methods != null) {
844 for (int i = 0; i < num_methods; ++i) {
845 MethodBuilder metb = (MethodBuilder) methods[i];
846 metb.GenerateDebugInfo (symbolWriter);
851 foreach (ConstructorBuilder ctor in ctors)
852 ctor.GenerateDebugInfo (symbolWriter);
855 symbolWriter.CloseNamespace ();
857 if (subtypes != null) {
858 for (int i = 0; i < subtypes.Length; ++i)
859 subtypes [i].GenerateDebugInfo (symbolWriter);
864 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
867 return created.GetConstructors (bindingAttr);
869 throw new NotSupportedException ();
872 internal ConstructorInfo[] GetConstructorsInternal (BindingFlags bindingAttr)
875 return new ConstructorInfo [0];
876 ArrayList l = new ArrayList ();
878 MethodAttributes mattrs;
880 foreach (ConstructorBuilder c in ctors) {
882 mattrs = c.Attributes;
883 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
884 if ((bindingAttr & BindingFlags.Public) != 0)
887 if ((bindingAttr & BindingFlags.NonPublic) != 0)
893 if ((mattrs & MethodAttributes.Static) != 0) {
894 if ((bindingAttr & BindingFlags.Static) != 0)
897 if ((bindingAttr & BindingFlags.Instance) != 0)
904 ConstructorInfo[] result = new ConstructorInfo [l.Count];
909 public override Type GetElementType ()
911 throw new NotSupportedException ();
914 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
917 return created.GetEvent (name, bindingAttr);
920 /* Needed to keep signature compatibility with MS.NET */
921 public override EventInfo[] GetEvents ()
923 const BindingFlags DefaultBindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
924 return GetEvents (DefaultBindingFlags);
927 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
930 return created.GetEvents (bindingAttr);
931 throw new NotSupportedException ();
934 // This is only used from MonoGenericInst.initialize().
935 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
938 return new EventInfo [0];
939 ArrayList l = new ArrayList ();
941 MethodAttributes mattrs;
944 foreach (EventBuilder eb in events) {
947 EventInfo c = get_event_info (eb);
949 accessor = c.GetAddMethod (true);
950 if (accessor == null)
951 accessor = c.GetRemoveMethod (true);
952 if (accessor == null)
954 mattrs = accessor.Attributes;
955 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
956 if ((bindingAttr & BindingFlags.Public) != 0)
959 if ((bindingAttr & BindingFlags.NonPublic) != 0)
965 if ((mattrs & MethodAttributes.Static) != 0) {
966 if ((bindingAttr & BindingFlags.Static) != 0)
969 if ((bindingAttr & BindingFlags.Instance) != 0)
976 EventInfo[] result = new EventInfo [l.Count];
981 public override FieldInfo GetField (string name, BindingFlags bindingAttr)
984 return created.GetField (name, bindingAttr);
990 FieldAttributes mattrs;
992 foreach (FieldInfo c in fields) {
998 mattrs = c.Attributes;
999 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1000 if ((bindingAttr & BindingFlags.Public) != 0)
1003 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1009 if ((mattrs & FieldAttributes.Static) != 0) {
1010 if ((bindingAttr & BindingFlags.Static) != 0)
1013 if ((bindingAttr & BindingFlags.Instance) != 0)
1023 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
1025 if (created != null)
1026 return created.GetFields (bindingAttr);
1029 return new FieldInfo [0];
1030 ArrayList l = new ArrayList ();
1032 FieldAttributes mattrs;
1034 foreach (FieldInfo c in fields) {
1038 mattrs = c.Attributes;
1039 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
1040 if ((bindingAttr & BindingFlags.Public) != 0)
1043 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1049 if ((mattrs & FieldAttributes.Static) != 0) {
1050 if ((bindingAttr & BindingFlags.Static) != 0)
1053 if ((bindingAttr & BindingFlags.Instance) != 0)
1060 FieldInfo[] result = new FieldInfo [l.Count];
1065 public override Type GetInterface (string name, bool ignoreCase)
1068 return created.GetInterface (name, ignoreCase);
1071 public override Type[] GetInterfaces ()
1074 return created.GetInterfaces ();
1076 if (interfaces != null) {
1077 Type[] ret = new Type [interfaces.Length];
1078 interfaces.CopyTo (ret, 0);
1081 return Type.EmptyTypes;
1085 public override MemberInfo[] GetMember (string name, MemberTypes type,
1086 BindingFlags bindingAttr)
1089 return created.GetMember (name, type, bindingAttr);
1092 public override MemberInfo[] GetMembers (BindingFlags bindingAttr)
1095 return created.GetMembers (bindingAttr);
1098 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type)
1100 MethodInfo[] candidates;
1102 MethodAttributes mattrs;
1104 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
1105 MethodInfo [] parent_methods = parent.GetMethods (bindingAttr);
1106 ArrayList parent_candidates = new ArrayList (parent_methods.Length);
1108 bool flatten = (bindingAttr & BindingFlags.FlattenHierarchy) != 0;
1110 for (int i = 0; i < parent_methods.Length; i++) {
1111 MethodInfo m = parent_methods [i];
1113 mattrs = m.Attributes;
1115 if (m.IsStatic && !flatten)
1118 switch (mattrs & MethodAttributes.MemberAccessMask) {
1119 case MethodAttributes.Public:
1120 match = (bindingAttr & BindingFlags.Public) != 0;
1122 case MethodAttributes.Assembly:
1123 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1125 case MethodAttributes.Private:
1129 match = (bindingAttr & BindingFlags.NonPublic) != 0;
1134 parent_candidates.Add (m);
1137 if (methods == null) {
1138 candidates = new MethodInfo [parent_candidates.Count];
1139 parent_candidates.CopyTo (candidates);
1141 candidates = new MethodInfo [methods.Length + parent_candidates.Count];
1142 parent_candidates.CopyTo (candidates, 0);
1143 methods.CopyTo (candidates, parent_candidates.Count);
1147 candidates = methods;
1149 if (candidates == null)
1150 return new MethodInfo [0];
1152 ArrayList l = new ArrayList ();
1154 foreach (MethodInfo c in candidates) {
1158 if (String.Compare (c.Name, name, ignoreCase) != 0)
1162 mattrs = c.Attributes;
1163 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1164 if ((bindingAttr & BindingFlags.Public) != 0)
1167 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1173 if ((mattrs & MethodAttributes.Static) != 0) {
1174 if ((bindingAttr & BindingFlags.Static) != 0)
1177 if ((bindingAttr & BindingFlags.Instance) != 0)
1185 MethodInfo[] result = new MethodInfo [l.Count];
1190 public override MethodInfo[] GetMethods (BindingFlags bindingAttr)
1192 return GetMethodsByName (null, bindingAttr, false, this);
1195 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
1197 CallingConventions callConvention,
1198 Type[] types, ParameterModifier[] modifiers)
1203 return created.GetMethod (name, bindingAttr);
1205 return created.GetMethod (name, bindingAttr, binder, callConvention, types, modifiers);
1208 public override Type GetNestedType (string name, BindingFlags bindingAttr)
1212 if (subtypes == null)
1215 foreach (TypeBuilder t in subtypes) {
1218 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1219 if ((bindingAttr & BindingFlags.Public) == 0)
1222 if ((bindingAttr & BindingFlags.NonPublic) == 0)
1232 public override Type[] GetNestedTypes (BindingFlags bindingAttr)
1235 throw new NotSupportedException ();
1238 ArrayList result = new ArrayList ();
1240 if (subtypes == null)
1241 return Type.EmptyTypes;
1242 foreach (TypeBuilder t in subtypes) {
1244 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
1245 if ((bindingAttr & BindingFlags.Public) != 0)
1248 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1255 Type[] r = new Type [result.Count];
1260 public override PropertyInfo[] GetProperties (BindingFlags bindingAttr)
1263 return created.GetProperties (bindingAttr);
1265 if (properties == null)
1266 return new PropertyInfo [0];
1267 ArrayList l = new ArrayList ();
1269 MethodAttributes mattrs;
1270 MethodInfo accessor;
1272 foreach (PropertyInfo c in properties) {
1274 accessor = c.GetGetMethod (true);
1275 if (accessor == null)
1276 accessor = c.GetSetMethod (true);
1277 if (accessor == null)
1279 mattrs = accessor.Attributes;
1280 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1281 if ((bindingAttr & BindingFlags.Public) != 0)
1284 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1290 if ((mattrs & MethodAttributes.Static) != 0) {
1291 if ((bindingAttr & BindingFlags.Static) != 0)
1294 if ((bindingAttr & BindingFlags.Instance) != 0)
1301 PropertyInfo[] result = new PropertyInfo [l.Count];
1306 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1308 throw not_supported ();
1311 protected override bool HasElementTypeImpl ()
1313 // a TypeBuilder can never represent an array, pointer
1317 return created.HasElementType;
1320 public override object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
1323 return created.InvokeMember (name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
1326 protected override bool IsArrayImpl ()
1328 return false; /*A TypeBuilder never represents a non typedef type.*/
1331 protected override bool IsByRefImpl ()
1333 return false; /*A TypeBuilder never represents a non typedef type.*/
1336 protected override bool IsCOMObjectImpl ()
1338 return ((GetAttributeFlagsImpl () & TypeAttributes.Import) != 0);
1341 protected override bool IsPointerImpl ()
1343 return false; /*A TypeBuilder never represents a non typedef type.*/
1346 protected override bool IsPrimitiveImpl ()
1352 // FIXME: I doubt just removing this still works.
1353 protected override bool IsValueTypeImpl ()
1355 if (this == pmodule.assemblyb.corlib_value_type || this == pmodule.assemblyb.corlib_enum_type)
1357 Type parent_type = parent;
1358 while (parent_type != null) {
1359 if (parent_type == pmodule.assemblyb.corlib_value_type)
1361 parent_type = parent_type.BaseType;
1366 public override Type MakeArrayType ()
1368 return new ArrayType (this, 0);
1371 public override Type MakeArrayType (int rank)
1374 throw new IndexOutOfRangeException ();
1375 return new ArrayType (this, rank);
1378 public override Type MakeByRefType ()
1380 return new ByRefType (this);
1383 public override Type MakeGenericType (params Type [] typeArguments)
1385 //return base.MakeGenericType (typeArguments);
1387 if (!IsGenericTypeDefinition)
1388 throw new InvalidOperationException ("not a generic type definition");
1389 if (typeArguments == null)
1390 throw new ArgumentNullException ("typeArguments");
1392 if (generic_params.Length != typeArguments.Length)
1393 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");
1395 foreach (Type t in typeArguments) {
1397 throw new ArgumentNullException ("typeArguments");
1400 Type[] copy = new Type [typeArguments.Length];
1401 typeArguments.CopyTo (copy, 0);
1402 return pmodule.assemblyb.MakeGenericType (this, copy);
1405 public override Type MakePointerType ()
1407 return new PointerType (this);
1410 public override RuntimeTypeHandle TypeHandle {
1413 return created.TypeHandle;
1417 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
1419 if (customBuilder == null)
1420 throw new ArgumentNullException ("customBuilder");
1422 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1423 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1424 byte[] data = customBuilder.Data;
1425 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1426 layout_kind = (int)data [2];
1427 layout_kind |= ((int)data [3]) << 8;
1428 attrs &= ~TypeAttributes.LayoutMask;
1429 switch ((LayoutKind)layout_kind) {
1430 case LayoutKind.Auto:
1431 attrs |= TypeAttributes.AutoLayout;
1433 case LayoutKind.Explicit:
1434 attrs |= TypeAttributes.ExplicitLayout;
1436 case LayoutKind.Sequential:
1437 attrs |= TypeAttributes.SequentialLayout;
1440 // we should ignore it since it can be any value anyway...
1441 throw new Exception ("Error in customattr");
1444 var ctor_type = customBuilder.Ctor is ConstructorBuilder ? ((ConstructorBuilder)customBuilder.Ctor).parameters[0] : customBuilder.Ctor.GetParametersInternal()[0].ParameterType;
1446 if (ctor_type.FullName == "System.Int16")
1448 int nnamed = (int)data [pos++];
1449 nnamed |= ((int)data [pos++]) << 8;
1450 for (int i = 0; i < nnamed; ++i) {
1451 //byte named_type = data [pos++];
1453 byte type = data [pos++];
1458 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1459 //string named_typename =
1460 CustomAttributeBuilder.string_from_bytes (data, pos, len);
1462 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1463 // See related code/FIXME in mono/mono/metadata/reflection.c
1466 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1467 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1469 /* all the fields are integers in StructLayout */
1470 int value = (int)data [pos++];
1471 value |= ((int)data [pos++]) << 8;
1472 value |= ((int)data [pos++]) << 16;
1473 value |= ((int)data [pos++]) << 24;
1474 switch (named_name) {
1476 switch ((CharSet)value) {
1479 attrs &= ~(TypeAttributes.UnicodeClass | TypeAttributes.AutoClass);
1481 case CharSet.Unicode:
1482 attrs &= ~TypeAttributes.AutoClass;
1483 attrs |= TypeAttributes.UnicodeClass;
1486 attrs &= ~TypeAttributes.UnicodeClass;
1487 attrs |= TypeAttributes.AutoClass;
1490 break; // error out...
1494 packing_size = (PackingSize)value;
1500 break; // error out...
1504 } else if (attrname == "System.Runtime.CompilerServices.SpecialNameAttribute") {
1505 attrs |= TypeAttributes.SpecialName;
1507 } else if (attrname == "System.SerializableAttribute") {
1508 attrs |= TypeAttributes.Serializable;
1510 } else if (attrname == "System.Runtime.InteropServices.ComImportAttribute") {
1511 attrs |= TypeAttributes.Import;
1513 } else if (attrname == "System.Security.SuppressUnmanagedCodeSecurityAttribute") {
1514 attrs |= TypeAttributes.HasSecurity;
1517 if (cattrs != null) {
1518 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1519 cattrs.CopyTo (new_array, 0);
1520 new_array [cattrs.Length] = customBuilder;
1523 cattrs = new CustomAttributeBuilder [1];
1524 cattrs [0] = customBuilder;
1529 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
1531 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1534 public EventBuilder DefineEvent (string name, EventAttributes attributes, Type eventtype)
1536 check_name ("name", name);
1537 if (eventtype == null)
1538 throw new ArgumentNullException ("type");
1539 check_not_created ();
1541 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1542 if (events != null) {
1543 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1544 System.Array.Copy (events, new_events, events.Length);
1545 new_events [events.Length] = res;
1546 events = new_events;
1548 events = new EventBuilder [1];
1554 public FieldBuilder DefineInitializedData (string name, byte[] data, FieldAttributes attributes) {
1556 throw new ArgumentNullException ("data");
1558 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1559 res.SetRVAData (data);
1563 public FieldBuilder DefineUninitializedData (string name, int size, FieldAttributes attributes)
1566 throw new ArgumentNullException ("name");
1567 if (name.Length == 0)
1568 throw new ArgumentException ("Empty name is not legal", "name");
1569 if ((size <= 0) || (size > 0x3f0000))
1570 throw new ArgumentException ("Data size must be > 0 and < 0x3f0000");
1571 check_not_created ();
1573 string typeName = "$ArrayType$" + size;
1574 Type datablobtype = pmodule.GetRegisteredType (fullname + "+" + typeName);
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;
1607 // will just set the parent-related bits if called a second time
1608 setup_internal_class (this);
1611 internal int get_next_table_index (object obj, int table, bool inc) {
1612 return pmodule.get_next_table_index (obj, table, inc);
1616 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1618 if (created == null)
1619 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1621 return created.GetInterfaceMap (interfaceType);
1624 internal override Type InternalResolve ()
1630 internal bool is_created {
1632 return createTypeCalled;
1636 private Exception not_supported ()
1638 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1641 private void check_not_created ()
1644 throw new InvalidOperationException ("Unable to change after type has been created.");
1647 private void check_created ()
1650 throw not_supported ();
1653 private void check_name (string argName, string name)
1656 throw new ArgumentNullException (argName);
1657 if (name.Length == 0)
1658 throw new ArgumentException ("Empty name is not legal", argName);
1659 if (name [0] == ((char)0))
1660 throw new ArgumentException ("Illegal name", argName);
1663 public override String ToString ()
1669 public override bool IsAssignableFrom (Type c)
1671 return base.IsAssignableFrom (c);
1674 [MonoTODO ("arrays")]
1675 internal bool IsAssignableTo (Type c)
1680 if (c.IsInterface) {
1681 if (parent != null && is_created) {
1682 if (c.IsAssignableFrom (parent))
1686 if (interfaces == null)
1688 foreach (Type t in interfaces)
1689 if (c.IsAssignableFrom (t))
1696 return c == typeof (object);
1698 return c.IsAssignableFrom (parent);
1701 public bool IsCreated ()
1706 public override Type[] GetGenericArguments ()
1708 if (generic_params == null)
1710 Type[] args = new Type [generic_params.Length];
1711 generic_params.CopyTo (args, 0);
1715 public override Type GetGenericTypeDefinition ()
1717 if (generic_params == null)
1718 throw new InvalidOperationException ("Type is not generic");
1722 public override bool ContainsGenericParameters {
1724 return generic_params != null;
1728 public extern override bool IsGenericParameter {
1729 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1733 public override GenericParameterAttributes GenericParameterAttributes {
1734 get { return GenericParameterAttributes.None; }
1737 public override bool IsGenericTypeDefinition {
1739 return generic_params != null;
1743 public override bool IsGenericType {
1744 get { return IsGenericTypeDefinition; }
1748 public override int GenericParameterPosition {
1754 public override MethodBase DeclaringMethod {
1760 public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
1763 throw new ArgumentNullException ("names");
1764 if (names.Length == 0)
1765 throw new ArgumentException ("names");
1767 setup_generic_class ();
1769 generic_params = new GenericTypeParameterBuilder [names.Length];
1770 for (int i = 0; i < names.Length; i++) {
1771 string item = names [i];
1773 throw new ArgumentNullException ("names");
1774 generic_params [i] = new GenericTypeParameterBuilder (this, null, item, i);
1777 return generic_params;
1780 public static ConstructorInfo GetConstructor (Type type, ConstructorInfo constructor)
1782 /*FIXME I would expect the same checks of GetMethod here*/
1784 throw new ArgumentException ("Type is not generic", "type");
1786 if (!type.IsGenericType)
1787 throw new ArgumentException ("Type is not a generic type", "type");
1789 if (type.IsGenericTypeDefinition)
1790 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1792 if (constructor == null)
1793 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1795 if (!constructor.DeclaringType.IsGenericTypeDefinition)
1796 throw new ArgumentException ("constructor declaring type is not a generic type definition", "constructor");
1797 if (constructor.DeclaringType != type.GetGenericTypeDefinition ())
1798 throw new ArgumentException ("constructor declaring type is not the generic type definition of type", "constructor");
1800 ConstructorInfo res = type.GetConstructor (constructor);
1802 throw new ArgumentException ("constructor not found");
1807 static bool IsValidGetMethodType (Type type)
1809 if (type is TypeBuilder || type is MonoGenericClass)
1811 /*GetMethod() must work with TypeBuilders after CreateType() was called.*/
1812 if (type.Module is ModuleBuilder)
1814 if (type.IsGenericParameter)
1817 Type[] inst = type.GetGenericArguments ();
1820 for (int i = 0; i < inst.Length; ++i) {
1821 if (IsValidGetMethodType (inst [i]))
1827 public static MethodInfo GetMethod (Type type, MethodInfo method)
1829 if (!IsValidGetMethodType (type))
1830 throw new ArgumentException ("type is not TypeBuilder but " + type.GetType (), "type");
1832 if (type is TypeBuilder && type.ContainsGenericParameters)
1833 type = type.MakeGenericType (type.GetGenericArguments ());
1835 if (!type.IsGenericType)
1836 throw new ArgumentException ("type is not a generic type", "type");
1838 if (!method.DeclaringType.IsGenericTypeDefinition)
1839 throw new ArgumentException ("method declaring type is not a generic type definition", "method");
1840 if (method.DeclaringType != type.GetGenericTypeDefinition ())
1841 throw new ArgumentException ("method declaring type is not the generic type definition of type", "method");
1843 throw new NullReferenceException (); //MS raises this instead of an ArgumentNullException
1845 MethodInfo res = type.GetMethod (method);
1847 throw new ArgumentException (String.Format ("method {0} not found in type {1}", method.Name, type));
1852 public static FieldInfo GetField (Type type, FieldInfo field)
1854 if (!type.IsGenericType)
1855 throw new ArgumentException ("Type is not a generic type", "type");
1857 if (type.IsGenericTypeDefinition)
1858 throw new ArgumentException ("Type cannot be a generic type definition", "type");
1860 if (field is FieldOnTypeBuilderInst)
1861 throw new ArgumentException ("The specified field must be declared on a generic type definition.", "field");
1863 if (field.DeclaringType != type.GetGenericTypeDefinition ())
1864 throw new ArgumentException ("field declaring type is not the generic type definition of type", "method");
1866 FieldInfo res = type.GetField (field);
1868 throw new System.Exception ("field not found");
1874 void _TypeBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1876 throw new NotImplementedException ();
1879 void _TypeBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1881 throw new NotImplementedException ();
1884 void _TypeBuilder.GetTypeInfoCount (out uint pcTInfo)
1886 throw new NotImplementedException ();
1889 void _TypeBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1891 throw new NotImplementedException ();
1894 internal override bool IsUserType {
1900 public override bool IsConstructedGenericType {
1901 get { return false; }
1904 public override bool IsAssignableFrom (TypeInfo typeInfo)
1906 return base.IsAssignableFrom (typeInfo);