5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // (C) Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Diagnostics;
35 using System.Reflection;
37 using System.Reflection.Emit;
39 using System.Collections;
40 using System.Collections.Generic;
41 using System.Runtime.InteropServices;
42 using System.Runtime.CompilerServices;
43 using System.Globalization;
48 [ClassInterface (ClassInterfaceType.None)]
50 [ComDefaultInterface (typeof (_Type))]
51 [StructLayout (LayoutKind.Sequential)]
53 public abstract class Type : MemberInfo, IReflect {
55 public abstract class Type : MemberInfo, IReflect, _Type {
58 internal RuntimeTypeHandle _impl;
60 public static readonly char Delimiter = '.';
61 public static readonly Type[] EmptyTypes = {};
62 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
63 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
64 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
65 public static readonly object Missing = System.Reflection.Missing.Value;
67 internal const BindingFlags DefaultBindingFlags =
68 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
70 /* implementation of the delegates for MemberFilter */
71 static bool FilterName_impl (MemberInfo m, object filterCriteria)
73 string name = (string) filterCriteria;
74 if (name == null || name.Length == 0 )
75 return false; // because m.Name cannot be null or empty
77 if (name [name.Length - 1] == '*')
78 return m.Name.StartsWithOrdinalUnchecked (name.Substring (0, name.Length - 1));
80 return m.Name == name;
83 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
85 string name = (string) filterCriteria;
86 if (name == null || name.Length == 0 )
87 return false; // because m.Name cannot be null or empty
89 if (name [name.Length - 1] == '*')
90 return m.Name.StartsWithOrdinalCaseInsensitiveUnchecked (name.Substring (0, name.Length - 1));
92 return string.CompareOrdinalCaseInsensitiveUnchecked (m.Name, name) == 0;
95 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
97 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
99 return ((int)((MethodInfo)m).Attributes & flags) != 0;
101 return ((int)((FieldInfo)m).Attributes & flags) != 0;
102 if (m is PropertyInfo)
103 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
105 return ((int)((EventInfo)m).Attributes & flags) != 0;
114 /// The assembly where the type is defined.
116 public abstract Assembly Assembly {
121 /// Gets the fully qualified name for the type including the
122 /// assembly name where the type is defined.
124 public abstract string AssemblyQualifiedName {
129 /// Returns the Attributes associated with the type.
131 public TypeAttributes Attributes {
133 return GetAttributeFlagsImpl ();
138 /// Returns the basetype for this type
140 public abstract Type BaseType {
145 /// Returns the class that declares the member.
147 public override Type DeclaringType {
156 public static Binder DefaultBinder {
158 return Binder.DefaultBinder;
163 /// The full name of the type including its namespace
165 public abstract string FullName {
169 public abstract Guid GUID {
173 public bool HasElementType {
175 return HasElementTypeImpl ();
179 public bool IsAbstract {
181 return (Attributes & TypeAttributes.Abstract) != 0;
185 public bool IsAnsiClass {
187 return (Attributes & TypeAttributes.StringFormatMask)
188 == TypeAttributes.AnsiClass;
192 public bool IsArray {
194 return IsArrayImpl ();
198 public bool IsAutoClass {
200 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
204 public bool IsAutoLayout {
206 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
210 public bool IsByRef {
212 return IsByRefImpl ();
216 public bool IsClass {
225 public bool IsCOMObject {
227 return IsCOMObjectImpl ();
232 public virtual bool IsConstructedGenericType {
234 throw new NotImplementedException ();
239 public bool IsContextful {
241 return IsContextfulImpl ();
251 return IsSubclassOf (typeof (Enum));
255 public bool IsExplicitLayout {
257 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
261 public bool IsImport {
263 return (Attributes & TypeAttributes.Import) != 0;
267 public bool IsInterface {
269 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
273 public bool IsLayoutSequential {
275 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
279 public bool IsMarshalByRef {
281 return IsMarshalByRefImpl ();
285 public bool IsNestedAssembly {
287 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
291 public bool IsNestedFamANDAssem {
293 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
297 public bool IsNestedFamily {
299 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
303 public bool IsNestedFamORAssem {
305 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
309 public bool IsNestedPrivate {
311 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
315 public bool IsNestedPublic {
317 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
321 public bool IsNotPublic {
323 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
327 public bool IsPointer {
329 return IsPointerImpl ();
333 public bool IsPrimitive {
335 return IsPrimitiveImpl ();
339 public bool IsPublic {
341 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
345 public bool IsSealed {
347 return (Attributes & TypeAttributes.Sealed) != 0;
355 bool IsSerializable {
357 if ((Attributes & TypeAttributes.Serializable) != 0)
360 // Enums and delegates are always serializable
362 Type type = UnderlyingSystemType;
366 // Fast check for system types
367 if (type.IsSystemType)
368 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
370 // User defined types depend on this behavior
372 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
375 type = type.BaseType;
376 } while (type != null);
382 public bool IsSpecialName {
384 return (Attributes & TypeAttributes.SpecialName) != 0;
388 public bool IsUnicodeClass {
390 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
394 public bool IsValueType {
396 return IsValueTypeImpl ();
400 public override MemberTypes MemberType {
402 return MemberTypes.TypeInfo;
406 public abstract override Module Module {
410 public abstract string Namespace {get;}
412 public override Type ReflectedType {
418 public virtual RuntimeTypeHandle TypeHandle {
419 get { throw new ArgumentException ("Derived class must provide implementation."); }
423 public ConstructorInfo TypeInitializer {
425 return GetConstructorImpl (
426 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
428 CallingConventions.Any,
435 * This has NOTHING to do with getting the base type of an enum. Use
436 * Enum.GetUnderlyingType () for that.
438 public abstract Type UnderlyingSystemType {get;}
440 public override bool Equals (object o)
443 return Equals (o as Type);
448 Type me = UnderlyingSystemType;
451 return me.EqualsInternal (o as Type);
456 public virtual bool Equals (Type o)
458 if ((object)o == (object)this)
460 if ((object)o == null)
462 Type me = UnderlyingSystemType;
463 if ((object)me == null)
466 o = o.UnderlyingSystemType;
467 if ((object)o == null)
469 if ((object)o == (object)this)
471 return me.EqualsInternal (o);
474 public bool Equals (Type o)
481 Type me = UnderlyingSystemType;
484 return me.EqualsInternal (o.UnderlyingSystemType);
488 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
489 public static bool operator == (Type left, Type right)
491 return Object.ReferenceEquals (left, right);
494 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
495 public static bool operator != (Type left, Type right)
497 return !Object.ReferenceEquals (left, right);
500 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
501 public virtual Type GetEnumUnderlyingType () {
503 throw new ArgumentException ("Type is not an enumeration", "enumType");
505 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
507 if (fields == null || fields.Length != 1)
508 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
510 return fields [0].FieldType;
513 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
514 public virtual string[] GetEnumNames () {
516 throw new ArgumentException ("Type is not an enumeration", "enumType");
518 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
520 string [] names = new string [fields.Length];
521 if (0 != names.Length) {
522 for (int i = 0; i < fields.Length; ++i)
523 names [i] = fields [i].Name;
525 var et = GetEnumUnderlyingType ();
526 var values = Array.CreateInstance (et, names.Length);
527 for (int i = 0; i < fields.Length; ++i)
528 values.SetValue (fields [i].GetValue (null), i);
529 MonoEnumInfo.SortEnums (et, values, names);
535 static NotImplementedException CreateNIE () {
536 return new NotImplementedException ();
539 public virtual Array GetEnumValues () {
541 throw new ArgumentException ("Type is not an enumeration", "enumType");
546 bool IsValidEnumType (Type type) {
547 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
550 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
551 public virtual string GetEnumName (object value) {
553 throw new ArgumentException ("Value is null", "value");
554 if (!IsValidEnumType (value.GetType ()))
555 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
557 throw new ArgumentException ("Type is not an enumeration", "enumType");
560 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
562 for (int i = 0; i < fields.Length; ++i) {
563 var fv = fields [i].GetValue (null);
566 //XXX we can't use 'this' as argument as it might be an UserType
567 obj = Enum.ToObject (fv.GetType (), value);
568 } catch (OverflowException) {
570 } catch (InvalidCastException) {
571 throw new ArgumentException ("Value is not valid", "value");
575 return fields [i].Name;
581 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
582 public virtual bool IsEnumDefined (object value) {
584 throw new ArgumentException ("Value is null", "value");
586 throw new ArgumentException ("Type is not an enumeration", "enumType");
588 Type vt = value.GetType ();
589 if (!IsValidEnumType (vt) && vt != typeof (string))
590 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
592 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
594 if (value is string) {
595 for (int i = 0; i < fields.Length; ++i) {
596 if (fields [i].Name.Equals (value))
600 if (vt != this && vt != GetEnumUnderlyingType ())
601 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
604 for (int i = 0; i < fields.Length; ++i) {
605 var fv = fields [i].GetValue (null);
608 //XXX we can't use 'this' as argument as it might be an UserType
609 obj = Enum.ToObject (fv.GetType (), value);
610 } catch (OverflowException) {
612 } catch (InvalidCastException) {
613 throw new ArgumentException ("Value is not valid", "value");
623 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
625 return GetType (typeName, assemblyResolver, typeResolver, false, false);
628 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
630 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
633 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
635 TypeSpec spec = TypeSpec.Parse (typeName);
636 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
639 public virtual bool IsSecurityTransparent
641 get { throw CreateNIE (); }
644 public virtual bool IsSecurityCritical
646 get { throw CreateNIE (); }
649 public virtual bool IsSecuritySafeCritical
651 get { throw CreateNIE (); }
655 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656 internal extern bool EqualsInternal (Type type);
658 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659 private static extern Type internal_from_handle (IntPtr handle);
661 [MethodImplAttribute(MethodImplOptions.InternalCall)]
662 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
664 public static Type GetType(string typeName)
666 if (typeName == null)
667 throw new ArgumentNullException ("TypeName");
669 return internal_from_name (typeName, false, false);
672 public static Type GetType(string typeName, bool throwOnError)
674 if (typeName == null)
675 throw new ArgumentNullException ("TypeName");
677 Type type = internal_from_name (typeName, throwOnError, false);
678 if (throwOnError && type == null)
679 throw new TypeLoadException ("Error loading '" + typeName + "'");
684 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
686 if (typeName == null)
687 throw new ArgumentNullException ("TypeName");
689 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
690 if (throwOnError && t == null)
691 throw new TypeLoadException ("Error loading '" + typeName + "'");
696 public static Type[] GetTypeArray (object[] args) {
698 throw new ArgumentNullException ("args");
701 ret = new Type [args.Length];
702 for (int i = 0; i < args.Length; ++i)
703 ret [i] = args[i].GetType ();
707 [MethodImplAttribute(MethodImplOptions.InternalCall)]
708 internal extern static TypeCode GetTypeCodeInternal (Type type);
713 TypeCode GetTypeCodeImpl () {
715 if (type is MonoType)
716 return GetTypeCodeInternal (type);
717 #if !FULL_AOT_RUNTIME
718 if (type is TypeBuilder)
719 return ((TypeBuilder)type).GetTypeCodeInternal ();
722 type = type.UnderlyingSystemType;
724 if (!type.IsSystemType)
725 return TypeCode.Object;
727 return GetTypeCodeInternal (type);
730 public static TypeCode GetTypeCode (Type type) {
732 /* MS.NET returns this */
733 return TypeCode.Empty;
734 return type.GetTypeCodeImpl ();
737 [MonoTODO("This operation is currently not supported by Mono")]
738 public static Type GetTypeFromCLSID (Guid clsid)
740 throw new NotImplementedException ();
743 [MonoTODO("This operation is currently not supported by Mono")]
744 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
746 throw new NotImplementedException ();
749 [MonoTODO("This operation is currently not supported by Mono")]
750 public static Type GetTypeFromCLSID (Guid clsid, string server)
752 throw new NotImplementedException ();
755 [MonoTODO("This operation is currently not supported by Mono")]
756 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
758 throw new NotImplementedException ();
761 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
763 if (handle.Value == IntPtr.Zero)
764 // This is not consistent with the other GetXXXFromHandle methods, but
765 // MS.NET seems to do this
768 return internal_from_handle (handle.Value);
771 [MonoTODO("Mono does not support COM")]
772 public static Type GetTypeFromProgID (string progID)
774 throw new NotImplementedException ();
777 [MonoTODO("Mono does not support COM")]
778 public static Type GetTypeFromProgID (string progID, bool throwOnError)
780 throw new NotImplementedException ();
783 [MonoTODO("Mono does not support COM")]
784 public static Type GetTypeFromProgID (string progID, string server)
786 throw new NotImplementedException ();
789 [MonoTODO("Mono does not support COM")]
790 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
792 throw new NotImplementedException ();
795 public static RuntimeTypeHandle GetTypeHandle (object o)
798 throw new ArgumentNullException ();
800 return o.GetType().TypeHandle;
803 [MethodImplAttribute(MethodImplOptions.InternalCall)]
804 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
806 [MethodImplAttribute(MethodImplOptions.InternalCall)]
807 internal static extern bool type_is_assignable_from (Type a, Type b);
809 public new Type GetType ()
811 return base.GetType ();
815 public virtual bool IsSubclassOf (Type c)
817 if (c == null || c == this)
820 // Fast check for system types
822 return c.IsSystemType && type_is_subtype_of (this, c, false);
824 // User defined types depend on this behavior
825 for (Type type = BaseType; type != null; type = type.BaseType)
832 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
835 throw new ArgumentNullException ("filter");
837 var ifaces = new List<Type> ();
838 foreach (Type iface in GetInterfaces ()) {
839 if (filter (iface, filterCriteria))
843 return ifaces.ToArray ();
846 public Type GetInterface (string name) {
847 return GetInterface (name, false);
850 public abstract Type GetInterface (string name, bool ignoreCase);
852 [MethodImplAttribute(MethodImplOptions.InternalCall)]
853 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
856 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
858 throw new NotSupportedException ("Derived classes must provide an implementation.");
859 if (interfaceType == null)
860 throw new ArgumentNullException ("interfaceType");
861 if (!interfaceType.IsSystemType)
862 throw new ArgumentException ("interfaceType", "Type is an user type");
863 InterfaceMapping res;
864 if (!interfaceType.IsInterface)
865 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
867 throw new ArgumentException ("'this' type cannot be an interface itself");
868 res.TargetType = this;
869 res.InterfaceType = interfaceType;
870 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
871 if (res.TargetMethods == null)
872 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
877 public abstract Type[] GetInterfaces ();
879 public virtual bool IsAssignableFrom (Type c)
887 #if !FULL_AOT_RUNTIME
888 if (c is TypeBuilder)
889 return ((TypeBuilder)c).IsAssignableTo (this);
892 /* Handle user defined type classes */
894 Type systemType = UnderlyingSystemType;
895 if (!systemType.IsSystemType)
898 Type other = c.UnderlyingSystemType;
899 if (!other.IsSystemType)
902 return systemType.IsAssignableFrom (other);
905 if (!c.IsSystemType) {
906 Type underlyingType = c.UnderlyingSystemType;
907 if (!underlyingType.IsSystemType)
909 return IsAssignableFrom (underlyingType);
912 return type_is_assignable_from (this, c);
915 [MethodImplAttribute(MethodImplOptions.InternalCall)]
916 extern static bool IsInstanceOfType (Type type, object o);
918 public virtual bool IsInstanceOfType (object o)
920 Type type = UnderlyingSystemType;
921 if (!type.IsSystemType)
923 return IsInstanceOfType (type, o);
926 public virtual int GetArrayRank ()
928 throw new NotSupportedException (); // according to MSDN
931 public abstract Type GetElementType ();
933 public EventInfo GetEvent (string name)
935 return GetEvent (name, DefaultBindingFlags);
938 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
940 public virtual EventInfo[] GetEvents ()
942 return GetEvents (DefaultBindingFlags);
945 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
947 public FieldInfo GetField( string name)
949 return GetField (name, DefaultBindingFlags);
952 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
954 public FieldInfo[] GetFields ()
956 return GetFields (DefaultBindingFlags);
959 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
961 public override int GetHashCode()
963 Type t = UnderlyingSystemType;
964 if (t != null && t != this)
965 return t.GetHashCode ();
966 return (int)_impl.Value;
969 public MemberInfo[] GetMember (string name)
971 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
974 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
976 return GetMember (name, MemberTypes.All, bindingAttr);
979 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
982 throw new ArgumentNullException ("name");
983 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
984 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
986 return FindMembers (type, bindingAttr, FilterName, name);
989 public MemberInfo[] GetMembers ()
991 return GetMembers (DefaultBindingFlags);
994 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
996 public MethodInfo GetMethod (string name)
999 throw new ArgumentNullException ("name");
1000 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
1003 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
1006 throw new ArgumentNullException ("name");
1008 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1011 public MethodInfo GetMethod (string name, Type[] types)
1013 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1016 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1018 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1021 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1022 Type[] types, ParameterModifier[] modifiers)
1024 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1027 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1028 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1031 throw new ArgumentNullException ("name");
1033 throw new ArgumentNullException ("types");
1035 for (int i = 0; i < types.Length; i++)
1036 if (types[i] == null)
1037 throw new ArgumentNullException ("types");
1039 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1042 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1043 CallingConventions callConvention, Type[] types,
1044 ParameterModifier[] modifiers);
1046 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1047 CallingConventions callConvention, Type[] types,
1048 ParameterModifier[] modifiers)
1050 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1053 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1055 throw new System.InvalidOperationException ("can only be called in generic type");
1058 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1060 throw new System.InvalidOperationException ("can only be called in generic type");
1063 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1065 throw new System.InvalidOperationException ("can only be called in generic type");
1069 public MethodInfo[] GetMethods ()
1071 return GetMethods (DefaultBindingFlags);
1074 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1076 public Type GetNestedType (string name)
1078 return GetNestedType (name, DefaultBindingFlags);
1081 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1083 public Type[] GetNestedTypes ()
1085 return GetNestedTypes (DefaultBindingFlags);
1088 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1091 public PropertyInfo[] GetProperties ()
1093 return GetProperties (DefaultBindingFlags);
1096 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1099 public PropertyInfo GetProperty (string name)
1102 throw new ArgumentNullException ("name");
1104 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1107 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1110 throw new ArgumentNullException ("name");
1111 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1114 public PropertyInfo GetProperty (string name, Type returnType)
1117 throw new ArgumentNullException ("name");
1118 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1121 public PropertyInfo GetProperty (string name, Type[] types)
1123 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1126 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1128 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1131 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1133 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1136 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1137 Type[] types, ParameterModifier[] modifiers)
1140 throw new ArgumentNullException ("name");
1142 throw new ArgumentNullException ("types");
1144 foreach (Type t in types) {
1146 throw new ArgumentNullException ("types");
1149 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1152 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1153 Type returnType, Type[] types, ParameterModifier[] modifiers);
1155 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1156 Type returnType, Type[] types, ParameterModifier[] modifiers)
1158 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1161 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1163 CallingConventions callConvention,
1165 ParameterModifier[] modifiers);
1167 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1168 protected abstract bool HasElementTypeImpl ();
1169 protected abstract bool IsArrayImpl ();
1170 protected abstract bool IsByRefImpl ();
1171 protected abstract bool IsCOMObjectImpl ();
1172 protected abstract bool IsPointerImpl ();
1173 protected abstract bool IsPrimitiveImpl ();
1175 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1176 internal static extern bool IsArrayImpl (Type type);
1178 protected virtual bool IsValueTypeImpl ()
1180 if (this == typeof (ValueType) || this == typeof (Enum))
1183 return IsSubclassOf (typeof (ValueType));
1186 protected virtual bool IsContextfulImpl ()
1188 return typeof (ContextBoundObject).IsAssignableFrom (this);
1191 protected virtual bool IsMarshalByRefImpl ()
1193 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1197 public ConstructorInfo GetConstructor (Type[] types)
1199 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1203 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1204 Type[] types, ParameterModifier[] modifiers)
1206 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1210 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1211 CallingConventions callConvention,
1212 Type[] types, ParameterModifier[] modifiers)
1215 throw new ArgumentNullException ("types");
1217 foreach (Type t in types) {
1219 throw new ArgumentNullException ("types");
1222 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1226 public ConstructorInfo[] GetConstructors ()
1228 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1232 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1234 public virtual MemberInfo[] GetDefaultMembers ()
1236 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1237 if (att.Length == 0)
1238 return EmptyArray<MemberInfo>.Value;
1240 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1241 return (member != null) ? member : EmptyArray<MemberInfo>.Value;
1244 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1245 MemberFilter filter, object filterCriteria)
1247 MemberInfo[] result;
1248 ArrayList l = new ArrayList ();
1250 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1251 // this.FullName, this.GetType().FullName, this.obj_address());
1252 if ((memberType & MemberTypes.Method) != 0) {
1253 MethodInfo[] c = GetMethods (bindingAttr);
1254 if (filter != null) {
1255 foreach (MemberInfo m in c) {
1256 if (filter (m, filterCriteria))
1263 if ((memberType & MemberTypes.Constructor) != 0) {
1264 ConstructorInfo[] c = GetConstructors (bindingAttr);
1265 if (filter != null) {
1266 foreach (MemberInfo m in c) {
1267 if (filter (m, filterCriteria))
1274 if ((memberType & MemberTypes.Property) != 0) {
1275 PropertyInfo[] c = GetProperties (bindingAttr);
1278 if (filter != null) {
1279 foreach (MemberInfo m in c) {
1280 if (filter (m, filterCriteria))
1288 if ((memberType & MemberTypes.Event) != 0) {
1289 EventInfo[] c = GetEvents (bindingAttr);
1290 if (filter != null) {
1291 foreach (MemberInfo m in c) {
1292 if (filter (m, filterCriteria))
1299 if ((memberType & MemberTypes.Field) != 0) {
1300 FieldInfo[] c = GetFields (bindingAttr);
1301 if (filter != null) {
1302 foreach (MemberInfo m in c) {
1303 if (filter (m, filterCriteria))
1310 if ((memberType & MemberTypes.NestedType) != 0) {
1311 Type[] c = GetNestedTypes (bindingAttr);
1312 if (filter != null) {
1313 foreach (MemberInfo m in c) {
1314 if (filter (m, filterCriteria)) {
1323 switch (memberType) {
1324 case MemberTypes.Constructor :
1325 result = new ConstructorInfo [l.Count];
1327 case MemberTypes.Event :
1328 result = new EventInfo [l.Count];
1330 case MemberTypes.Field :
1331 result = new FieldInfo [l.Count];
1333 case MemberTypes.Method :
1334 result = new MethodInfo [l.Count];
1336 case MemberTypes.NestedType :
1337 case MemberTypes.TypeInfo :
1338 result = new Type [l.Count];
1340 case MemberTypes.Property :
1341 result = new PropertyInfo [l.Count];
1344 result = new MemberInfo [l.Count];
1352 [DebuggerStepThrough]
1353 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1355 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1359 [DebuggerStepThrough]
1360 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1361 object target, object[] args, CultureInfo culture)
1363 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1366 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1367 Binder binder, object target, object[] args,
1368 ParameterModifier[] modifiers,
1369 CultureInfo culture, string[] namedParameters);
1371 public override string ToString()
1376 internal virtual Type InternalResolve ()
1378 return UnderlyingSystemType;
1381 internal bool IsSystemType {
1383 return _impl.Value != IntPtr.Zero;
1388 public virtual Type[] GenericTypeArguments {
1390 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1395 public virtual Type[] GetGenericArguments ()
1397 throw new NotSupportedException ();
1400 public virtual bool ContainsGenericParameters {
1401 get { return false; }
1404 public virtual extern bool IsGenericTypeDefinition {
1405 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1409 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1410 internal extern Type GetGenericTypeDefinition_impl ();
1412 public virtual Type GetGenericTypeDefinition ()
1414 throw new NotSupportedException ("Derived classes must provide an implementation.");
1417 public virtual extern bool IsGenericType {
1418 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1422 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1423 static extern Type MakeGenericType (Type gt, Type [] types);
1425 public virtual Type MakeGenericType (params Type[] typeArguments)
1428 throw new NotSupportedException ();
1429 if (!IsGenericTypeDefinition)
1430 throw new InvalidOperationException ("not a generic type definition");
1431 if (typeArguments == null)
1432 throw new ArgumentNullException ("typeArguments");
1433 if (GetGenericArguments().Length != typeArguments.Length)
1434 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");
1436 bool hasUserType = false;
1438 Type[] systemTypes = new Type[typeArguments.Length];
1439 for (int i = 0; i < typeArguments.Length; ++i) {
1440 Type t = typeArguments [i];
1442 throw new ArgumentNullException ("typeArguments");
1444 if (!(t is MonoType))
1446 systemTypes [i] = t;
1450 #if FULL_AOT_RUNTIME
1451 throw new NotSupportedException ("User types are not supported under full aot");
1453 return new MonoGenericClass (this, typeArguments);
1457 Type res = MakeGenericType (this, systemTypes);
1459 throw new TypeLoadException ();
1463 public virtual bool IsGenericParameter {
1469 public bool IsNested {
1471 return DeclaringType != null;
1475 public bool IsVisible {
1478 return DeclaringType.IsVisible;
1484 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1485 extern int GetGenericParameterPosition ();
1487 public virtual int GenericParameterPosition {
1489 int res = GetGenericParameterPosition ();
1491 throw new InvalidOperationException ();
1496 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1497 extern GenericParameterAttributes GetGenericParameterAttributes ();
1499 public virtual GenericParameterAttributes GenericParameterAttributes {
1502 throw new NotSupportedException ("Derived classes must provide an implementation.");
1504 if (!IsGenericParameter)
1505 throw new InvalidOperationException ();
1507 return GetGenericParameterAttributes ();
1511 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1512 extern Type[] GetGenericParameterConstraints_impl ();
1514 public virtual Type[] GetGenericParameterConstraints ()
1517 throw new InvalidOperationException ();
1519 if (!IsGenericParameter)
1520 throw new InvalidOperationException ();
1522 return GetGenericParameterConstraints_impl ();
1525 public virtual MethodBase DeclaringMethod {
1531 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1532 extern Type make_array_type (int rank);
1534 public virtual Type MakeArrayType ()
1537 throw new NotSupportedException ("Derived classes must provide an implementation.");
1538 return make_array_type (0);
1541 public virtual Type MakeArrayType (int rank)
1544 throw new NotSupportedException ("Derived classes must provide an implementation.");
1545 if (rank < 1 || rank > 255)
1546 throw new IndexOutOfRangeException ();
1547 return make_array_type (rank);
1550 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1551 extern Type make_byref_type ();
1553 public virtual Type MakeByRefType ()
1556 throw new NotSupportedException ("Derived classes must provide an implementation.");
1558 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1559 return make_byref_type ();
1562 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1563 static extern Type MakePointerType (Type type);
1565 public virtual Type MakePointerType ()
1568 throw new NotSupportedException ("Derived classes must provide an implementation.");
1569 return MakePointerType (this);
1572 public static Type ReflectionOnlyGetType (string typeName,
1573 bool throwIfNotFound,
1576 if (typeName == null)
1577 throw new ArgumentNullException ("typeName");
1578 int idx = typeName.IndexOf (',');
1579 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1580 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1581 string an = typeName.Substring (idx + 1);
1584 a = Assembly.ReflectionOnlyLoad (an);
1586 if (throwIfNotFound)
1590 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1593 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1594 extern void GetPacking (out int packing, out int size);
1596 public virtual StructLayoutAttribute StructLayoutAttribute {
1599 throw new NotSupportedException ();
1601 return GetStructLayoutAttribute ();
1606 internal StructLayoutAttribute GetStructLayoutAttribute ()
1610 if (IsLayoutSequential)
1611 kind = LayoutKind.Sequential;
1612 else if (IsExplicitLayout)
1613 kind = LayoutKind.Explicit;
1615 kind = LayoutKind.Auto;
1617 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1620 attr.CharSet = CharSet.Unicode;
1621 else if (IsAnsiClass)
1622 attr.CharSet = CharSet.Ansi;
1624 attr.CharSet = CharSet.Auto;
1626 if (kind != LayoutKind.Auto) {
1628 GetPacking (out packing, out attr.Size);
1629 // 0 means no data provided, we end up with default value
1631 attr.Pack = packing;
1637 internal object[] GetPseudoCustomAttributes ()
1641 /* IsSerializable returns true for delegates/enums as well */
1642 if ((Attributes & TypeAttributes.Serializable) != 0)
1644 if ((Attributes & TypeAttributes.Import) != 0)
1649 object[] attrs = new object [count];
1652 if ((Attributes & TypeAttributes.Serializable) != 0)
1653 attrs [count ++] = new SerializableAttribute ();
1654 if ((Attributes & TypeAttributes.Import) != 0)
1655 attrs [count ++] = new ComImportAttribute ();
1662 public virtual bool IsEquivalentTo (Type other)
1664 return this == other;
1669 * Return whenever this object is an instance of a user defined subclass
1670 * of System.Type or an instance of TypeDelegator.
1671 * A user defined type is not simply the opposite of a system type.
1672 * It's any class that's neither a SRE or runtime baked type.
1674 internal virtual bool IsUserType {
1681 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1683 throw new NotImplementedException ();
1686 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1688 throw new NotImplementedException ();
1691 void _Type.GetTypeInfoCount (out uint pcTInfo)
1693 throw new NotImplementedException ();
1696 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1698 throw new NotImplementedException ();