2 // System.Reflection.Emit/TypeBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Globalization;
40 using System.Collections;
41 using System.Security;
42 using System.Security.Permissions;
44 namespace System.Reflection.Emit {
46 public sealed class TypeBuilder : Type {
47 #region Sync with reflection.h
49 private string nspace;
51 private Type nesting_type;
52 private Type[] interfaces;
53 private int num_methods;
54 private MethodBuilder[] methods;
55 private ConstructorBuilder[] ctors;
56 private PropertyBuilder[] properties;
57 private int num_fields;
58 private FieldBuilder[] fields;
59 private EventBuilder[] events;
60 private CustomAttributeBuilder[] cattrs;
61 internal TypeBuilder[] subtypes;
62 internal TypeAttributes attrs;
63 private int table_idx;
64 private ModuleBuilder pmodule;
65 private int class_size;
66 private PackingSize packing_size;
67 #if NET_2_0 || BOOTSTRAP_NET_2_0
68 private GenericTypeParameterBuilder[] generic_params;
70 private Object generic_params; /* so offsets don't change */
72 private RefEmitPermissionSet[] permissions;
77 public const int UnspecifiedTypeSize = 0;
79 protected override TypeAttributes GetAttributeFlagsImpl () {
83 [MethodImplAttribute(MethodImplOptions.InternalCall)]
84 private extern void setup_internal_class (TypeBuilder tb);
86 [MethodImplAttribute(MethodImplOptions.InternalCall)]
87 private extern void create_internal_class (TypeBuilder tb);
89 [MethodImplAttribute(MethodImplOptions.InternalCall)]
90 private extern void setup_generic_class (TypeBuilder tb);
92 [MethodImplAttribute(MethodImplOptions.InternalCall)]
93 private extern EventInfo get_event_info (EventBuilder eb);
95 internal TypeBuilder (ModuleBuilder mb, TypeAttributes attr) {
98 this.class_size = UnspecifiedTypeSize;
100 fullname = this.tname = "<Module>";
103 setup_internal_class (this);
106 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
108 this.parent = parent;
110 this.class_size = type_size;
111 this.packing_size = packing_size;
112 this.nesting_type = nesting_type;
113 sep_index = name.LastIndexOf('.');
114 if (sep_index != -1) {
115 this.tname = name.Substring (sep_index + 1);
116 this.nspace = name.Substring (0, sep_index);
121 if (interfaces != null) {
122 this.interfaces = new Type[interfaces.Length];
123 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
127 table_idx = mb.get_next_table_index (this, 0x02, true);
128 setup_internal_class (this);
129 fullname = GetFullName ();
132 public override Assembly Assembly {
133 get {return pmodule.Assembly;}
136 public override string AssemblyQualifiedName {
138 return fullname + ", " + Assembly.GetName().FullName;
141 public override Type BaseType {
146 public override Type DeclaringType {get {return nesting_type;}}
148 /* public override bool IsSubclassOf (Type c)
165 public override Type UnderlyingSystemType {
167 // This should return the type itself for non-enum types but
169 if (fields != null) {
170 foreach (FieldBuilder f in fields) {
171 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
175 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
179 string GetFullName () {
180 if (nesting_type != null)
181 return String.Concat (nesting_type.FullName, "+", tname);
182 if ((nspace != null) && (nspace.Length > 0))
183 return String.Concat (nspace, ".", tname);
187 public override string FullName {
193 public override Guid GUID {
195 throw not_supported ();
199 public override Module Module {
200 get {return pmodule;}
202 public override string Name {
205 public override string Namespace {
208 public PackingSize PackingSize {
209 get {return packing_size;}
212 get { return class_size; }
214 public override Type ReflectedType {get {return nesting_type;}}
216 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
218 throw new ArgumentNullException ("pset");
219 if ((action == SecurityAction.RequestMinimum) ||
220 (action == SecurityAction.RequestOptional) ||
221 (action == SecurityAction.RequestRefuse))
222 throw new ArgumentException ("Request* values are not permitted", "action");
225 throw not_after_created ();
227 if (permissions != null) {
228 /* Check duplicate actions */
229 foreach (RefEmitPermissionSet set in permissions)
230 if (set.action == action)
231 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
233 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
234 permissions.CopyTo (new_array, 0);
235 permissions = new_array;
238 permissions = new RefEmitPermissionSet [1];
240 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
241 attrs |= TypeAttributes.HasSecurity;
244 public void AddInterfaceImplementation( Type interfaceType) {
245 if (interfaceType == null)
246 throw new ArgumentNullException ("interfaceType");
248 throw not_after_created ();
250 if (interfaces != null) {
251 // Check for duplicates
252 foreach (Type t in interfaces)
253 if (t == interfaceType)
256 Type[] ifnew = new Type [interfaces.Length + 1];
257 interfaces.CopyTo (ifnew, 0);
258 ifnew [interfaces.Length] = interfaceType;
261 interfaces = new Type [1];
262 interfaces [0] = interfaceType;
267 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
268 CallingConventions callConvention, Type[] types,
269 ParameterModifier[] modifiers)
274 ConstructorBuilder found = null;
277 foreach (ConstructorBuilder cb in ctors){
278 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
288 throw new AmbiguousMatchException ();
291 MethodBase[] match = new MethodBase [count];
296 foreach (ConstructorInfo m in ctors) {
297 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
303 binder = Binder.DefaultBinder;
304 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
307 public override bool IsDefined( Type attributeType, bool inherit)
310 * MS throws NotSupported here, but we can't because some corlib
311 * classes make calls to IsDefined.
313 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
316 public override object[] GetCustomAttributes(bool inherit)
318 throw not_supported ();
321 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
323 throw not_supported ();
326 public TypeBuilder DefineNestedType (string name) {
327 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
330 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
331 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
334 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
335 return DefineNestedType (name, attr, parent, null);
338 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
339 PackingSize packsize, int typesize)
341 check_name ("name", name);
342 // Visibility must be NestedXXX
344 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
345 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
346 throw new ArgumentException ("attr", "Bad type flags for nested type.");
348 if (interfaces != null)
349 foreach (Type iface in interfaces)
351 throw new ArgumentNullException ("interfaces");
353 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
354 res.fullname = res.GetFullName ();
355 pmodule.RegisterTypeName (res, res.fullname);
356 if (subtypes != null) {
357 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
358 System.Array.Copy (subtypes, new_types, subtypes.Length);
359 new_types [subtypes.Length] = res;
360 subtypes = new_types;
362 subtypes = new TypeBuilder [1];
368 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
369 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
372 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
373 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
376 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
377 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
380 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
381 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
384 #if NET_2_0 || BOOTSTRAP_NET_2_0
389 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
392 throw not_after_created ();
393 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
395 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
396 System.Array.Copy (ctors, new_ctors, ctors.Length);
397 new_ctors [ctors.Length] = cb;
400 ctors = new ConstructorBuilder [1];
406 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
408 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
413 parent_type = parent;
415 parent_type = pmodule.assemblyb.corlib_object_type;
417 ConstructorInfo parent_constructor =
418 parent_type.GetConstructor (
419 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
420 null, Type.EmptyTypes, null);
422 ILGenerator ig = cb.GetILGenerator ();
423 if (parent_constructor != null){
424 ig.Emit (OpCodes.Ldarg_0);
425 ig.Emit (OpCodes.Call, parent_constructor);
427 ig.Emit (OpCodes.Ret);
431 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
432 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
435 private void append_method (MethodBuilder mb) {
436 if (methods != null) {
437 if (methods.Length == num_methods) {
438 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
439 System.Array.Copy (methods, new_methods, num_methods);
440 methods = new_methods;
443 methods = new MethodBuilder [1];
445 methods [num_methods] = mb;
449 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
450 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
453 #if NET_2_0 || BOOTSTRAP_NET_2_0
458 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
459 check_name ("name", name);
461 throw not_after_created ();
463 !((attributes & MethodAttributes.Abstract) != 0) ||
464 !((attributes & MethodAttributes.Virtual) != 0)))
465 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
467 if (returnType == null)
468 returnType = pmodule.assemblyb.corlib_void_type;
469 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
474 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
475 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
478 #if NET_2_0 || BOOTSTRAP_NET_2_0
483 MethodBuilder DefinePInvokeMethod (
486 string entryName, MethodAttributes attributes,
487 CallingConventions callingConvention,
489 Type[] returnTypeRequiredCustomModifiers,
490 Type[] returnTypeOptionalCustomModifiers,
491 Type[] parameterTypes,
492 Type[][] parameterTypeRequiredCustomModifiers,
493 Type[][] parameterTypeOptionalCustomModifiers,
494 CallingConvention nativeCallConv,
495 CharSet nativeCharSet) {
496 check_name ("name", name);
497 check_name ("dllName", dllName);
498 check_name ("entryName", entryName);
499 if ((attributes & MethodAttributes.Abstract) != 0)
500 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
502 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
504 throw not_after_created ();
507 = new MethodBuilder (
513 returnTypeRequiredCustomModifiers,
514 returnTypeOptionalCustomModifiers,
516 parameterTypeRequiredCustomModifiers,
517 parameterTypeOptionalCustomModifiers,
526 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
527 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
528 nativeCallConv, nativeCharSet);
531 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
532 if (methodInfoBody == null)
533 throw new ArgumentNullException ("methodInfoBody");
534 if (methodInfoDeclaration == null)
535 throw new ArgumentNullException ("methodInfoDeclaration");
537 throw not_after_created ();
539 if (methodInfoBody is MethodBuilder) {
540 MethodBuilder mb = (MethodBuilder)methodInfoBody;
541 mb.set_override (methodInfoDeclaration);
545 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
546 return DefineField (fieldName, type, null, null, attributes);
549 #if NET_2_0 || BOOTSTRAP_NET_2_0
554 FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
555 check_name ("fieldName", fieldName);
556 if (type == typeof (void))
557 throw new ArgumentException ("type", "Bad field type in defining field.");
559 throw not_after_created ();
561 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
562 if (fields != null) {
563 if (fields.Length == num_fields) {
564 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
565 System.Array.Copy (fields, new_fields, num_fields);
568 fields [num_fields] = res;
571 fields = new FieldBuilder [1];
574 create_internal_class (this);
579 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
580 check_name ("name", name);
581 if (parameterTypes != null)
582 foreach (Type param in parameterTypes)
584 throw new ArgumentNullException ("parameterTypes");
586 throw not_after_created ();
588 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
590 if (properties != null) {
591 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
592 System.Array.Copy (properties, new_properties, properties.Length);
593 new_properties [properties.Length] = res;
594 properties = new_properties;
596 properties = new PropertyBuilder [1];
597 properties [0] = res;
602 public ConstructorBuilder DefineTypeInitializer() {
603 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
606 [MethodImplAttribute(MethodImplOptions.InternalCall)]
607 private extern Type create_runtime_class (TypeBuilder tb);
609 private bool is_nested_in (Type t) {
619 public Type CreateType() {
620 /* handle nesting_type */
622 throw not_after_created ();
624 // Fire TypeResolve events for fields whose type is an unfinished
626 if (fields != null) {
627 foreach (FieldBuilder fb in fields) {
630 Type ft = fb.FieldType;
631 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
632 TypeBuilder tb = (TypeBuilder)ft;
633 if (!tb.is_created) {
634 AppDomain.CurrentDomain.DoTypeResolve (tb);
635 if (!tb.is_created) {
636 // FIXME: We should throw an exception here,
637 // but mcs expects that the type is created
638 // even if the exception is thrown
639 //throw new TypeLoadException ("Could not load type " + tb);
646 if (methods != null) {
647 for (int i = 0; i < num_methods; ++i)
648 ((MethodBuilder)(methods[i])).fixup ();
652 // On classes, define a default constructor if not provided
654 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>") &&
655 (GetAttributeFlagsImpl () & TypeAttributes.Abstract | TypeAttributes.Sealed) != (TypeAttributes.Abstract | TypeAttributes.Sealed))
656 DefineDefaultConstructor (MethodAttributes.Public);
659 foreach (ConstructorBuilder ctor in ctors)
663 created = create_runtime_class (this);
669 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
672 return new ConstructorInfo [0];
673 ArrayList l = new ArrayList ();
675 MethodAttributes mattrs;
677 foreach (ConstructorBuilder c in ctors) {
679 mattrs = c.Attributes;
680 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
681 if ((bindingAttr & BindingFlags.Public) != 0)
684 if ((bindingAttr & BindingFlags.NonPublic) != 0)
690 if ((mattrs & MethodAttributes.Static) != 0) {
691 if ((bindingAttr & BindingFlags.Static) != 0)
694 if ((bindingAttr & BindingFlags.Instance) != 0)
701 ConstructorInfo[] result = new ConstructorInfo [l.Count];
706 public override Type GetElementType () {
707 throw not_supported ();
710 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
711 throw not_supported ();
714 /* Needed to keep signature compatibility with MS.NET */
715 public override EventInfo[] GetEvents ()
717 return GetEvents (DefaultBindingFlags);
720 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
721 // FIXME: Under MS.NET, this throws a NotImplementedException
722 // But mcs calls this method. How can that be?
723 return new EventInfo [0];
726 // This is only used from MonoGenericInst.initialize().
727 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
730 return new EventInfo [0];
731 ArrayList l = new ArrayList ();
733 MethodAttributes mattrs;
736 foreach (EventBuilder eb in events) {
739 EventInfo c = get_event_info (eb);
741 accessor = c.GetAddMethod (true);
742 if (accessor == null)
743 accessor = c.GetRemoveMethod (true);
744 if (accessor == null)
746 mattrs = accessor.Attributes;
747 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
748 if ((bindingAttr & BindingFlags.Public) != 0)
751 if ((bindingAttr & BindingFlags.NonPublic) != 0)
757 if ((mattrs & MethodAttributes.Static) != 0) {
758 if ((bindingAttr & BindingFlags.Static) != 0)
761 if ((bindingAttr & BindingFlags.Instance) != 0)
768 EventInfo[] result = new EventInfo [l.Count];
773 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
778 FieldAttributes mattrs;
780 foreach (FieldInfo c in fields) {
786 mattrs = c.Attributes;
787 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
788 if ((bindingAttr & BindingFlags.Public) != 0)
791 if ((bindingAttr & BindingFlags.NonPublic) != 0)
797 if ((mattrs & FieldAttributes.Static) != 0) {
798 if ((bindingAttr & BindingFlags.Static) != 0)
801 if ((bindingAttr & BindingFlags.Instance) != 0)
811 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
813 return new FieldInfo [0];
814 ArrayList l = new ArrayList ();
816 FieldAttributes mattrs;
818 foreach (FieldInfo c in fields) {
822 mattrs = c.Attributes;
823 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
824 if ((bindingAttr & BindingFlags.Public) != 0)
827 if ((bindingAttr & BindingFlags.NonPublic) != 0)
833 if ((mattrs & FieldAttributes.Static) != 0) {
834 if ((bindingAttr & BindingFlags.Static) != 0)
837 if ((bindingAttr & BindingFlags.Instance) != 0)
844 FieldInfo[] result = new FieldInfo [l.Count];
849 public override Type GetInterface (string name, bool ignoreCase) {
850 throw not_supported ();
853 public override Type[] GetInterfaces () {
854 if (interfaces != null) {
855 Type[] ret = new Type [interfaces.Length];
856 interfaces.CopyTo (ret, 0);
859 return Type.EmptyTypes;
863 public override MemberInfo[] GetMember (string name, MemberTypes type,
864 BindingFlags bindingAttr) {
865 throw not_supported ();
868 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
869 throw not_supported ();
872 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
873 MethodInfo[] candidates;
874 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
875 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
877 candidates = parent_methods;
879 candidates = new MethodInfo [methods.Length + parent_methods.Length];
880 parent_methods.CopyTo (candidates, 0);
881 methods.CopyTo (candidates, parent_methods.Length);
885 candidates = methods;
887 if (candidates == null)
888 return new MethodInfo [0];
890 ArrayList l = new ArrayList ();
892 MethodAttributes mattrs;
894 foreach (MethodInfo c in candidates) {
898 if (String.Compare (c.Name, name, ignoreCase) != 0)
902 mattrs = c.Attributes;
903 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
904 if ((bindingAttr & BindingFlags.Public) != 0)
907 if ((bindingAttr & BindingFlags.NonPublic) != 0)
913 if ((mattrs & MethodAttributes.Static) != 0) {
914 if ((bindingAttr & BindingFlags.Static) != 0)
917 if ((bindingAttr & BindingFlags.Instance) != 0)
925 MethodInfo[] result = new MethodInfo [l.Count];
930 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
931 return GetMethodsByName (null, bindingAttr, false, this);
934 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
936 CallingConventions callConvention,
937 Type[] types, ParameterModifier[] modifiers)
940 /* MS.Net throws this exception if the type is unfinished... */
941 throw not_supported ();
943 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
944 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
945 MethodInfo found = null;
947 int typesLen = (types != null) ? types.Length : 0;
950 foreach (MethodInfo m in methods) {
951 // Under MS.NET, Standard|HasThis matches Standard...
952 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
961 if (count == 1 && typesLen == 0)
964 match = new MethodBase [count];
969 foreach (MethodInfo m in methods) {
970 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
977 return (MethodInfo) Binder.FindMostDerivedMatch (match);
980 binder = Binder.DefaultBinder;
982 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
985 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
986 throw not_supported ();
989 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
991 ArrayList result = new ArrayList ();
993 if (subtypes == null)
994 return Type.EmptyTypes;
995 foreach (TypeBuilder t in subtypes) {
997 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
998 if ((bindingAttr & BindingFlags.Public) != 0)
1001 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1008 Type[] r = new Type [result.Count];
1013 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
1014 if (properties == null)
1015 return new PropertyInfo [0];
1016 ArrayList l = new ArrayList ();
1018 MethodAttributes mattrs;
1019 MethodInfo accessor;
1021 foreach (PropertyInfo c in properties) {
1023 accessor = c.GetGetMethod (true);
1024 if (accessor == null)
1025 accessor = c.GetSetMethod (true);
1026 if (accessor == null)
1028 mattrs = accessor.Attributes;
1029 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1030 if ((bindingAttr & BindingFlags.Public) != 0)
1033 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1039 if ((mattrs & MethodAttributes.Static) != 0) {
1040 if ((bindingAttr & BindingFlags.Static) != 0)
1043 if ((bindingAttr & BindingFlags.Instance) != 0)
1050 PropertyInfo[] result = new PropertyInfo [l.Count];
1055 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1056 throw not_supported ();
1059 protected override bool HasElementTypeImpl () {
1060 // According to the MSDN docs, this is supported for TypeBuilders,
1061 // but in reality, it is not
1062 throw not_supported ();
1063 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1066 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1067 throw not_supported ();
1070 protected override bool IsArrayImpl ()
1072 return Type.IsArrayImpl (this);
1075 protected override bool IsByRefImpl () {
1079 protected override bool IsCOMObjectImpl () {
1082 protected override bool IsPointerImpl () {
1086 protected override bool IsPrimitiveImpl () {
1090 protected override bool IsValueTypeImpl () {
1091 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1092 this != pmodule.assemblyb.corlib_value_type &&
1093 this != pmodule.assemblyb.corlib_enum_type);
1096 public override RuntimeTypeHandle TypeHandle {
1098 throw not_supported ();
1102 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1103 if (customBuilder == null)
1104 throw new ArgumentNullException ("customBuilder");
1106 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1107 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1108 byte[] data = customBuilder.Data;
1109 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1110 layout_kind = (int)data [2];
1111 layout_kind |= ((int)data [3]) << 8;
1112 attrs &= ~TypeAttributes.LayoutMask;
1113 switch ((LayoutKind)layout_kind) {
1114 case LayoutKind.Auto:
1115 attrs |= TypeAttributes.AutoLayout;
1117 case LayoutKind.Explicit:
1118 attrs |= TypeAttributes.ExplicitLayout;
1120 case LayoutKind.Sequential:
1121 attrs |= TypeAttributes.SequentialLayout;
1124 // we should ignore it since it can be any value anyway...
1125 throw new Exception ("Error in customattr");
1127 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1129 if (first_type_name == "System.Int16")
1131 int nnamed = (int)data [pos++];
1132 nnamed |= ((int)data [pos++]) << 8;
1133 for (int i = 0; i < nnamed; ++i) {
1134 byte named_type = data [pos++];
1135 byte type = data [pos++];
1140 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1141 string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1143 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1144 // See related code/FIXME in mono/mono/metadata/reflection.c
1147 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1148 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1150 /* all the fields are integers in StructLayout */
1151 int value = (int)data [pos++];
1152 value |= ((int)data [pos++]) << 8;
1153 value |= ((int)data [pos++]) << 16;
1154 value |= ((int)data [pos++]) << 24;
1155 switch (named_name) {
1157 switch ((CharSet)value) {
1161 case CharSet.Unicode:
1162 attrs |= TypeAttributes.UnicodeClass;
1165 attrs |= TypeAttributes.AutoClass;
1168 break; // error out...
1172 packing_size = (PackingSize)value;
1178 break; // error out...
1182 } else if (attrname == "System.SerializableAttribute") {
1183 attrs |= TypeAttributes.Serializable;
1186 if (cattrs != null) {
1187 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1188 cattrs.CopyTo (new_array, 0);
1189 new_array [cattrs.Length] = customBuilder;
1192 cattrs = new CustomAttributeBuilder [1];
1193 cattrs [0] = customBuilder;
1196 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1197 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1200 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1201 check_name ("name", name);
1202 if (eventtype == null)
1203 throw new ArgumentNullException ("eventtype");
1205 throw not_after_created ();
1207 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1208 if (events != null) {
1209 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1210 System.Array.Copy (events, new_events, events.Length);
1211 new_events [events.Length] = res;
1212 events = new_events;
1214 events = new EventBuilder [1];
1220 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1222 throw new ArgumentNullException ("data");
1223 if ((data.Length == 0) || (data.Length > 0x3f0000))
1224 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1226 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1227 res.SetRVAData (data);
1232 static int UnmanagedDataCount = 0;
1234 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1235 check_name ("name", name);
1236 if ((size <= 0) || (size > 0x3f0000))
1237 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1239 throw not_after_created ();
1241 string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1242 UnmanagedDataCount++;
1243 TypeBuilder datablobtype = DefineNestedType (s,
1244 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1245 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1246 datablobtype.CreateType ();
1247 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1250 public TypeToken TypeToken {
1252 return new TypeToken (0x02000000 | table_idx);
1255 public void SetParent (Type parentType) {
1256 if (parentType == null)
1257 throw new ArgumentNullException ("parentType");
1259 throw not_after_created ();
1261 parent = parentType;
1262 // will just set the parent-related bits if called a second time
1263 setup_internal_class (this);
1265 internal int get_next_table_index (object obj, int table, bool inc) {
1266 return pmodule.get_next_table_index (obj, table, inc);
1269 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1271 if (created == null)
1272 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1274 return created.GetInterfaceMap (interfaceType);
1277 internal bool is_created {
1279 return created != null;
1283 private Exception not_supported ()
1285 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1288 private Exception not_after_created ()
1290 return new InvalidOperationException ("Unable to change after type has been created.");
1293 private void check_name (string argName, string name)
1296 throw new ArgumentNullException (argName);
1298 throw new ArgumentException (argName, "Empty name is not legal.");
1299 if (name.IndexOf ((char)0) != -1)
1300 throw new ArgumentException (argName, "Illegal name.");
1303 public override String ToString ()
1309 public override bool IsAssignableFrom (Type c)
1311 return base.IsAssignableFrom (c);
1315 public override bool IsSubclassOf (Type c)
1317 return base.IsSubclassOf (c);
1320 #if NET_2_0 || BOOTSTRAP_NET_2_0
1321 public bool IsCreated () {
1325 public override Type[] GetGenericArguments ()
1327 if (generic_params != null)
1328 return generic_params;
1330 throw new InvalidOperationException ();
1333 public override Type GetGenericTypeDefinition ()
1335 setup_generic_class (this);
1337 return base.GetGenericTypeDefinition ();
1340 public override bool HasGenericArguments {
1342 throw new NotImplementedException ();
1346 public override bool ContainsGenericParameters {
1348 return generic_params != null;
1352 public extern override bool IsGenericParameter {
1353 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1357 public override int GenericParameterPosition {
1359 throw new NotImplementedException ();
1363 public override MethodInfo DeclaringMethod {
1365 throw new NotImplementedException ();
1369 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
1371 generic_params = new GenericTypeParameterBuilder [names.Length];
1372 for (int i = 0; i < names.Length; i++)
1373 generic_params [i] = new GenericTypeParameterBuilder (
1374 this, null, names [i], i);
1376 return generic_params;
1379 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1381 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);