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) {
99 fullname = this.tname = "<Module>";
102 setup_internal_class (this);
105 internal TypeBuilder (ModuleBuilder mb, string name, TypeAttributes attr, Type parent, Type[] interfaces, PackingSize packing_size, int type_size, Type nesting_type) {
107 this.parent = parent;
109 this.class_size = type_size;
110 this.packing_size = packing_size;
111 this.nesting_type = nesting_type;
112 sep_index = name.LastIndexOf('.');
113 if (sep_index != -1) {
114 this.tname = name.Substring (sep_index + 1);
115 this.nspace = name.Substring (0, sep_index);
120 if (interfaces != null) {
121 this.interfaces = new Type[interfaces.Length];
122 System.Array.Copy (interfaces, this.interfaces, interfaces.Length);
126 table_idx = mb.get_next_table_index (this, 0x02, true);
127 setup_internal_class (this);
128 fullname = GetFullName ();
131 public override Assembly Assembly {
132 get {return pmodule.Assembly;}
135 public override string AssemblyQualifiedName {
137 return fullname + ", " + Assembly.GetName().FullName;
140 public override Type BaseType {
145 public override Type DeclaringType {get {return nesting_type;}}
147 /* public override bool IsSubclassOf (Type c)
164 public override Type UnderlyingSystemType {
166 // This should return the type itself for non-enum types but
168 if (fields != null) {
169 foreach (FieldBuilder f in fields) {
170 if ((f != null) && (f.Attributes & FieldAttributes.Static) == 0)
174 throw new InvalidOperationException ("Underlying type information on enumeration is not specified.");
178 string GetFullName () {
179 if (nesting_type != null)
180 return String.Concat (nesting_type.FullName, "+", tname);
181 if ((nspace != null) && (nspace.Length > 0))
182 return String.Concat (nspace, ".", tname);
186 public override string FullName {
192 public override Guid GUID {
194 throw not_supported ();
198 public override Module Module {
199 get {return pmodule;}
201 public override string Name {
204 public override string Namespace {
207 public PackingSize PackingSize {
208 get {return packing_size;}
211 get { return class_size; }
213 public override Type ReflectedType {get {return nesting_type;}}
215 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
217 throw new ArgumentNullException ("pset");
218 if ((action == SecurityAction.RequestMinimum) ||
219 (action == SecurityAction.RequestOptional) ||
220 (action == SecurityAction.RequestRefuse))
221 throw new ArgumentException ("Request* values are not permitted", "action");
224 throw not_after_created ();
226 if (permissions != null) {
227 /* Check duplicate actions */
228 foreach (RefEmitPermissionSet set in permissions)
229 if (set.action == action)
230 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
232 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
233 permissions.CopyTo (new_array, 0);
234 permissions = new_array;
237 permissions = new RefEmitPermissionSet [1];
239 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
240 attrs |= TypeAttributes.HasSecurity;
243 public void AddInterfaceImplementation( Type interfaceType) {
244 if (interfaceType == null)
245 throw new ArgumentNullException ("interfaceType");
247 throw not_after_created ();
249 if (interfaces != null) {
250 // Check for duplicates
251 foreach (Type t in interfaces)
252 if (t == interfaceType)
255 Type[] ifnew = new Type [interfaces.Length + 1];
256 interfaces.CopyTo (ifnew, 0);
257 ifnew [interfaces.Length] = interfaceType;
260 interfaces = new Type [1];
261 interfaces [0] = interfaceType;
266 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder,
267 CallingConventions callConvention, Type[] types,
268 ParameterModifier[] modifiers)
273 ConstructorBuilder found = null;
276 foreach (ConstructorBuilder cb in ctors){
277 if (callConvention != CallingConventions.Any && cb.CallingConvention != callConvention)
287 throw new AmbiguousMatchException ();
290 MethodBase[] match = new MethodBase [count];
295 foreach (ConstructorInfo m in ctors) {
296 if (callConvention != CallingConventions.Any && m.CallingConvention != callConvention)
302 binder = Binder.DefaultBinder;
303 return (ConstructorInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
306 public override bool IsDefined( Type attributeType, bool inherit)
309 * MS throws NotSupported here, but we can't because some corlib
310 * classes make calls to IsDefined.
312 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
315 public override object[] GetCustomAttributes(bool inherit)
317 throw not_supported ();
320 public override object[] GetCustomAttributes(Type attributeType, bool inherit)
322 throw not_supported ();
325 public TypeBuilder DefineNestedType (string name) {
326 return DefineNestedType (name, TypeAttributes.NestedPrivate, pmodule.assemblyb.corlib_object_type, null);
329 public TypeBuilder DefineNestedType (string name, TypeAttributes attr) {
330 return DefineNestedType (name, attr, pmodule.assemblyb.corlib_object_type, null);
333 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent) {
334 return DefineNestedType (name, attr, parent, null);
337 private TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces,
338 PackingSize packsize, int typesize)
340 check_name ("name", name);
341 // Visibility must be NestedXXX
343 if (((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.Public) ||
344 ((attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic))
345 throw new ArgumentException ("attr", "Bad type flags for nested type.");
347 if (interfaces != null)
348 foreach (Type iface in interfaces)
350 throw new ArgumentNullException ("interfaces");
352 TypeBuilder res = new TypeBuilder (pmodule, name, attr, parent, interfaces, packsize, typesize, this);
353 res.fullname = res.GetFullName ();
354 pmodule.RegisterTypeName (res, res.fullname);
355 if (subtypes != null) {
356 TypeBuilder[] new_types = new TypeBuilder [subtypes.Length + 1];
357 System.Array.Copy (subtypes, new_types, subtypes.Length);
358 new_types [subtypes.Length] = res;
359 subtypes = new_types;
361 subtypes = new TypeBuilder [1];
367 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
368 return DefineNestedType (name, attr, parent, interfaces, PackingSize.Unspecified, UnspecifiedTypeSize);
371 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, int typesize) {
372 return DefineNestedType (name, attr, parent, null, PackingSize.Unspecified, typesize);
375 public TypeBuilder DefineNestedType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
376 return DefineNestedType (name, attr, parent, null, packsize, UnspecifiedTypeSize);
379 public ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
380 return DefineConstructor (attributes, callingConvention, parameterTypes, null, null);
383 #if NET_2_0 || BOOTSTRAP_NET_2_0
388 ConstructorBuilder DefineConstructor (MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
391 throw not_after_created ();
392 ConstructorBuilder cb = new ConstructorBuilder (this, attributes, callingConvention, parameterTypes, requiredCustomModifiers, optionalCustomModifiers);
394 ConstructorBuilder[] new_ctors = new ConstructorBuilder [ctors.Length+1];
395 System.Array.Copy (ctors, new_ctors, ctors.Length);
396 new_ctors [ctors.Length] = cb;
399 ctors = new ConstructorBuilder [1];
405 public ConstructorBuilder DefineDefaultConstructor (MethodAttributes attributes)
407 ConstructorBuilder cb = DefineConstructor (attributes, CallingConventions.Standard, new Type [0]);
412 parent_type = parent;
414 parent_type = pmodule.assemblyb.corlib_object_type;
416 ConstructorInfo parent_constructor =
417 parent_type.GetConstructor (
418 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
419 null, Type.EmptyTypes, null);
421 ILGenerator ig = cb.GetILGenerator ();
422 if (parent_constructor != null){
423 ig.Emit (OpCodes.Ldarg_0);
424 ig.Emit (OpCodes.Call, parent_constructor);
426 ig.Emit (OpCodes.Ret);
430 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, Type returnType, Type[] parameterTypes) {
431 return DefineMethod (name, attributes, CallingConventions.Standard, returnType, parameterTypes);
434 private void append_method (MethodBuilder mb) {
435 if (methods != null) {
436 if (methods.Length == num_methods) {
437 MethodBuilder[] new_methods = new MethodBuilder [methods.Length * 2];
438 System.Array.Copy (methods, new_methods, num_methods);
439 methods = new_methods;
442 methods = new MethodBuilder [1];
444 methods [num_methods] = mb;
448 public MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
449 return DefineMethod (name, attributes, callingConvention, returnType, null, null, parameterTypes, null, null);
452 #if NET_2_0 || BOOTSTRAP_NET_2_0
457 MethodBuilder DefineMethod( string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers) {
458 check_name ("name", name);
460 throw not_after_created ();
462 !((attributes & MethodAttributes.Abstract) != 0) ||
463 !((attributes & MethodAttributes.Virtual) != 0)))
464 throw new ArgumentException ("attributes", "Interface method must be abstract and virtual.");
466 if (returnType == null)
467 returnType = pmodule.assemblyb.corlib_void_type;
468 MethodBuilder res = new MethodBuilder (this, name, attributes, callingConvention, returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
473 public MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
474 return DefinePInvokeMethod (name, dllName, entryName, attributes, callingConvention, returnType, null, null, parameterTypes, null, null, nativeCallConv, nativeCharSet);
477 #if NET_2_0 || BOOTSTRAP_NET_2_0
482 MethodBuilder DefinePInvokeMethod (
485 string entryName, MethodAttributes attributes,
486 CallingConventions callingConvention,
488 Type[] returnTypeRequiredCustomModifiers,
489 Type[] returnTypeOptionalCustomModifiers,
490 Type[] parameterTypes,
491 Type[][] parameterTypeRequiredCustomModifiers,
492 Type[][] parameterTypeOptionalCustomModifiers,
493 CallingConvention nativeCallConv,
494 CharSet nativeCharSet) {
495 check_name ("name", name);
496 check_name ("dllName", dllName);
497 check_name ("entryName", entryName);
498 if ((attributes & MethodAttributes.Abstract) != 0)
499 throw new ArgumentException ("attributes", "PInvoke methods must be static and native and cannot be abstract.");
501 throw new ArgumentException ("PInvoke methods cannot exist on interfaces.");
503 throw not_after_created ();
506 = new MethodBuilder (
512 returnTypeRequiredCustomModifiers,
513 returnTypeOptionalCustomModifiers,
515 parameterTypeRequiredCustomModifiers,
516 parameterTypeOptionalCustomModifiers,
525 public MethodBuilder DefinePInvokeMethod (string name, string dllName, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, CallingConvention nativeCallConv, CharSet nativeCharSet) {
526 return DefinePInvokeMethod (name, dllName, name, attributes, callingConvention, returnType, parameterTypes,
527 nativeCallConv, nativeCharSet);
530 public void DefineMethodOverride( MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) {
531 if (methodInfoBody == null)
532 throw new ArgumentNullException ("methodInfoBody");
533 if (methodInfoDeclaration == null)
534 throw new ArgumentNullException ("methodInfoDeclaration");
536 throw not_after_created ();
538 if (methodInfoBody is MethodBuilder) {
539 MethodBuilder mb = (MethodBuilder)methodInfoBody;
540 mb.set_override (methodInfoDeclaration);
544 public FieldBuilder DefineField( string fieldName, Type type, FieldAttributes attributes) {
545 return DefineField (fieldName, type, null, null, attributes);
548 #if NET_2_0 || BOOTSTRAP_NET_2_0
553 FieldBuilder DefineField( string fieldName, Type type, Type[] requiredCustomAttributes, Type[] optionalCustomAttributes, FieldAttributes attributes) {
554 check_name ("fieldName", fieldName);
555 if (type == typeof (void))
556 throw new ArgumentException ("type", "Bad field type in defining field.");
558 throw not_after_created ();
560 FieldBuilder res = new FieldBuilder (this, fieldName, type, attributes, requiredCustomAttributes, optionalCustomAttributes);
561 if (fields != null) {
562 if (fields.Length == num_fields) {
563 FieldBuilder[] new_fields = new FieldBuilder [fields.Length * 2];
564 System.Array.Copy (fields, new_fields, num_fields);
567 fields [num_fields] = res;
570 fields = new FieldBuilder [1];
573 create_internal_class (this);
578 public PropertyBuilder DefineProperty( string name, PropertyAttributes attributes, Type returnType, Type[] parameterTypes) {
579 check_name ("name", name);
580 if (parameterTypes != null)
581 foreach (Type param in parameterTypes)
583 throw new ArgumentNullException ("parameterTypes");
585 throw not_after_created ();
587 PropertyBuilder res = new PropertyBuilder (this, name, attributes, returnType, parameterTypes);
589 if (properties != null) {
590 PropertyBuilder[] new_properties = new PropertyBuilder [properties.Length+1];
591 System.Array.Copy (properties, new_properties, properties.Length);
592 new_properties [properties.Length] = res;
593 properties = new_properties;
595 properties = new PropertyBuilder [1];
596 properties [0] = res;
601 public ConstructorBuilder DefineTypeInitializer() {
602 return DefineConstructor (MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, null);
605 [MethodImplAttribute(MethodImplOptions.InternalCall)]
606 private extern Type create_runtime_class (TypeBuilder tb);
608 private bool is_nested_in (Type t) {
618 public Type CreateType() {
619 /* handle nesting_type */
621 throw not_after_created ();
623 // Fire TypeResolve events for fields whose type is an unfinished
625 if (fields != null) {
626 foreach (FieldBuilder fb in fields) {
629 Type ft = fb.FieldType;
630 if (!fb.IsStatic && (ft is TypeBuilder) && ft.IsValueType && (ft != this) && is_nested_in (ft)) {
631 TypeBuilder tb = (TypeBuilder)ft;
632 if (!tb.is_created) {
633 AppDomain.CurrentDomain.DoTypeResolve (tb);
634 if (!tb.is_created) {
635 // FIXME: We should throw an exception here,
636 // but mcs expects that the type is created
637 // even if the exception is thrown
638 //throw new TypeLoadException ("Could not load type " + tb);
645 if (methods != null) {
646 for (int i = 0; i < num_methods; ++i)
647 ((MethodBuilder)(methods[i])).fixup ();
651 // On classes, define a default constructor if not provided
653 if (!(IsInterface || IsValueType) && (ctors == null) && (tname != "<Module>"))
654 DefineDefaultConstructor (MethodAttributes.Public);
657 foreach (ConstructorBuilder ctor in ctors)
661 created = create_runtime_class (this);
667 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
670 return new ConstructorInfo [0];
671 ArrayList l = new ArrayList ();
673 MethodAttributes mattrs;
675 foreach (ConstructorBuilder c in ctors) {
677 mattrs = c.Attributes;
678 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
679 if ((bindingAttr & BindingFlags.Public) != 0)
682 if ((bindingAttr & BindingFlags.NonPublic) != 0)
688 if ((mattrs & MethodAttributes.Static) != 0) {
689 if ((bindingAttr & BindingFlags.Static) != 0)
692 if ((bindingAttr & BindingFlags.Instance) != 0)
699 ConstructorInfo[] result = new ConstructorInfo [l.Count];
704 public override Type GetElementType () {
705 throw not_supported ();
708 public override EventInfo GetEvent (string name, BindingFlags bindingAttr) {
709 throw not_supported ();
712 /* Needed to keep signature compatibility with MS.NET */
713 public override EventInfo[] GetEvents ()
715 return GetEvents (DefaultBindingFlags);
718 public override EventInfo[] GetEvents (BindingFlags bindingAttr) {
719 // FIXME: Under MS.NET, this throws a NotImplementedException
720 // But mcs calls this method. How can that be?
721 return new EventInfo [0];
724 // This is only used from MonoGenericInst.initialize().
725 internal EventInfo[] GetEvents_internal (BindingFlags bindingAttr)
728 return new EventInfo [0];
729 ArrayList l = new ArrayList ();
731 MethodAttributes mattrs;
734 foreach (EventBuilder eb in events) {
737 EventInfo c = get_event_info (eb);
739 accessor = c.GetAddMethod (true);
740 if (accessor == null)
741 accessor = c.GetRemoveMethod (true);
742 if (accessor == null)
744 mattrs = accessor.Attributes;
745 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
746 if ((bindingAttr & BindingFlags.Public) != 0)
749 if ((bindingAttr & BindingFlags.NonPublic) != 0)
755 if ((mattrs & MethodAttributes.Static) != 0) {
756 if ((bindingAttr & BindingFlags.Static) != 0)
759 if ((bindingAttr & BindingFlags.Instance) != 0)
766 EventInfo[] result = new EventInfo [l.Count];
771 public override FieldInfo GetField( string name, BindingFlags bindingAttr) {
776 FieldAttributes mattrs;
778 foreach (FieldInfo c in fields) {
784 mattrs = c.Attributes;
785 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
786 if ((bindingAttr & BindingFlags.Public) != 0)
789 if ((bindingAttr & BindingFlags.NonPublic) != 0)
795 if ((mattrs & FieldAttributes.Static) != 0) {
796 if ((bindingAttr & BindingFlags.Static) != 0)
799 if ((bindingAttr & BindingFlags.Instance) != 0)
809 public override FieldInfo[] GetFields (BindingFlags bindingAttr) {
811 return new FieldInfo [0];
812 ArrayList l = new ArrayList ();
814 FieldAttributes mattrs;
816 foreach (FieldInfo c in fields) {
820 mattrs = c.Attributes;
821 if ((mattrs & FieldAttributes.FieldAccessMask) == FieldAttributes.Public) {
822 if ((bindingAttr & BindingFlags.Public) != 0)
825 if ((bindingAttr & BindingFlags.NonPublic) != 0)
831 if ((mattrs & FieldAttributes.Static) != 0) {
832 if ((bindingAttr & BindingFlags.Static) != 0)
835 if ((bindingAttr & BindingFlags.Instance) != 0)
842 FieldInfo[] result = new FieldInfo [l.Count];
847 public override Type GetInterface (string name, bool ignoreCase) {
848 throw not_supported ();
851 public override Type[] GetInterfaces () {
852 if (interfaces != null) {
853 Type[] ret = new Type [interfaces.Length];
854 interfaces.CopyTo (ret, 0);
857 return Type.EmptyTypes;
861 public override MemberInfo[] GetMember (string name, MemberTypes type,
862 BindingFlags bindingAttr) {
863 throw not_supported ();
866 public override MemberInfo[] GetMembers (BindingFlags bindingAttr) {
867 throw not_supported ();
870 private MethodInfo[] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type) {
871 MethodInfo[] candidates;
872 if (((bindingAttr & BindingFlags.DeclaredOnly) == 0) && (parent != null)) {
873 MethodInfo[] parent_methods = parent.GetMethods (bindingAttr);
875 candidates = parent_methods;
877 candidates = new MethodInfo [methods.Length + parent_methods.Length];
878 parent_methods.CopyTo (candidates, 0);
879 methods.CopyTo (candidates, parent_methods.Length);
883 candidates = methods;
885 if (candidates == null)
886 return new MethodInfo [0];
888 ArrayList l = new ArrayList ();
890 MethodAttributes mattrs;
892 foreach (MethodInfo c in candidates) {
896 if (String.Compare (c.Name, name, ignoreCase) != 0)
900 mattrs = c.Attributes;
901 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
902 if ((bindingAttr & BindingFlags.Public) != 0)
905 if ((bindingAttr & BindingFlags.NonPublic) != 0)
911 if ((mattrs & MethodAttributes.Static) != 0) {
912 if ((bindingAttr & BindingFlags.Static) != 0)
915 if ((bindingAttr & BindingFlags.Instance) != 0)
923 MethodInfo[] result = new MethodInfo [l.Count];
928 public override MethodInfo[] GetMethods (BindingFlags bindingAttr) {
929 return GetMethodsByName (null, bindingAttr, false, this);
932 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
934 CallingConventions callConvention,
935 Type[] types, ParameterModifier[] modifiers)
938 /* MS.Net throws this exception if the type is unfinished... */
939 throw not_supported ();
941 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
942 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
943 MethodInfo found = null;
945 int typesLen = (types != null) ? types.Length : 0;
948 foreach (MethodInfo m in methods) {
949 // Under MS.NET, Standard|HasThis matches Standard...
950 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
959 if (count == 1 && typesLen == 0)
962 match = new MethodBase [count];
967 foreach (MethodInfo m in methods) {
968 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
975 return (MethodInfo) Binder.FindMostDerivedMatch (match);
978 binder = Binder.DefaultBinder;
980 return (MethodInfo)binder.SelectMethod (bindingAttr, match, types, modifiers);
983 public override Type GetNestedType( string name, BindingFlags bindingAttr) {
984 throw not_supported ();
987 public override Type[] GetNestedTypes (BindingFlags bindingAttr) {
989 ArrayList result = new ArrayList ();
991 if (subtypes == null)
992 return Type.EmptyTypes;
993 foreach (TypeBuilder t in subtypes) {
995 if ((t.attrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) {
996 if ((bindingAttr & BindingFlags.Public) != 0)
999 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1006 Type[] r = new Type [result.Count];
1011 public override PropertyInfo[] GetProperties( BindingFlags bindingAttr) {
1012 if (properties == null)
1013 return new PropertyInfo [0];
1014 ArrayList l = new ArrayList ();
1016 MethodAttributes mattrs;
1017 MethodInfo accessor;
1019 foreach (PropertyInfo c in properties) {
1021 accessor = c.GetGetMethod (true);
1022 if (accessor == null)
1023 accessor = c.GetSetMethod (true);
1024 if (accessor == null)
1026 mattrs = accessor.Attributes;
1027 if ((mattrs & MethodAttributes.MemberAccessMask) == MethodAttributes.Public) {
1028 if ((bindingAttr & BindingFlags.Public) != 0)
1031 if ((bindingAttr & BindingFlags.NonPublic) != 0)
1037 if ((mattrs & MethodAttributes.Static) != 0) {
1038 if ((bindingAttr & BindingFlags.Static) != 0)
1041 if ((bindingAttr & BindingFlags.Instance) != 0)
1048 PropertyInfo[] result = new PropertyInfo [l.Count];
1053 protected override PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
1054 throw not_supported ();
1057 protected override bool HasElementTypeImpl () {
1058 // According to the MSDN docs, this is supported for TypeBuilders,
1059 // but in reality, it is not
1060 throw not_supported ();
1061 // return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
1064 public override object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) {
1065 throw not_supported ();
1068 protected override bool IsArrayImpl ()
1070 return Type.IsArrayImpl (this);
1073 protected override bool IsByRefImpl () {
1077 protected override bool IsCOMObjectImpl () {
1080 protected override bool IsPointerImpl () {
1084 protected override bool IsPrimitiveImpl () {
1088 protected override bool IsValueTypeImpl () {
1089 return ((type_is_subtype_of (this, pmodule.assemblyb.corlib_value_type, false) || type_is_subtype_of (this, typeof(System.ValueType), false)) &&
1090 this != pmodule.assemblyb.corlib_value_type &&
1091 this != pmodule.assemblyb.corlib_enum_type);
1094 public override RuntimeTypeHandle TypeHandle {
1096 throw not_supported ();
1100 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
1101 if (customBuilder == null)
1102 throw new ArgumentNullException ("customBuilder");
1104 string attrname = customBuilder.Ctor.ReflectedType.FullName;
1105 if (attrname == "System.Runtime.InteropServices.StructLayoutAttribute") {
1106 byte[] data = customBuilder.Data;
1107 int layout_kind; /* the (stupid) ctor takes a short or an int ... */
1108 layout_kind = (int)data [2];
1109 layout_kind |= ((int)data [3]) << 8;
1110 attrs &= ~TypeAttributes.LayoutMask;
1111 switch ((LayoutKind)layout_kind) {
1112 case LayoutKind.Auto:
1113 attrs |= TypeAttributes.AutoLayout;
1115 case LayoutKind.Explicit:
1116 attrs |= TypeAttributes.ExplicitLayout;
1118 case LayoutKind.Sequential:
1119 attrs |= TypeAttributes.SequentialLayout;
1122 // we should ignore it since it can be any value anyway...
1123 throw new Exception ("Error in customattr");
1125 string first_type_name = customBuilder.Ctor.GetParameters()[0].ParameterType.FullName;
1127 if (first_type_name == "System.Int16")
1129 int nnamed = (int)data [pos++];
1130 nnamed |= ((int)data [pos++]) << 8;
1131 for (int i = 0; i < nnamed; ++i) {
1132 byte named_type = data [pos++];
1133 byte type = data [pos++];
1138 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1139 string named_typename = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1141 // FIXME: Check that 'named_type' and 'named_typename' match, etc.
1142 // See related code/FIXME in mono/mono/metadata/reflection.c
1145 len = CustomAttributeBuilder.decode_len (data, pos, out pos);
1146 named_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
1148 /* all the fields are integers in StructLayout */
1149 int value = (int)data [pos++];
1150 value |= ((int)data [pos++]) << 8;
1151 value |= ((int)data [pos++]) << 16;
1152 value |= ((int)data [pos++]) << 24;
1153 switch (named_name) {
1155 switch ((CharSet)value) {
1159 case CharSet.Unicode:
1160 attrs |= TypeAttributes.UnicodeClass;
1163 attrs |= TypeAttributes.AutoClass;
1166 break; // error out...
1170 packing_size = (PackingSize)value;
1176 break; // error out...
1180 } else if (attrname == "System.SerializableAttribute") {
1181 attrs |= TypeAttributes.Serializable;
1184 if (cattrs != null) {
1185 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
1186 cattrs.CopyTo (new_array, 0);
1187 new_array [cattrs.Length] = customBuilder;
1190 cattrs = new CustomAttributeBuilder [1];
1191 cattrs [0] = customBuilder;
1194 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
1195 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
1198 public EventBuilder DefineEvent( string name, EventAttributes attributes, Type eventtype) {
1199 check_name ("name", name);
1200 if (eventtype == null)
1201 throw new ArgumentNullException ("eventtype");
1203 throw not_after_created ();
1205 EventBuilder res = new EventBuilder (this, name, attributes, eventtype);
1206 if (events != null) {
1207 EventBuilder[] new_events = new EventBuilder [events.Length+1];
1208 System.Array.Copy (events, new_events, events.Length);
1209 new_events [events.Length] = res;
1210 events = new_events;
1212 events = new EventBuilder [1];
1218 public FieldBuilder DefineInitializedData( string name, byte[] data, FieldAttributes attributes) {
1220 throw new ArgumentNullException ("data");
1221 if ((data.Length == 0) || (data.Length > 0x3f0000))
1222 throw new ArgumentException ("data", "Data size must be > 0 and < 0x3f0000");
1224 FieldBuilder res = DefineUninitializedData (name, data.Length, attributes);
1225 res.SetRVAData (data);
1230 static int UnmanagedDataCount = 0;
1232 public FieldBuilder DefineUninitializedData( string name, int size, FieldAttributes attributes) {
1233 check_name ("name", name);
1234 if ((size <= 0) || (size > 0x3f0000))
1235 throw new ArgumentException ("size", "Data size must be > 0 and < 0x3f0000");
1237 throw not_after_created ();
1239 string s = "$ArrayType$"+UnmanagedDataCount.ToString();
1240 UnmanagedDataCount++;
1241 TypeBuilder datablobtype = DefineNestedType (s,
1242 TypeAttributes.NestedPrivate|TypeAttributes.ExplicitLayout|TypeAttributes.Sealed,
1243 pmodule.assemblyb.corlib_value_type, null, PackingSize.Size1, size);
1244 datablobtype.CreateType ();
1245 return DefineField (name, datablobtype, attributes|FieldAttributes.Static|FieldAttributes.HasFieldRVA);
1248 public TypeToken TypeToken {
1250 return new TypeToken (0x02000000 | table_idx);
1253 public void SetParent (Type parentType) {
1254 if (parentType == null)
1255 throw new ArgumentNullException ("parentType");
1257 throw not_after_created ();
1259 parent = parentType;
1260 // will just set the parent-related bits if called a second time
1261 setup_internal_class (this);
1263 internal int get_next_table_index (object obj, int table, bool inc) {
1264 return pmodule.get_next_table_index (obj, table, inc);
1267 public override InterfaceMapping GetInterfaceMap (Type interfaceType)
1269 if (created == null)
1270 throw new NotSupportedException ("This method is not implemented for incomplete types.");
1272 return created.GetInterfaceMap (interfaceType);
1275 internal bool is_created {
1277 return created != null;
1281 private Exception not_supported ()
1283 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
1286 private Exception not_after_created ()
1288 return new InvalidOperationException ("Unable to change after type has been created.");
1291 private void check_name (string argName, string name)
1294 throw new ArgumentNullException (argName);
1296 throw new ArgumentException (argName, "Empty name is not legal.");
1297 if (name.IndexOf ((char)0) != -1)
1298 throw new ArgumentException (argName, "Illegal name.");
1301 public override String ToString ()
1307 public override bool IsAssignableFrom (Type c)
1309 return base.IsAssignableFrom (c);
1313 public override bool IsSubclassOf (Type c)
1315 return base.IsSubclassOf (c);
1318 #if NET_2_0 || BOOTSTRAP_NET_2_0
1319 public override Type[] GetGenericArguments ()
1321 if (generic_params != null)
1322 return generic_params;
1324 throw new InvalidOperationException ();
1327 public override Type GetGenericTypeDefinition ()
1329 setup_generic_class (this);
1331 return base.GetGenericTypeDefinition ();
1334 public override bool HasGenericArguments {
1336 throw new NotImplementedException ();
1340 public override bool ContainsGenericParameters {
1342 return generic_params != null;
1346 public extern override bool IsGenericParameter {
1347 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1351 public override int GenericParameterPosition {
1353 throw new NotImplementedException ();
1357 public override MethodInfo DeclaringMethod {
1359 throw new NotImplementedException ();
1363 public GenericTypeParameterBuilder[] DefineGenericParameters (string[] names)
1365 generic_params = new GenericTypeParameterBuilder [names.Length];
1366 for (int i = 0; i < names.Length; i++)
1367 generic_params [i] = new GenericTypeParameterBuilder (
1368 this, null, names [i], i);
1370 return generic_params;
1373 public MethodBuilder DefineGenericMethod (string name, MethodAttributes attributes)
1375 return DefineMethod (name, attributes, CallingConventions.Standard, null, null);