2 // System.Reflection.Emit/TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
16 using System.Globalization;
17 using System.Collections;
18 using System.Security;
19 using System.Security.Permissions;
21 namespace System.Reflection.Emit {
23 public sealed class TypeBuilder : Type {
24 #region Sync with reflection.h
26 private string nspace;
28 private Type nesting_type;
29 private Type[] interfaces;
30 private int num_methods;
31 private MethodBuilder[] methods;
32 private ConstructorBuilder[] ctors;
33 private PropertyBuilder[] properties;
34 private int num_fields;
35 private FieldBuilder[] fields;
36 private EventBuilder[] events;
37 private CustomAttributeBuilder[] cattrs;
38 internal TypeBuilder[] subtypes;
39 private TypeAttributes attrs;
40 private int table_idx;
41 private ModuleBuilder pmodule;
42 private int class_size;
43 private PackingSize packing_size;
44 private MonoGenericParam[] generic_params;
45 private RefEmitPermissionSet[] permissions;
50 public const int UnspecifiedTypeSize = 0;
52 protected override TypeAttributes GetAttributeFlagsImpl () {
56 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57 private extern void setup_internal_class (TypeBuilder tb);
59 [MethodImplAttribute(MethodImplOptions.InternalCall)]
60 private extern void create_internal_class (TypeBuilder tb);
62 [MethodImplAttribute(MethodImplOptions.InternalCall)]
63 private extern void setup_generic_class (TypeBuilder tb);
65 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66 private extern EventInfo get_event_info (EventBuilder eb);
68 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
72 fullname = this.tname = "<Module>";
75 setup_internal_class (this);
78 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
82 this.class_size = type_size;
83 this.packing_size = packing_size;
84 this.nesting_type = nesting_type;
85 sep_index = name.LastIndexOf('.');
86 if (sep_index != -1) {
87 this.tname = name.Substring (sep_index + 1);
88 this.nspace = name.Substring (0, sep_index);
93 if (interfaces != null) {
94 this.interfaces = new Type[interfaces.Length];
95 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
99 table_idx = mb.get_next_table_index (this, 0x02, true);
100 setup_internal_class (this);
101 fullname = GetFullName ();
104 public override Assembly Assembly {
105 get {return pmodule.Assembly;}
108 public override string AssemblyQualifiedName {
110 return fullname + ", " + Assembly.GetName().FullName;
113 public override Type BaseType {
118 public override Type DeclaringType {get {return nesting_type;}}
120 /* public override bool IsSubclassOf (Type c)
137 public override Type UnderlyingSystemType {
139 // This should return the type itself for non-enum types but
141 if (fields != null) {
142 foreach (FieldBuilder f in fields) {
143 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
147 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
151 string GetFullName () {
152 if (nesting_type != null)
153 return String.Concat (nesting_type.FullName, "+", tname);
154 if ((nspace != null) && (nspace.Length > 0))
155 return String.Concat (nspace, ".", tname);
159 public override string FullName {
165 public override Guid GUID {
167 throw not_supported ();
171 public override Module Module {
172 get {return pmodule;}
174 public override string Name {
177 public override string Namespace {
180 public PackingSize PackingSize {
181 get {return packing_size;}
184 get { return class_size; }
186 public override Type ReflectedType {get {return nesting_type;}}
188 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
190 throw new ArgumentNullException ("pset");
191 if ((action == SecurityAction.RequestMinimum) ||
192 (action == SecurityAction.RequestOptional) ||
193 (action == SecurityAction.RequestRefuse))
194 throw new ArgumentException ("Request* values are not permitted", "action");
197 throw not_after_created ();
199 if (permissions != null) {
200 /* Check duplicate actions */
201 foreach (RefEmitPermissionSet set in permissions)
202 if (set.action == action)
203 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
205 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
206 permissions.CopyTo (new_array, 0);
207 permissions = new_array;
210 permissions = new RefEmitPermissionSet [1];
212 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
213 attrs |= TypeAttributes.HasSecurity;
216 public void AddInterfaceImplementation( Type interfaceType) {
217 if (interfaceType == null)
218 throw new ArgumentNullException ("interfaceType");
220 throw not_after_created ();
222 if (interfaces != null) {
223 // Check for duplicates
224 foreach (Type t in interfaces)
225 if (t == interfaceType)
228 Type[] ifnew = new Type [interfaces.Length + 1];
229 interfaces.CopyTo (ifnew, 0);
230 ifnew [interfaces.Length] = interfaceType;
233 interfaces = new Type [1];
234 interfaces [0] = interfaceType;
239 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
240 CallingConventions callConvention, Type[] types,
241 ParameterModifier[] modifiers)
246 ConstructorBuilder found = null;
249 foreach (ConstructorBuilder cb in ctors){
250 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
260 throw new AmbiguousMatchException ();
263 MethodBase[] match = new MethodBase [count];
268 foreach (ConstructorInfo m in ctors) {
269 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
275 binder = Binder.DefaultBinder;
276 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
279 public override bool IsDefined( Type attributeType, bool inherit)
282 * MS throws NotSupported here, but we can't because some corlib
283 * classes make calls to IsDefined.
285 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
288 public override object[] GetCustomAttributes(bool inherit)
290 throw not_supported ();
293 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
295 throw not_supported ();
298 public TypeBuilder DefineNestedType (string name) {
299 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
302 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
303 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
306 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
307 return DefineNestedType (name, attr, parent, null);
310 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
311 PackingSize packsize, int typesize)
313 check_name ("name", name);
314 // Visibility must be NestedXXX
316 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
317 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
318 throw new ArgumentException ("attr", "Bad type flags for nested type.");
320 if (interfaces != null)
321 foreach (Type iface in interfaces)
323 throw new ArgumentNullException ("interfaces");
325 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
326 res.fullname = res.GetFullName ();
327 pmodule.RegisterTypeName (res, res.fullname);
328 if (subtypes != null) {
329 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
330 System.Array.Copy (subtypes, new_types, subtypes.Length);
331 new_types [subtypes.Length] = res;
332 subtypes = new_types;
334 subtypes = new TypeBuilder [1];
340 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
341 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
344 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
345 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
348 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
349 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
352 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
353 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
361 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
364 throw not_after_created ();
365 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
367 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
368 System.Array.Copy (ctors, new_ctors, ctors.Length);
369 new_ctors [ctors.Length] = cb;
372 ctors = new ConstructorBuilder [1];
378 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
380 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
385 parent_type = parent;
387 parent_type = pmodule.assemblyb.corlib_object_type;
389 ConstructorInfo parent_constructor =
390 parent_type.GetConstructor (
391 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
392 null, Type.EmptyTypes, null);
394 ILGenerator ig = cb.GetILGenerator ();
395 if (parent_constructor != null){
396 ig.Emit (OpCodes.Ldarg_0);
397 ig.Emit (OpCodes.Call, parent_constructor);
399 ig.Emit (OpCodes.Ret);
403 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
404 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
407 private void append_method (MethodBuilder mb) {
408 if (methods != null) {
409 if (methods.Length == num_methods) {
410 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
411 System.Array.Copy (methods, new_methods, num_methods);
412 methods = new_methods;
415 methods = new MethodBuilder [1];
417 methods [num_methods] = mb;
421 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
422 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
430 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
431 check_name ("name", name);
433 throw not_after_created ();
435 !((attributes & MethodAttributes.Abstract) != 0) ||
436 !((attributes & MethodAttributes.Virtual) != 0)))
437 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
439 if (returnType == null)
440 returnType = pmodule.assemblyb.corlib_void_type;
441 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
446 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
447 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
455 MethodBuilder DefinePInvokeMethod (
458 string entryName, MethodAttributes attributes,
459 CallingConventions callingConvention,
461 Type[] returnTypeRequiredCustomModifiers,
462 Type[] returnTypeOptionalCustomModifiers,
463 Type[] parameterTypes,
464 Type[][] parameterTypeRequiredCustomModifiers,
465 Type[][] parameterTypeOptionalCustomModifiers,
466 CallingConvention nativeCallConv,
467 CharSet nativeCharSet) {
468 check_name ("name", name);
469 check_name ("dllName", dllName);
470 check_name ("entryName", entryName);
471 if ((attributes & MethodAttributes.Abstract) != 0)
472 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
474 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
476 throw not_after_created ();
479 = new MethodBuilder (
485 returnTypeRequiredCustomModifiers,
486 returnTypeOptionalCustomModifiers,
488 parameterTypeRequiredCustomModifiers,
489 parameterTypeOptionalCustomModifiers,
498 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
499 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
500 nativeCallConv, nativeCharSet);
503 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
504 if (methodInfoBody == null)
505 throw new ArgumentNullException ("methodInfoBody");
506 if (methodInfoDeclaration == null)
507 throw new ArgumentNullException ("methodInfoDeclaration");
509 throw not_after_created ();
511 if (methodInfoBody is MethodBuilder) {
512 MethodBuilder mb = (MethodBuilder)methodInfoBody;
513 mb.set_override (methodInfoDeclaration);
517 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
518 return DefineField (fieldName, type, null, null, attributes);
526 FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
527 check_name ("fieldName", fieldName);
528 if (type == typeof (void))
529 throw new ArgumentException ("type", "Bad field type in defining field.");
531 throw not_after_created ();
533 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
534 if (fields != null) {
535 if (fields.Length == num_fields) {
536 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
537 System.Array.Copy (fields, new_fields, num_fields);
540 fields [num_fields] = res;
543 fields = new FieldBuilder [1];
546 create_internal_class (this);
551 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
552 check_name ("name", name);
553 if (parameterTypes != null)
554 foreach (Type param in parameterTypes)
556 throw new ArgumentNullException ("parameterTypes");
558 throw not_after_created ();
560 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
562 if (properties != null) {
563 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
564 System.Array.Copy (properties, new_properties, properties.Length);
565 new_properties [properties.Length] = res;
566 properties = new_properties;
568 properties = new PropertyBuilder [1];
569 properties [0] = res;
574 public ConstructorBuilder DefineTypeInitializer() {
575 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
578 [MethodImplAttribute(MethodImplOptions.InternalCall)]
579 private extern Type create_runtime_class (TypeBuilder tb);
581 private bool is_nested_in (Type t) {
591 public Type CreateType() {
592 /* handle nesting_type */
594 throw not_after_created ();
596 // Fire TypeResolve events for fields whose type is an unfinished
598 if (fields != null) {
599 foreach (FieldBuilder fb in fields) {
602 Type ft = fb.FieldType;
603 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
604 TypeBuilder tb = (TypeBuilder)ft;
605 if (!tb.is_created) {
606 AppDomain.CurrentDomain.DoTypeResolve (tb);
607 if (!tb.is_created) {
608 // FIXME: We should throw an exception here,
609 // but mcs expects that the type is created
610 // even if the exception is thrown
611 //throw new TypeLoadException ("Could not load type " + tb);
618 if (methods != null) {
619 for (int i = 0; i < num_methods; ++i)
620 ((MethodBuilder)(methods[i])).fixup ();
624 // On classes, define a default constructor if not provided
626 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
627 DefineDefaultConstructor (MethodAttributes.Public);
630 foreach (ConstructorBuilder ctor in ctors)
634 created = create_runtime_class (this);
640 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
643 return new ConstructorInfo [0];
644 ArrayList l = new ArrayList ();
646 MethodAttributes mattrs;
648 foreach (ConstructorBuilder c in ctors) {
650 mattrs = c.Attributes;
651 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
652 if ((bindingAttr & BindingFlags.Public) != 0)
655 if ((bindingAttr & BindingFlags.NonPublic) != 0)
661 if ((mattrs & MethodAttributes.Static) != 0) {
662 if ((bindingAttr & BindingFlags.Static) != 0)
665 if ((bindingAttr & BindingFlags.Instance) != 0)
672 ConstructorInfo[] result = new ConstructorInfo [l.Count];
677 public override Type GetElementType () {
678 throw not_supported ();
681 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
682 throw not_supported ();
685 /* Needed to keep signature compatibility with MS.NET */
686 public override EventInfo[] GetEvents ()
688 return GetEvents (DefaultBindingFlags);
691 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
692 // FIXME: Under MS.NET, this throws a NotImplementedException
693 // But mcs calls this method. How can that be?
694 return new EventInfo [0];
697 // This is only used from MonoGenericInst.initialize().
698 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
701 return new EventInfo [0];
702 ArrayList l = new ArrayList ();
704 MethodAttributes mattrs;
707 foreach (EventBuilder eb in events) {
710 EventInfo c = get_event_info (eb);
712 accessor = c.GetAddMethod (true);
713 if (accessor == null)
714 accessor = c.GetRemoveMethod (true);
715 if (accessor == null)
717 mattrs = accessor.Attributes;
718 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
719 if ((bindingAttr & BindingFlags.Public) != 0)
722 if ((bindingAttr & BindingFlags.NonPublic) != 0)
728 if ((mattrs & MethodAttributes.Static) != 0) {
729 if ((bindingAttr & BindingFlags.Static) != 0)
732 if ((bindingAttr & BindingFlags.Instance) != 0)
739 EventInfo[] result = new EventInfo [l.Count];
744 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
749 FieldAttributes mattrs;
751 foreach (FieldInfo c in fields) {
757 mattrs = c.Attributes;
758 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
759 if ((bindingAttr & BindingFlags.Public) != 0)
762 if ((bindingAttr & BindingFlags.NonPublic) != 0)
768 if ((mattrs & FieldAttributes.Static) != 0) {
769 if ((bindingAttr & BindingFlags.Static) != 0)
772 if ((bindingAttr & BindingFlags.Instance) != 0)
782 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
784 return new FieldInfo [0];
785 ArrayList l = new ArrayList ();
787 FieldAttributes mattrs;
789 foreach (FieldInfo c in fields) {
793 mattrs = c.Attributes;
794 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
795 if ((bindingAttr & BindingFlags.Public) != 0)
798 if ((bindingAttr & BindingFlags.NonPublic) != 0)
804 if ((mattrs & FieldAttributes.Static) != 0) {
805 if ((bindingAttr & BindingFlags.Static) != 0)
808 if ((bindingAttr & BindingFlags.Instance) != 0)
815 FieldInfo[] result = new FieldInfo [l.Count];
820 public override Type GetInterface (string name, bool ignoreCase) {
821 throw not_supported ();
824 public override Type[] GetInterfaces () {
825 if (interfaces != null) {
826 Type[] ret = new Type [interfaces.Length];
827 interfaces.CopyTo (ret, 0);
830 return Type.EmptyTypes;
834 public override MemberInfo[] GetMember (string name, MemberTypes type,
835 BindingFlags bindingAttr) {
836 throw not_supported ();
839 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
840 throw not_supported ();
843 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
844 MethodInfo[] candidates;
845 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
846 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
848 candidates = parent_methods;
850 candidates = new MethodInfo [methods.Length + parent_methods.Length];
851 parent_methods.CopyTo (candidates, 0);
852 methods.CopyTo (candidates, parent_methods.Length);
856 candidates = methods;
858 if (candidates == null)
859 return new MethodInfo [0];
861 ArrayList l = new ArrayList ();
863 MethodAttributes mattrs;
865 foreach (MethodInfo c in candidates) {
869 if (String.Compare (c.Name, name, ignoreCase) != 0)
873 mattrs = c.Attributes;
874 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
875 if ((bindingAttr & BindingFlags.Public) != 0)
878 if ((bindingAttr & BindingFlags.NonPublic) != 0)
884 if ((mattrs & MethodAttributes.Static) != 0) {
885 if ((bindingAttr & BindingFlags.Static) != 0)
888 if ((bindingAttr & BindingFlags.Instance) != 0)
896 MethodInfo[] result = new MethodInfo [l.Count];
901 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
902 return GetMethodsByName (null, bindingAttr, false, this);
905 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
907 CallingConventions callConvention,
908 Type[] types, ParameterModifier[] modifiers)
911 /* MS.Net throws this exception if the type is unfinished... */
912 throw not_supported ();
914 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
915 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
916 MethodInfo found = null;
918 int typesLen = (types != null) ? types.Length : 0;
921 foreach (MethodInfo m in methods) {
922 // Under MS.NET, Standard|HasThis matches Standard...
923 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
932 if (count == 1 && typesLen == 0)
935 match = new MethodBase [count];
940 foreach (MethodInfo m in methods) {
941 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
948 return (MethodInfo) Binder.FindMostDerivedMatch (match);
951 binder = Binder.DefaultBinder;
953 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
956 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
957 throw not_supported ();
960 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
962 ArrayList result = new ArrayList ();
964 if (subtypes == null)
965 return Type.EmptyTypes;
966 foreach (TypeBuilder t in subtypes) {
968 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
969 if ((bindingAttr & BindingFlags.Public) != 0)
972 if ((bindingAttr & BindingFlags.NonPublic) != 0)
979 Type[] r = new Type [result.Count];
984 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
985 if (properties == null)
986 return new PropertyInfo [0];
987 ArrayList l = new ArrayList ();
989 MethodAttributes mattrs;
992 foreach (PropertyInfo c in properties) {
994 accessor = c.GetGetMethod (true);
995 if (accessor == null)
996 accessor = c.GetSetMethod (true);
997 if (accessor == null)
999 mattrs = accessor.Attributes;
1000 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1001 if ((bindingAttr & BindingFlags.Public) != 0)
1004 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1010 if ((mattrs & MethodAttributes.Static) != 0) {
1011 if ((bindingAttr & BindingFlags.Static) != 0)
1014 if ((bindingAttr & BindingFlags.Instance) != 0)
1021 PropertyInfo[] result = new PropertyInfo [l.Count];
1026 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1027 throw not_supported ();
1030 protected override bool HasElementTypeImpl () {
1031 // According to the MSDN docs, this is supported for TypeBuilders,
1032 // but in reality, it is not
1033 throw not_supported ();
1034 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1037 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1038 throw not_supported ();
1041 protected override bool IsArrayImpl ()
1043 return Type.IsArrayImpl (this);
1046 protected override bool IsByRefImpl () {
1050 protected override bool IsCOMObjectImpl () {
1053 protected override bool IsPointerImpl () {
1057 protected override bool IsPrimitiveImpl () {
1061 protected override bool IsValueTypeImpl () {
1062 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1063 this != pmodule.assemblyb.corlib_value_type &&
1064 this != pmodule.assemblyb.corlib_enum_type);
1067 public override RuntimeTypeHandle TypeHandle {
1069 throw not_supported ();
1073 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1074 if (customBuilder == null)
1075 throw new ArgumentNullException ("customBuilder");
1077 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1078 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1079 byte[] data = customBuilder.Data;
1080 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1081 layout_kind = (int)data [2];
1082 layout_kind |= ((int)data [3]) << 8;
1083 attrs &= ~TypeAttributes.LayoutMask;
1084 switch ((LayoutKind)layout_kind) {
1085 case LayoutKind.Auto:
1086 attrs |= TypeAttributes.AutoLayout;
1088 case LayoutKind.Explicit:
1089 attrs |= TypeAttributes.ExplicitLayout;
1091 case LayoutKind.Sequential:
1092 attrs |= TypeAttributes.SequentialLayout;
1095 // we should ignore it since it can be any value anyway...
1096 throw new Exception ("Error in customattr");
1098 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1100 if (first_type_name == "System.Int16")
1102 int nnamed = (int)data [pos++];
1103 nnamed |= ((int)data [pos++]) << 8;
1104 for (int i = 0; i < nnamed; ++i) {
1105 byte named_type = data [pos++];
1106 byte type = data [pos++];
1107 int len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1108 string named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1110 /* all the fields are integers in StructLayout */
1111 int value = (int)data [pos++];
1112 value |= ((int)data [pos++]) << 8;
1113 value |= ((int)data [pos++]) << 16;
1114 value |= ((int)data [pos++]) << 24;
1115 switch (named_name) {
1117 switch ((CharSet)value) {
1121 case CharSet.Unicode:
1122 attrs |= TypeAttributes.UnicodeClass;
1125 attrs |= TypeAttributes.AutoClass;
1128 break; // error out...
1132 packing_size = (PackingSize)value;
1138 break; // error out...
1142 } else if (attrname == "System.SerializableAttribute") {
1143 attrs |= TypeAttributes.Serializable;
1146 if (cattrs != null) {
1147 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1148 cattrs.CopyTo (new_array, 0);
1149 new_array [cattrs.Length] = customBuilder;
1152 cattrs = new CustomAttributeBuilder [1];
1153 cattrs [0] = customBuilder;
1156 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1157 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1160 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1161 check_name ("name", name);
1162 if (eventtype == null)
1163 throw new ArgumentNullException ("eventtype");
1165 throw not_after_created ();
1167 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1168 if (events != null) {
1169 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1170 System.Array.Copy (events, new_events, events.Length);
1171 new_events [events.Length] = res;
1172 events = new_events;
1174 events = new EventBuilder [1];
1180 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1182 throw new ArgumentNullException ("data");
1183 if ((data.Length == 0) || (data.Length > 0x3f0000))
1184 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1186 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1187 res.SetRVAData (data);
1192 static int UnmanagedDataCount = 0;
1194 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1195 check_name ("name", name);
1196 if ((size <= 0) || (size > 0x3f0000))
1197 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1199 throw not_after_created ();
1201 string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1202 UnmanagedDataCount++;
1203 TypeBuilder datablobtype = DefineNestedType (s,
1204 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1205 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1206 datablobtype.CreateType ();
1207 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1210 public TypeToken TypeToken {
1212 return new TypeToken (0x02000000 | table_idx);
1215 public void SetParent (Type parentType) {
1216 if (parentType == null)
1217 throw new ArgumentNullException ("parentType");
1219 throw not_after_created ();
1221 parent = parentType;
1223 internal int get_next_table_index (object obj, int table, bool inc) {
1224 return pmodule.get_next_table_index (obj, table, inc);
1227 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1229 if (created == null)
1230 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1232 return created.GetInterfaceMap (interfaceType);
1235 internal bool is_created {
1237 return created != null;
1241 private Exception not_supported ()
1243 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1246 private Exception not_after_created ()
1248 return new InvalidOperationException ("Unable to change after type has been created.");
1251 private void check_name (string argName, string name)
1254 throw new ArgumentNullException (argName);
1256 throw new ArgumentException (argName, "Empty name is not legal.");
1257 if (name.IndexOf ((char)0) != -1)
1258 throw new ArgumentException (argName, "Illegal name.");
1262 public override Type GetGenericTypeDefinition ()
1264 setup_generic_class (this);
1266 return base.GetGenericTypeDefinition ();
1269 public override bool HasGenericArguments {
1271 throw new NotImplementedException ();
1275 public override bool ContainsGenericParameters {
1277 return generic_params != null;
1281 public extern override bool IsGenericParameter {
1282 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1286 public override int GenericParameterPosition {
1288 throw new NotImplementedException ();
1292 public override MethodInfo DeclaringMethod {
1294 throw new NotImplementedException ();
1298 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1299 private extern MonoGenericParam define_generic_parameter (string name, int index);
1301 public Type DefineGenericParameter (string name)
1304 if (generic_params != null) {
1305 MonoGenericParam[] new_generic_params = new MonoGenericParam [generic_params.Length+1];
1306 System.Array.Copy (generic_params, new_generic_params, generic_params.Length);
1307 index = generic_params.Length;
1308 generic_params = new_generic_params;
1310 generic_params = new MonoGenericParam [1];
1314 generic_params [index] = define_generic_parameter (name, index);
1315 return generic_params [index];
1318 public void SetGenericParameterConstraints (int index, Type[] constraints, bool has_ctor_constraint)
1320 generic_params [index].SetConstraints (constraints, has_ctor_constraint);
1323 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1325 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);