5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 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.
33 using System.Diagnostics;
34 using System.Reflection;
35 using System.Reflection.Emit;
36 using System.Collections;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
39 using System.Globalization;
44 [ClassInterface (ClassInterfaceType.None)]
46 [ComDefaultInterface (typeof (_Type))]
47 public abstract class Type : MemberInfo, IReflect, _Type {
49 internal RuntimeTypeHandle _impl;
51 public static readonly char Delimiter = '.';
52 public static readonly Type[] EmptyTypes = {};
53 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
54 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
55 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
56 public static readonly object Missing = System.Reflection.Missing.Value;
58 internal const BindingFlags DefaultBindingFlags =
59 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
61 /* implementation of the delegates for MemberFilter */
62 static bool FilterName_impl (MemberInfo m, object filterCriteria)
64 string name = (string) filterCriteria;
65 if (name == null || name.Length == 0 )
66 return false; // because m.Name cannot be null or empty
68 if (name [name.Length-1] == '*')
69 return string.CompareOrdinal (name, 0, m.Name, 0, name.Length-1) == 0;
71 return name.Equals (m.Name);
74 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
76 string name = (string) filterCriteria;
77 if (name == null || name.Length == 0 )
78 return false; // because m.Name cannot be null or empty
80 if (name [name.Length-1] == '*')
81 return string.Compare (name, 0, m.Name, 0, name.Length-1, StringComparison.OrdinalIgnoreCase) == 0;
83 return string.Equals (name, m.Name, StringComparison.OrdinalIgnoreCase);
86 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
88 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
90 return ((int)((MethodInfo)m).Attributes & flags) != 0;
92 return ((int)((FieldInfo)m).Attributes & flags) != 0;
93 if (m is PropertyInfo)
94 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
96 return ((int)((EventInfo)m).Attributes & flags) != 0;
105 /// The assembly where the type is defined.
107 public abstract Assembly Assembly {
112 /// Gets the fully qualified name for the type including the
113 /// assembly name where the type is defined.
115 public abstract string AssemblyQualifiedName {
120 /// Returns the Attributes associated with the type.
122 public TypeAttributes Attributes {
124 return GetAttributeFlagsImpl ();
129 /// Returns the basetype for this type
131 public abstract Type BaseType {
136 /// Returns the class that declares the member.
138 public override Type DeclaringType {
147 public static Binder DefaultBinder {
149 return Binder.DefaultBinder;
154 /// The full name of the type including its namespace
156 public abstract string FullName {
160 public abstract Guid GUID {
164 public bool HasElementType {
166 return HasElementTypeImpl ();
170 public bool IsAbstract {
172 return (Attributes & TypeAttributes.Abstract) != 0;
176 public bool IsAnsiClass {
178 return (Attributes & TypeAttributes.StringFormatMask)
179 == TypeAttributes.AnsiClass;
183 public bool IsArray {
185 return IsArrayImpl ();
189 public bool IsAutoClass {
191 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
195 public bool IsAutoLayout {
197 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
201 public bool IsByRef {
203 return IsByRefImpl ();
207 public bool IsClass {
216 public bool IsCOMObject {
218 return IsCOMObjectImpl ();
222 public bool IsContextful {
224 return IsContextfulImpl ();
234 return IsSubclassOf (typeof (Enum));
238 public bool IsExplicitLayout {
240 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
244 public bool IsImport {
246 return (Attributes & TypeAttributes.Import) != 0;
250 public bool IsInterface {
252 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
256 public bool IsLayoutSequential {
258 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
262 public bool IsMarshalByRef {
264 return IsMarshalByRefImpl ();
268 public bool IsNestedAssembly {
270 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
274 public bool IsNestedFamANDAssem {
276 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
280 public bool IsNestedFamily {
282 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
286 public bool IsNestedFamORAssem {
288 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
292 public bool IsNestedPrivate {
294 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
298 public bool IsNestedPublic {
300 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
304 public bool IsNotPublic {
306 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
310 public bool IsPointer {
312 return IsPointerImpl ();
316 public bool IsPrimitive {
318 return IsPrimitiveImpl ();
322 public bool IsPublic {
324 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
328 public bool IsSealed {
330 return (Attributes & TypeAttributes.Sealed) != 0;
338 bool IsSerializable {
340 if ((Attributes & TypeAttributes.Serializable) != 0)
343 // Enums and delegates are always serializable
345 Type type = UnderlyingSystemType;
349 // Fast check for system types
350 if (type.IsSystemType)
351 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
353 // User defined types depend on this behavior
355 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
358 type = type.BaseType;
359 } while (type != null);
365 public bool IsSpecialName {
367 return (Attributes & TypeAttributes.SpecialName) != 0;
371 public bool IsUnicodeClass {
373 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
377 public bool IsValueType {
379 return IsValueTypeImpl ();
383 public override MemberTypes MemberType {
384 get {return MemberTypes.TypeInfo;}
388 public abstract Module Module {get;}
390 public abstract string Namespace {get;}
392 public override Type ReflectedType {
398 public virtual RuntimeTypeHandle TypeHandle {
399 get { throw new ArgumentException ("Derived class must provide implementation."); }
403 public ConstructorInfo TypeInitializer {
405 return GetConstructorImpl (
406 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
408 CallingConventions.Any,
415 * This has NOTHING to do with getting the base type of an enum. Use
416 * Enum.GetUnderlyingType () for that.
418 public abstract Type UnderlyingSystemType {get;}
420 public override bool Equals (object o)
423 return Equals (o as Type);
428 Type me = UnderlyingSystemType;
431 return me.EqualsInternal (o as Type);
436 public virtual bool Equals (Type o)
438 if ((object)o == this)
440 if ((object)o == null)
442 Type me = UnderlyingSystemType;
443 if ((object)me == null)
446 o = o.UnderlyingSystemType;
447 if ((object)o == null)
449 if ((object)o == this)
451 return me.EqualsInternal (o);
454 public bool Equals (Type o)
461 Type me = UnderlyingSystemType;
464 return me.EqualsInternal (o.UnderlyingSystemType);
468 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
469 public static bool operator == (Type left, Type right)
471 return Object.ReferenceEquals (left, right);
474 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
475 public static bool operator != (Type left, Type right)
477 return !Object.ReferenceEquals (left, right);
480 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
481 public virtual Type GetEnumUnderlyingType () {
483 throw new ArgumentException ("Type is not an enumeration", "enumType");
485 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
487 if (fields == null || fields.Length != 1)
488 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
490 return fields [0].FieldType;
493 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
494 public virtual string[] GetEnumNames () {
496 throw new ArgumentException ("Type is not an enumeration", "enumType");
498 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
500 string [] result = new string [fields.Length];
501 for (int i = 0; i < fields.Length; ++i)
502 result [i] = fields [i].Name;
507 NotImplementedException CreateNIE () {
508 return new NotImplementedException ();
511 public virtual Array GetEnumValues () {
513 throw new ArgumentException ("Type is not an enumeration", "enumType");
518 bool IsValidEnumType (Type type) {
519 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
522 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
523 public virtual string GetEnumName (object value) {
525 throw new ArgumentException ("Value is null", "value");
526 if (!IsValidEnumType (value.GetType ()))
527 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
529 throw new ArgumentException ("Type is not an enumeration", "enumType");
532 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
534 for (int i = 0; i < fields.Length; ++i) {
535 var fv = fields [i].GetValue (null);
538 //XXX we can't use 'this' as argument as it might be an UserType
539 obj = Enum.ToObject (fv.GetType (), value);
540 } catch (OverflowException) {
542 } catch (InvalidCastException) {
543 throw new ArgumentException ("Value is not valid", "value");
547 return fields [i].Name;
553 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
554 public virtual bool IsEnumDefined (object value) {
556 throw new ArgumentException ("Value is null", "value");
558 throw new ArgumentException ("Type is not an enumeration", "enumType");
560 Type vt = value.GetType ();
561 if (!IsValidEnumType (vt) && vt != typeof (string))
562 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
564 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
566 if (value is string) {
567 for (int i = 0; i < fields.Length; ++i) {
568 if (fields [i].Name.Equals (value))
572 if (vt != this && vt != GetEnumUnderlyingType ())
573 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
576 for (int i = 0; i < fields.Length; ++i) {
577 var fv = fields [i].GetValue (null);
580 //XXX we can't use 'this' as argument as it might be an UserType
581 obj = Enum.ToObject (fv.GetType (), value);
582 } catch (OverflowException) {
584 } catch (InvalidCastException) {
585 throw new ArgumentException ("Value is not valid", "value");
595 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
597 return GetType (typeName, assemblyResolver, typeResolver, false, false);
600 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
602 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
605 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
607 TypeSpec spec = TypeSpec.Parse (typeName);
608 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
611 public virtual bool IsSecurityTransparent
613 get { throw CreateNIE (); }
616 public virtual bool IsSecurityCritical
618 get { throw CreateNIE (); }
621 public virtual bool IsSecuritySafeCritical
623 get { throw CreateNIE (); }
627 [MethodImplAttribute(MethodImplOptions.InternalCall)]
628 internal extern bool EqualsInternal (Type type);
630 [MethodImplAttribute(MethodImplOptions.InternalCall)]
631 private static extern Type internal_from_handle (IntPtr handle);
633 [MethodImplAttribute(MethodImplOptions.InternalCall)]
634 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
636 public static Type GetType(string typeName)
638 if (typeName == null)
639 throw new ArgumentNullException ("TypeName");
641 return internal_from_name (typeName, false, false);
644 public static Type GetType(string typeName, bool throwOnError)
646 if (typeName == null)
647 throw new ArgumentNullException ("TypeName");
649 Type type = internal_from_name (typeName, throwOnError, false);
650 if (throwOnError && type == null)
651 throw new TypeLoadException ("Error loading '" + typeName + "'");
656 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
658 if (typeName == null)
659 throw new ArgumentNullException ("TypeName");
661 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
662 if (throwOnError && t == null)
663 throw new TypeLoadException ("Error loading '" + typeName + "'");
668 public static Type[] GetTypeArray (object[] args) {
670 throw new ArgumentNullException ("args");
673 ret = new Type [args.Length];
674 for (int i = 0; i < args.Length; ++i)
675 ret [i] = args[i].GetType ();
679 [MethodImplAttribute(MethodImplOptions.InternalCall)]
680 internal extern static TypeCode GetTypeCodeInternal (Type type);
685 TypeCode GetTypeCodeImpl () {
687 if (type is MonoType)
688 return GetTypeCodeInternal (type);
690 type = type.UnderlyingSystemType;
692 if (!type.IsSystemType)
693 return TypeCode.Object;
695 return GetTypeCodeInternal (type);
698 public static TypeCode GetTypeCode (Type type) {
700 /* MS.NET returns this */
701 return TypeCode.Empty;
702 return type.GetTypeCodeImpl ();
705 [MonoTODO("This operation is currently not supported by Mono")]
706 public static Type GetTypeFromCLSID (Guid clsid)
708 throw new NotImplementedException ();
711 [MonoTODO("This operation is currently not supported by Mono")]
712 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
714 throw new NotImplementedException ();
717 [MonoTODO("This operation is currently not supported by Mono")]
718 public static Type GetTypeFromCLSID (Guid clsid, string server)
720 throw new NotImplementedException ();
723 [MonoTODO("This operation is currently not supported by Mono")]
724 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
726 throw new NotImplementedException ();
729 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
731 if (handle.Value == IntPtr.Zero)
732 // This is not consistent with the other GetXXXFromHandle methods, but
733 // MS.NET seems to do this
736 return internal_from_handle (handle.Value);
739 [MonoTODO("Mono does not support COM")]
740 public static Type GetTypeFromProgID (string progID)
742 throw new NotImplementedException ();
745 [MonoTODO("Mono does not support COM")]
746 public static Type GetTypeFromProgID (string progID, bool throwOnError)
748 throw new NotImplementedException ();
751 [MonoTODO("Mono does not support COM")]
752 public static Type GetTypeFromProgID (string progID, string server)
754 throw new NotImplementedException ();
757 [MonoTODO("Mono does not support COM")]
758 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
760 throw new NotImplementedException ();
763 public static RuntimeTypeHandle GetTypeHandle (object o)
766 throw new ArgumentNullException ();
768 return o.GetType().TypeHandle;
771 [MethodImplAttribute(MethodImplOptions.InternalCall)]
772 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
774 [MethodImplAttribute(MethodImplOptions.InternalCall)]
775 internal static extern bool type_is_assignable_from (Type a, Type b);
777 public new Type GetType ()
779 return base.GetType ();
783 public virtual bool IsSubclassOf (Type c)
785 if (c == null || c == this)
788 // Fast check for system types
790 return c.IsSystemType && type_is_subtype_of (this, c, false);
792 // User defined types depend on this behavior
793 for (Type type = BaseType; type != null; type = type.BaseType)
800 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
803 throw new ArgumentNullException ("filter");
805 ArrayList ifaces = new ArrayList ();
806 foreach (Type iface in GetInterfaces ()) {
807 if (filter (iface, filterCriteria))
811 return (Type []) ifaces.ToArray (typeof (Type));
814 public Type GetInterface (string name) {
815 return GetInterface (name, false);
818 public abstract Type GetInterface (string name, bool ignoreCase);
820 [MethodImplAttribute(MethodImplOptions.InternalCall)]
821 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
824 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
826 throw new NotSupportedException ("Derived classes must provide an implementation.");
827 if (!interfaceType.IsSystemType)
828 throw new ArgumentException ("interfaceType", "Type is an user type");
829 InterfaceMapping res;
830 if (interfaceType == null)
831 throw new ArgumentNullException ("interfaceType");
832 if (!interfaceType.IsInterface)
833 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
835 throw new ArgumentException ("'this' type cannot be an interface itself");
836 res.TargetType = this;
837 res.InterfaceType = interfaceType;
838 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
839 if (res.TargetMethods == null)
840 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
845 public abstract Type[] GetInterfaces ();
847 public virtual bool IsAssignableFrom (Type c)
855 if (c is TypeBuilder)
856 return ((TypeBuilder)c).IsAssignableTo (this);
858 /* Handle user defined type classes */
860 Type systemType = UnderlyingSystemType;
861 if (!systemType.IsSystemType)
864 Type other = c.UnderlyingSystemType;
865 if (!other.IsSystemType)
868 return systemType.IsAssignableFrom (other);
871 if (!c.IsSystemType) {
872 Type underlyingType = c.UnderlyingSystemType;
873 if (!underlyingType.IsSystemType)
875 return IsAssignableFrom (underlyingType);
878 return type_is_assignable_from (this, c);
881 [MethodImplAttribute(MethodImplOptions.InternalCall)]
882 extern static bool IsInstanceOfType (Type type, object o);
884 public virtual bool IsInstanceOfType (object o)
886 Type type = UnderlyingSystemType;
887 if (!type.IsSystemType)
889 return IsInstanceOfType (type, o);
892 public virtual int GetArrayRank ()
894 throw new NotSupportedException (); // according to MSDN
897 public abstract Type GetElementType ();
899 public EventInfo GetEvent (string name)
901 return GetEvent (name, DefaultBindingFlags);
904 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
906 public virtual EventInfo[] GetEvents ()
908 return GetEvents (DefaultBindingFlags);
911 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
913 public FieldInfo GetField( string name)
915 return GetField (name, DefaultBindingFlags);
918 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
920 public FieldInfo[] GetFields ()
922 return GetFields (DefaultBindingFlags);
925 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
927 public override int GetHashCode()
929 Type t = UnderlyingSystemType;
930 if (t != null && t != this)
931 return t.GetHashCode ();
932 return (int)_impl.Value;
935 public MemberInfo[] GetMember (string name)
937 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
940 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
942 return GetMember (name, MemberTypes.All, bindingAttr);
945 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
948 throw new ArgumentNullException ("name");
949 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
950 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
952 return FindMembers (type, bindingAttr, FilterName, name);
955 public MemberInfo[] GetMembers ()
957 return GetMembers (DefaultBindingFlags);
960 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
962 public MethodInfo GetMethod (string name)
965 throw new ArgumentNullException ("name");
966 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
969 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
972 throw new ArgumentNullException ("name");
974 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
977 public MethodInfo GetMethod (string name, Type[] types)
979 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
982 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
984 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
987 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
988 Type[] types, ParameterModifier[] modifiers)
990 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
993 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
994 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
997 throw new ArgumentNullException ("name");
999 throw new ArgumentNullException ("types");
1001 for (int i = 0; i < types.Length; i++)
1002 if (types[i] == null)
1003 throw new ArgumentNullException ("types");
1005 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1008 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1009 CallingConventions callConvention, Type[] types,
1010 ParameterModifier[] modifiers);
1012 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1013 CallingConventions callConvention, Type[] types,
1014 ParameterModifier[] modifiers)
1016 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1019 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1021 throw new System.InvalidOperationException ("can only be called in generic type");
1024 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1026 throw new System.InvalidOperationException ("can only be called in generic type");
1029 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1031 throw new System.InvalidOperationException ("can only be called in generic type");
1035 public MethodInfo[] GetMethods ()
1037 return GetMethods (DefaultBindingFlags);
1040 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1042 public Type GetNestedType (string name)
1044 return GetNestedType (name, DefaultBindingFlags);
1047 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1049 public Type[] GetNestedTypes ()
1051 return GetNestedTypes (DefaultBindingFlags);
1054 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1057 public PropertyInfo[] GetProperties ()
1059 return GetProperties (DefaultBindingFlags);
1062 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1065 public PropertyInfo GetProperty (string name)
1068 throw new ArgumentNullException ("name");
1070 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1073 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1076 throw new ArgumentNullException ("name");
1077 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1080 public PropertyInfo GetProperty (string name, Type returnType)
1083 throw new ArgumentNullException ("name");
1084 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1087 public PropertyInfo GetProperty (string name, Type[] types)
1089 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1092 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1094 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1097 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1099 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1102 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1103 Type[] types, ParameterModifier[] modifiers)
1106 throw new ArgumentNullException ("name");
1108 throw new ArgumentNullException ("types");
1110 foreach (Type t in types) {
1112 throw new ArgumentNullException ("types");
1115 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1118 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1119 Type returnType, Type[] types, ParameterModifier[] modifiers);
1121 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1122 Type returnType, Type[] types, ParameterModifier[] modifiers)
1124 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1127 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1129 CallingConventions callConvention,
1131 ParameterModifier[] modifiers);
1133 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1134 protected abstract bool HasElementTypeImpl ();
1135 protected abstract bool IsArrayImpl ();
1136 protected abstract bool IsByRefImpl ();
1137 protected abstract bool IsCOMObjectImpl ();
1138 protected abstract bool IsPointerImpl ();
1139 protected abstract bool IsPrimitiveImpl ();
1141 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1142 internal static extern bool IsArrayImpl (Type type);
1144 protected virtual bool IsValueTypeImpl ()
1146 if (this == typeof (ValueType) || this == typeof (Enum))
1149 return IsSubclassOf (typeof (ValueType));
1152 protected virtual bool IsContextfulImpl ()
1154 return typeof (ContextBoundObject).IsAssignableFrom (this);
1157 protected virtual bool IsMarshalByRefImpl ()
1159 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1163 public ConstructorInfo GetConstructor (Type[] types)
1165 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1169 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1170 Type[] types, ParameterModifier[] modifiers)
1172 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1176 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1177 CallingConventions callConvention,
1178 Type[] types, ParameterModifier[] modifiers)
1181 throw new ArgumentNullException ("types");
1183 foreach (Type t in types) {
1185 throw new ArgumentNullException ("types");
1188 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1192 public ConstructorInfo[] GetConstructors ()
1194 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1198 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1200 public virtual MemberInfo[] GetDefaultMembers ()
1202 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1203 if (att.Length == 0)
1204 return new MemberInfo [0];
1206 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1207 return (member != null) ? member : new MemberInfo [0];
1210 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1211 MemberFilter filter, object filterCriteria)
1213 MemberInfo[] result;
1214 ArrayList l = new ArrayList ();
1216 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1217 // this.FullName, this.GetType().FullName, this.obj_address());
1218 if ((memberType & MemberTypes.Method) != 0) {
1219 MethodInfo[] c = GetMethods (bindingAttr);
1220 if (filter != null) {
1221 foreach (MemberInfo m in c) {
1222 if (filter (m, filterCriteria))
1229 if ((memberType & MemberTypes.Constructor) != 0) {
1230 ConstructorInfo[] c = GetConstructors (bindingAttr);
1231 if (filter != null) {
1232 foreach (MemberInfo m in c) {
1233 if (filter (m, filterCriteria))
1240 if ((memberType & MemberTypes.Property) != 0) {
1241 PropertyInfo[] c = GetProperties (bindingAttr);
1244 if (filter != null) {
1245 foreach (MemberInfo m in c) {
1246 if (filter (m, filterCriteria))
1254 if ((memberType & MemberTypes.Event) != 0) {
1255 EventInfo[] c = GetEvents (bindingAttr);
1256 if (filter != null) {
1257 foreach (MemberInfo m in c) {
1258 if (filter (m, filterCriteria))
1265 if ((memberType & MemberTypes.Field) != 0) {
1266 FieldInfo[] c = GetFields (bindingAttr);
1267 if (filter != null) {
1268 foreach (MemberInfo m in c) {
1269 if (filter (m, filterCriteria))
1276 if ((memberType & MemberTypes.NestedType) != 0) {
1277 Type[] c = GetNestedTypes (bindingAttr);
1278 if (filter != null) {
1279 foreach (MemberInfo m in c) {
1280 if (filter (m, filterCriteria)) {
1289 switch (memberType) {
1290 case MemberTypes.Constructor :
1291 result = new ConstructorInfo [l.Count];
1293 case MemberTypes.Event :
1294 result = new EventInfo [l.Count];
1296 case MemberTypes.Field :
1297 result = new FieldInfo [l.Count];
1299 case MemberTypes.Method :
1300 result = new MethodInfo [l.Count];
1302 case MemberTypes.NestedType :
1303 case MemberTypes.TypeInfo :
1304 result = new Type [l.Count];
1306 case MemberTypes.Property :
1307 result = new PropertyInfo [l.Count];
1310 result = new MemberInfo [l.Count];
1318 [DebuggerStepThrough]
1319 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1321 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1325 [DebuggerStepThrough]
1326 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1327 object target, object[] args, CultureInfo culture)
1329 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1332 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1333 Binder binder, object target, object[] args,
1334 ParameterModifier[] modifiers,
1335 CultureInfo culture, string[] namedParameters);
1337 public override string ToString()
1342 internal virtual bool IsCompilerContext {
1344 AssemblyBuilder builder = Assembly as AssemblyBuilder;
1345 return builder != null && builder.IsCompilerContext;
1349 internal bool IsSystemType {
1351 return _impl.Value != IntPtr.Zero;
1355 public virtual Type[] GetGenericArguments ()
1357 throw new NotSupportedException ();
1360 public virtual bool ContainsGenericParameters {
1361 get { return false; }
1364 public virtual extern bool IsGenericTypeDefinition {
1365 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1369 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1370 internal extern Type GetGenericTypeDefinition_impl ();
1372 public virtual Type GetGenericTypeDefinition ()
1374 throw new NotSupportedException ("Derived classes must provide an implementation.");
1377 public virtual extern bool IsGenericType {
1378 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1382 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1383 static extern Type MakeGenericType (Type gt, Type [] types);
1385 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1387 if (type.Assembly is AssemblyBuilder)
1388 return (AssemblyBuilder)type.Assembly;
1390 if (type.HasElementType)
1391 return PeelAssemblyBuilder (type.GetElementType ());
1393 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1396 foreach (Type arg in type.GetGenericArguments ()) {
1397 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1404 public virtual Type MakeGenericType (params Type[] typeArguments)
1407 throw new NotSupportedException ();
1408 if (!IsGenericTypeDefinition)
1409 throw new InvalidOperationException ("not a generic type definition");
1410 if (typeArguments == null)
1411 throw new ArgumentNullException ("typeArguments");
1412 if (GetGenericArguments().Length != typeArguments.Length)
1413 throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", GetGenericArguments ().Length, typeArguments.Length), "typeArguments");
1415 bool hasUserType = false;
1416 AssemblyBuilder compilerContext = null;
1418 Type[] systemTypes = new Type[typeArguments.Length];
1419 for (int i = 0; i < typeArguments.Length; ++i) {
1420 Type t = typeArguments [i];
1422 throw new ArgumentNullException ("typeArguments");
1424 if (!(t is MonoType))
1426 if (t.IsCompilerContext)
1427 compilerContext = PeelAssemblyBuilder (t);
1428 systemTypes [i] = t;
1432 if (compilerContext != null)
1433 return compilerContext.MakeGenericType (this, typeArguments);
1434 return new MonoGenericClass (this, typeArguments);
1437 Type res = MakeGenericType (this, systemTypes);
1439 throw new TypeLoadException ();
1443 public virtual bool IsGenericParameter {
1449 public bool IsNested {
1451 return DeclaringType != null;
1455 public bool IsVisible {
1458 return DeclaringType.IsVisible;
1464 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1465 extern int GetGenericParameterPosition ();
1467 public virtual int GenericParameterPosition {
1469 int res = GetGenericParameterPosition ();
1471 throw new InvalidOperationException ();
1476 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1477 extern GenericParameterAttributes GetGenericParameterAttributes ();
1479 public virtual GenericParameterAttributes GenericParameterAttributes {
1482 throw new NotSupportedException ("Derived classes must provide an implementation.");
1484 if (!IsGenericParameter)
1485 throw new InvalidOperationException ();
1487 return GetGenericParameterAttributes ();
1491 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1492 extern Type[] GetGenericParameterConstraints_impl ();
1494 public virtual Type[] GetGenericParameterConstraints ()
1497 throw new InvalidOperationException ();
1499 if (!IsGenericParameter)
1500 throw new InvalidOperationException ();
1502 return GetGenericParameterConstraints_impl ();
1505 public virtual MethodBase DeclaringMethod {
1511 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1512 extern Type make_array_type (int rank);
1514 public virtual Type MakeArrayType ()
1517 throw new NotSupportedException ("Derived classes must provide an implementation.");
1518 return make_array_type (0);
1521 public virtual Type MakeArrayType (int rank)
1524 throw new NotSupportedException ("Derived classes must provide an implementation.");
1525 if (rank < 1 || rank > 255)
1526 throw new IndexOutOfRangeException ();
1527 return make_array_type (rank);
1530 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1531 extern Type make_byref_type ();
1533 public virtual Type MakeByRefType ()
1536 throw new NotSupportedException ("Derived classes must provide an implementation.");
1538 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1539 return make_byref_type ();
1542 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1543 static extern Type MakePointerType (Type type);
1545 public virtual Type MakePointerType ()
1548 throw new NotSupportedException ("Derived classes must provide an implementation.");
1549 return MakePointerType (this);
1552 public static Type ReflectionOnlyGetType (string typeName,
1553 bool throwIfNotFound,
1556 if (typeName == null)
1557 throw new ArgumentNullException ("typeName");
1558 int idx = typeName.IndexOf (',');
1559 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1560 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1561 string an = typeName.Substring (idx + 1);
1564 a = Assembly.ReflectionOnlyLoad (an);
1566 if (throwIfNotFound)
1570 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1573 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1574 extern void GetPacking (out int packing, out int size);
1576 public virtual StructLayoutAttribute StructLayoutAttribute {
1580 if (IsLayoutSequential)
1581 kind = LayoutKind.Sequential;
1582 else if (IsExplicitLayout)
1583 kind = LayoutKind.Explicit;
1585 kind = LayoutKind.Auto;
1587 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1590 attr.CharSet = CharSet.Unicode;
1591 else if (IsAnsiClass)
1592 attr.CharSet = CharSet.Ansi;
1594 attr.CharSet = CharSet.Auto;
1596 if (kind != LayoutKind.Auto)
1597 GetPacking (out attr.Pack, out attr.Size);
1603 internal object[] GetPseudoCustomAttributes ()
1607 /* IsSerializable returns true for delegates/enums as well */
1608 if ((Attributes & TypeAttributes.Serializable) != 0)
1610 if ((Attributes & TypeAttributes.Import) != 0)
1615 object[] attrs = new object [count];
1618 if ((Attributes & TypeAttributes.Serializable) != 0)
1619 attrs [count ++] = new SerializableAttribute ();
1620 if ((Attributes & TypeAttributes.Import) != 0)
1621 attrs [count ++] = new ComImportAttribute ();
1627 #if NET_4_0 || BOOTSTRAP_NET_4_0
1628 public virtual bool IsEquivalentTo (Type other)
1630 return this == other;
1635 * Return whenever this object is an instance of a user defined subclass
1636 * of System.Type or an instance of TypeDelegator.
1638 internal bool IsUserType {
1641 * subclasses cannot modify _impl so if it is zero, it means the
1642 * type is not created by the runtime.
1644 return _impl.Value == IntPtr.Zero &&
1645 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1649 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1651 throw new NotImplementedException ();
1654 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1656 throw new NotImplementedException ();
1659 void _Type.GetTypeInfoCount (out uint pcTInfo)
1661 throw new NotImplementedException ();
1664 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1666 throw new NotImplementedException ();