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 #if NET_2_0 || BOOTSTRAP_NET_2_0
45 private GenericTypeParameterBuilder[] generic_params;
47 private Object generic_params; /* so offsets don't change */
49 private RefEmitPermissionSet[] permissions;
54 public const int UnspecifiedTypeSize = 0;
56 protected override TypeAttributes GetAttributeFlagsImpl () {
60 [MethodImplAttribute(MethodImplOptions.InternalCall)]
61 private extern void setup_internal_class (TypeBuilder tb);
63 [MethodImplAttribute(MethodImplOptions.InternalCall)]
64 private extern void create_internal_class (TypeBuilder tb);
66 [MethodImplAttribute(MethodImplOptions.InternalCall)]
67 private extern void setup_generic_class (TypeBuilder tb);
69 [MethodImplAttribute(MethodImplOptions.InternalCall)]
70 private extern EventInfo get_event_info (EventBuilder eb);
72 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
76 fullname = this.tname = "<Module>";
79 setup_internal_class (this);
82 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
86 this.class_size = type_size;
87 this.packing_size = packing_size;
88 this.nesting_type = nesting_type;
89 sep_index = name.LastIndexOf('.');
90 if (sep_index != -1) {
91 this.tname = name.Substring (sep_index + 1);
92 this.nspace = name.Substring (0, sep_index);
97 if (interfaces != null) {
98 this.interfaces = new Type[interfaces.Length];
99 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
103 table_idx = mb.get_next_table_index (this, 0x02, true);
104 setup_internal_class (this);
105 fullname = GetFullName ();
108 public override Assembly Assembly {
109 get {return pmodule.Assembly;}
112 public override string AssemblyQualifiedName {
114 return fullname + ", " + Assembly.GetName().FullName;
117 public override Type BaseType {
122 public override Type DeclaringType {get {return nesting_type;}}
124 /* public override bool IsSubclassOf (Type c)
141 public override Type UnderlyingSystemType {
143 // This should return the type itself for non-enum types but
145 if (fields != null) {
146 foreach (FieldBuilder f in fields) {
147 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
151 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
155 string GetFullName () {
156 if (nesting_type != null)
157 return String.Concat (nesting_type.FullName, "+", tname);
158 if ((nspace != null) && (nspace.Length > 0))
159 return String.Concat (nspace, ".", tname);
163 public override string FullName {
169 public override Guid GUID {
171 throw not_supported ();
175 public override Module Module {
176 get {return pmodule;}
178 public override string Name {
181 public override string Namespace {
184 public PackingSize PackingSize {
185 get {return packing_size;}
188 get { return class_size; }
190 public override Type ReflectedType {get {return nesting_type;}}
192 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
194 throw new ArgumentNullException ("pset");
195 if ((action == SecurityAction.RequestMinimum) ||
196 (action == SecurityAction.RequestOptional) ||
197 (action == SecurityAction.RequestRefuse))
198 throw new ArgumentException ("Request* values are not permitted", "action");
201 throw not_after_created ();
203 if (permissions != null) {
204 /* Check duplicate actions */
205 foreach (RefEmitPermissionSet set in permissions)
206 if (set.action == action)
207 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
209 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
210 permissions.CopyTo (new_array, 0);
211 permissions = new_array;
214 permissions = new RefEmitPermissionSet [1];
216 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
217 attrs |= TypeAttributes.HasSecurity;
220 public void AddInterfaceImplementation( Type interfaceType) {
221 if (interfaceType == null)
222 throw new ArgumentNullException ("interfaceType");
224 throw not_after_created ();
226 if (interfaces != null) {
227 // Check for duplicates
228 foreach (Type t in interfaces)
229 if (t == interfaceType)
232 Type[] ifnew = new Type [interfaces.Length + 1];
233 interfaces.CopyTo (ifnew, 0);
234 ifnew [interfaces.Length] = interfaceType;
237 interfaces = new Type [1];
238 interfaces [0] = interfaceType;
243 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
244 CallingConventions callConvention, Type[] types,
245 ParameterModifier[] modifiers)
250 ConstructorBuilder found = null;
253 foreach (ConstructorBuilder cb in ctors){
254 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
264 throw new AmbiguousMatchException ();
267 MethodBase[] match = new MethodBase [count];
272 foreach (ConstructorInfo m in ctors) {
273 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
279 binder = Binder.DefaultBinder;
280 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
283 public override bool IsDefined( Type attributeType, bool inherit)
286 * MS throws NotSupported here, but we can't because some corlib
287 * classes make calls to IsDefined.
289 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
292 public override object[] GetCustomAttributes(bool inherit)
294 throw not_supported ();
297 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
299 throw not_supported ();
302 public TypeBuilder DefineNestedType (string name) {
303 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
306 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
307 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
310 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
311 return DefineNestedType (name, attr, parent, null);
314 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
315 PackingSize packsize, int typesize)
317 check_name ("name", name);
318 // Visibility must be NestedXXX
320 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
321 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
322 throw new ArgumentException ("attr", "Bad type flags for nested type.");
324 if (interfaces != null)
325 foreach (Type iface in interfaces)
327 throw new ArgumentNullException ("interfaces");
329 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
330 res.fullname = res.GetFullName ();
331 pmodule.RegisterTypeName (res, res.fullname);
332 if (subtypes != null) {
333 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
334 System.Array.Copy (subtypes, new_types, subtypes.Length);
335 new_types [subtypes.Length] = res;
336 subtypes = new_types;
338 subtypes = new TypeBuilder [1];
344 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
345 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
348 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
349 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
352 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
353 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
356 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
357 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
360 #if NET_2_0 || BOOTSTRAP_NET_2_0
365 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
368 throw not_after_created ();
369 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
371 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
372 System.Array.Copy (ctors, new_ctors, ctors.Length);
373 new_ctors [ctors.Length] = cb;
376 ctors = new ConstructorBuilder [1];
382 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
384 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
389 parent_type = parent;
391 parent_type = pmodule.assemblyb.corlib_object_type;
393 ConstructorInfo parent_constructor =
394 parent_type.GetConstructor (
395 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
396 null, Type.EmptyTypes, null);
398 ILGenerator ig = cb.GetILGenerator ();
399 if (parent_constructor != null){
400 ig.Emit (OpCodes.Ldarg_0);
401 ig.Emit (OpCodes.Call, parent_constructor);
403 ig.Emit (OpCodes.Ret);
407 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
408 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
411 private void append_method (MethodBuilder mb) {
412 if (methods != null) {
413 if (methods.Length == num_methods) {
414 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
415 System.Array.Copy (methods, new_methods, num_methods);
416 methods = new_methods;
419 methods = new MethodBuilder [1];
421 methods [num_methods] = mb;
425 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
426 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
429 #if NET_2_0 || BOOTSTRAP_NET_2_0
434 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
435 check_name ("name", name);
437 throw not_after_created ();
439 !((attributes & MethodAttributes.Abstract) != 0) ||
440 !((attributes & MethodAttributes.Virtual) != 0)))
441 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
443 if (returnType == null)
444 returnType = pmodule.assemblyb.corlib_void_type;
445 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
450 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
451 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
454 #if NET_2_0 || BOOTSTRAP_NET_2_0
459 MethodBuilder DefinePInvokeMethod (
462 string entryName, MethodAttributes attributes,
463 CallingConventions callingConvention,
465 Type[] returnTypeRequiredCustomModifiers,
466 Type[] returnTypeOptionalCustomModifiers,
467 Type[] parameterTypes,
468 Type[][] parameterTypeRequiredCustomModifiers,
469 Type[][] parameterTypeOptionalCustomModifiers,
470 CallingConvention nativeCallConv,
471 CharSet nativeCharSet) {
472 check_name ("name", name);
473 check_name ("dllName", dllName);
474 check_name ("entryName", entryName);
475 if ((attributes & MethodAttributes.Abstract) != 0)
476 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
478 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
480 throw not_after_created ();
483 = new MethodBuilder (
489 returnTypeRequiredCustomModifiers,
490 returnTypeOptionalCustomModifiers,
492 parameterTypeRequiredCustomModifiers,
493 parameterTypeOptionalCustomModifiers,
502 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
503 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
504 nativeCallConv, nativeCharSet);
507 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
508 if (methodInfoBody == null)
509 throw new ArgumentNullException ("methodInfoBody");
510 if (methodInfoDeclaration == null)
511 throw new ArgumentNullException ("methodInfoDeclaration");
513 throw not_after_created ();
515 if (methodInfoBody is MethodBuilder) {
516 MethodBuilder mb = (MethodBuilder)methodInfoBody;
517 mb.set_override (methodInfoDeclaration);
521 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
522 return DefineField (fieldName, type, null, null, attributes);
525 #if NET_2_0 || BOOTSTRAP_NET_2_0
530 FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
531 check_name ("fieldName", fieldName);
532 if (type == typeof (void))
533 throw new ArgumentException ("type", "Bad field type in defining field.");
535 throw not_after_created ();
537 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
538 if (fields != null) {
539 if (fields.Length == num_fields) {
540 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
541 System.Array.Copy (fields, new_fields, num_fields);
544 fields [num_fields] = res;
547 fields = new FieldBuilder [1];
550 create_internal_class (this);
555 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
556 check_name ("name", name);
557 if (parameterTypes != null)
558 foreach (Type param in parameterTypes)
560 throw new ArgumentNullException ("parameterTypes");
562 throw not_after_created ();
564 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
566 if (properties != null) {
567 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
568 System.Array.Copy (properties, new_properties, properties.Length);
569 new_properties [properties.Length] = res;
570 properties = new_properties;
572 properties = new PropertyBuilder [1];
573 properties [0] = res;
578 public ConstructorBuilder DefineTypeInitializer() {
579 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
582 [MethodImplAttribute(MethodImplOptions.InternalCall)]
583 private extern Type create_runtime_class (TypeBuilder tb);
585 private bool is_nested_in (Type t) {
595 public Type CreateType() {
596 /* handle nesting_type */
598 throw not_after_created ();
600 // Fire TypeResolve events for fields whose type is an unfinished
602 if (fields != null) {
603 foreach (FieldBuilder fb in fields) {
606 Type ft = fb.FieldType;
607 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
608 TypeBuilder tb = (TypeBuilder)ft;
609 if (!tb.is_created) {
610 AppDomain.CurrentDomain.DoTypeResolve (tb);
611 if (!tb.is_created) {
612 // FIXME: We should throw an exception here,
613 // but mcs expects that the type is created
614 // even if the exception is thrown
615 //throw new TypeLoadException ("Could not load type " + tb);
622 if (methods != null) {
623 for (int i = 0; i < num_methods; ++i)
624 ((MethodBuilder)(methods[i])).fixup ();
628 // On classes, define a default constructor if not provided
630 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
631 DefineDefaultConstructor (MethodAttributes.Public);
634 foreach (ConstructorBuilder ctor in ctors)
638 created = create_runtime_class (this);
644 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
647 return new ConstructorInfo [0];
648 ArrayList l = new ArrayList ();
650 MethodAttributes mattrs;
652 foreach (ConstructorBuilder c in ctors) {
654 mattrs = c.Attributes;
655 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
656 if ((bindingAttr & BindingFlags.Public) != 0)
659 if ((bindingAttr & BindingFlags.NonPublic) != 0)
665 if ((mattrs & MethodAttributes.Static) != 0) {
666 if ((bindingAttr & BindingFlags.Static) != 0)
669 if ((bindingAttr & BindingFlags.Instance) != 0)
676 ConstructorInfo[] result = new ConstructorInfo [l.Count];
681 public override Type GetElementType () {
682 throw not_supported ();
685 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
686 throw not_supported ();
689 /* Needed to keep signature compatibility with MS.NET */
690 public override EventInfo[] GetEvents ()
692 return GetEvents (DefaultBindingFlags);
695 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
696 // FIXME: Under MS.NET, this throws a NotImplementedException
697 // But mcs calls this method. How can that be?
698 return new EventInfo [0];
701 // This is only used from MonoGenericInst.initialize().
702 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
705 return new EventInfo [0];
706 ArrayList l = new ArrayList ();
708 MethodAttributes mattrs;
711 foreach (EventBuilder eb in events) {
714 EventInfo c = get_event_info (eb);
716 accessor = c.GetAddMethod (true);
717 if (accessor == null)
718 accessor = c.GetRemoveMethod (true);
719 if (accessor == null)
721 mattrs = accessor.Attributes;
722 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
723 if ((bindingAttr & BindingFlags.Public) != 0)
726 if ((bindingAttr & BindingFlags.NonPublic) != 0)
732 if ((mattrs & MethodAttributes.Static) != 0) {
733 if ((bindingAttr & BindingFlags.Static) != 0)
736 if ((bindingAttr & BindingFlags.Instance) != 0)
743 EventInfo[] result = new EventInfo [l.Count];
748 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
753 FieldAttributes mattrs;
755 foreach (FieldInfo c in fields) {
761 mattrs = c.Attributes;
762 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
763 if ((bindingAttr & BindingFlags.Public) != 0)
766 if ((bindingAttr & BindingFlags.NonPublic) != 0)
772 if ((mattrs & FieldAttributes.Static) != 0) {
773 if ((bindingAttr & BindingFlags.Static) != 0)
776 if ((bindingAttr & BindingFlags.Instance) != 0)
786 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
788 return new FieldInfo [0];
789 ArrayList l = new ArrayList ();
791 FieldAttributes mattrs;
793 foreach (FieldInfo c in fields) {
797 mattrs = c.Attributes;
798 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
799 if ((bindingAttr & BindingFlags.Public) != 0)
802 if ((bindingAttr & BindingFlags.NonPublic) != 0)
808 if ((mattrs & FieldAttributes.Static) != 0) {
809 if ((bindingAttr & BindingFlags.Static) != 0)
812 if ((bindingAttr & BindingFlags.Instance) != 0)
819 FieldInfo[] result = new FieldInfo [l.Count];
824 public override Type GetInterface (string name, bool ignoreCase) {
825 throw not_supported ();
828 public override Type[] GetInterfaces () {
829 if (interfaces != null) {
830 Type[] ret = new Type [interfaces.Length];
831 interfaces.CopyTo (ret, 0);
834 return Type.EmptyTypes;
838 public override MemberInfo[] GetMember (string name, MemberTypes type,
839 BindingFlags bindingAttr) {
840 throw not_supported ();
843 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
844 throw not_supported ();
847 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
848 MethodInfo[] candidates;
849 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
850 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
852 candidates = parent_methods;
854 candidates = new MethodInfo [methods.Length + parent_methods.Length];
855 parent_methods.CopyTo (candidates, 0);
856 methods.CopyTo (candidates, parent_methods.Length);
860 candidates = methods;
862 if (candidates == null)
863 return new MethodInfo [0];
865 ArrayList l = new ArrayList ();
867 MethodAttributes mattrs;
869 foreach (MethodInfo c in candidates) {
873 if (String.Compare (c.Name, name, ignoreCase) != 0)
877 mattrs = c.Attributes;
878 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
879 if ((bindingAttr & BindingFlags.Public) != 0)
882 if ((bindingAttr & BindingFlags.NonPublic) != 0)
888 if ((mattrs & MethodAttributes.Static) != 0) {
889 if ((bindingAttr & BindingFlags.Static) != 0)
892 if ((bindingAttr & BindingFlags.Instance) != 0)
900 MethodInfo[] result = new MethodInfo [l.Count];
905 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
906 return GetMethodsByName (null, bindingAttr, false, this);
909 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
911 CallingConventions callConvention,
912 Type[] types, ParameterModifier[] modifiers)
915 /* MS.Net throws this exception if the type is unfinished... */
916 throw not_supported ();
918 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
919 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
920 MethodInfo found = null;
922 int typesLen = (types != null) ? types.Length : 0;
925 foreach (MethodInfo m in methods) {
926 // Under MS.NET, Standard|HasThis matches Standard...
927 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
936 if (count == 1 && typesLen == 0)
939 match = new MethodBase [count];
944 foreach (MethodInfo m in methods) {
945 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
952 return (MethodInfo) Binder.FindMostDerivedMatch (match);
955 binder = Binder.DefaultBinder;
957 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
960 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
961 throw not_supported ();
964 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
966 ArrayList result = new ArrayList ();
968 if (subtypes == null)
969 return Type.EmptyTypes;
970 foreach (TypeBuilder t in subtypes) {
972 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
973 if ((bindingAttr & BindingFlags.Public) != 0)
976 if ((bindingAttr & BindingFlags.NonPublic) != 0)
983 Type[] r = new Type [result.Count];
988 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
989 if (properties == null)
990 return new PropertyInfo [0];
991 ArrayList l = new ArrayList ();
993 MethodAttributes mattrs;
996 foreach (PropertyInfo c in properties) {
998 accessor = c.GetGetMethod (true);
999 if (accessor == null)
1000 accessor = c.GetSetMethod (true);
1001 if (accessor == null)
1003 mattrs = accessor.Attributes;
1004 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1005 if ((bindingAttr & BindingFlags.Public) != 0)
1008 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1014 if ((mattrs & MethodAttributes.Static) != 0) {
1015 if ((bindingAttr & BindingFlags.Static) != 0)
1018 if ((bindingAttr & BindingFlags.Instance) != 0)
1025 PropertyInfo[] result = new PropertyInfo [l.Count];
1030 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1031 throw not_supported ();
1034 protected override bool HasElementTypeImpl () {
1035 // According to the MSDN docs, this is supported for TypeBuilders,
1036 // but in reality, it is not
1037 throw not_supported ();
1038 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1041 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1042 throw not_supported ();
1045 protected override bool IsArrayImpl ()
1047 return Type.IsArrayImpl (this);
1050 protected override bool IsByRefImpl () {
1054 protected override bool IsCOMObjectImpl () {
1057 protected override bool IsPointerImpl () {
1061 protected override bool IsPrimitiveImpl () {
1065 protected override bool IsValueTypeImpl () {
1066 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1067 this != pmodule.assemblyb.corlib_value_type &&
1068 this != pmodule.assemblyb.corlib_enum_type);
1071 public override RuntimeTypeHandle TypeHandle {
1073 throw not_supported ();
1077 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1078 if (customBuilder == null)
1079 throw new ArgumentNullException ("customBuilder");
1081 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1082 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1083 byte[] data = customBuilder.Data;
1084 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1085 layout_kind = (int)data [2];
1086 layout_kind |= ((int)data [3]) << 8;
1087 attrs &= ~TypeAttributes.LayoutMask;
1088 switch ((LayoutKind)layout_kind) {
1089 case LayoutKind.Auto:
1090 attrs |= TypeAttributes.AutoLayout;
1092 case LayoutKind.Explicit:
1093 attrs |= TypeAttributes.ExplicitLayout;
1095 case LayoutKind.Sequential:
1096 attrs |= TypeAttributes.SequentialLayout;
1099 // we should ignore it since it can be any value anyway...
1100 throw new Exception ("Error in customattr");
1102 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1104 if (first_type_name == "System.Int16")
1106 int nnamed = (int)data [pos++];
1107 nnamed |= ((int)data [pos++]) << 8;
1108 for (int i = 0; i < nnamed; ++i) {
1109 byte named_type = data [pos++];
1110 byte type = data [pos++];
1115 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1116 string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1118 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1119 // See related code/FIXME in mono/mono/metadata/reflection.c
1122 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1123 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1125 /* all the fields are integers in StructLayout */
1126 int value = (int)data [pos++];
1127 value |= ((int)data [pos++]) << 8;
1128 value |= ((int)data [pos++]) << 16;
1129 value |= ((int)data [pos++]) << 24;
1130 switch (named_name) {
1132 switch ((CharSet)value) {
1136 case CharSet.Unicode:
1137 attrs |= TypeAttributes.UnicodeClass;
1140 attrs |= TypeAttributes.AutoClass;
1143 break; // error out...
1147 packing_size = (PackingSize)value;
1153 break; // error out...
1157 } else if (attrname == "System.SerializableAttribute") {
1158 attrs |= TypeAttributes.Serializable;
1161 if (cattrs != null) {
1162 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1163 cattrs.CopyTo (new_array, 0);
1164 new_array [cattrs.Length] = customBuilder;
1167 cattrs = new CustomAttributeBuilder [1];
1168 cattrs [0] = customBuilder;
1171 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1172 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1175 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1176 check_name ("name", name);
1177 if (eventtype == null)
1178 throw new ArgumentNullException ("eventtype");
1180 throw not_after_created ();
1182 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1183 if (events != null) {
1184 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1185 System.Array.Copy (events, new_events, events.Length);
1186 new_events [events.Length] = res;
1187 events = new_events;
1189 events = new EventBuilder [1];
1195 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1197 throw new ArgumentNullException ("data");
1198 if ((data.Length == 0) || (data.Length > 0x3f0000))
1199 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1201 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1202 res.SetRVAData (data);
1207 static int UnmanagedDataCount = 0;
1209 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1210 check_name ("name", name);
1211 if ((size <= 0) || (size > 0x3f0000))
1212 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1214 throw not_after_created ();
1216 string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1217 UnmanagedDataCount++;
1218 TypeBuilder datablobtype = DefineNestedType (s,
1219 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1220 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1221 datablobtype.CreateType ();
1222 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1225 public TypeToken TypeToken {
1227 return new TypeToken (0x02000000 | table_idx);
1230 public void SetParent (Type parentType) {
1231 if (parentType == null)
1232 throw new ArgumentNullException ("parentType");
1234 throw not_after_created ();
1236 parent = parentType;
1238 internal int get_next_table_index (object obj, int table, bool inc) {
1239 return pmodule.get_next_table_index (obj, table, inc);
1242 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1244 if (created == null)
1245 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1247 return created.GetInterfaceMap (interfaceType);
1250 internal bool is_created {
1252 return created != null;
1256 private Exception not_supported ()
1258 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1261 private Exception not_after_created ()
1263 return new InvalidOperationException ("Unable to change after type has been created.");
1266 private void check_name (string argName, string name)
1269 throw new ArgumentNullException (argName);
1271 throw new ArgumentException (argName, "Empty name is not legal.");
1272 if (name.IndexOf ((char)0) != -1)
1273 throw new ArgumentException (argName, "Illegal name.");
1276 #if NET_2_0 || BOOTSTRAP_NET_2_0
1277 public override Type[] GetGenericArguments ()
1279 if (generic_params != null)
1280 return generic_params;
1282 throw new InvalidOperationException ();
1285 public override Type GetGenericTypeDefinition ()
1287 setup_generic_class (this);
1289 return base.GetGenericTypeDefinition ();
1292 public override bool HasGenericArguments {
1294 throw new NotImplementedException ();
1298 public override bool ContainsGenericParameters {
1300 return generic_params != null;
1304 public extern override bool IsGenericParameter {
1305 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1309 public override int GenericParameterPosition {
1311 throw new NotImplementedException ();
1315 public override MethodInfo DeclaringMethod {
1317 throw new NotImplementedException ();
1321 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
1323 generic_params = new GenericTypeParameterBuilder [names.Length];
1324 for (int i = 0; i < names.Length; i++)
1325 generic_params [i] = new GenericTypeParameterBuilder (
1326 this, null, names [i], i);
1328 return generic_params;
1331 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1333 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);