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)]
52 public abstract class Type : MemberInfo, IReflect, _Type {
54 internal RuntimeTypeHandle _impl;
56 public static readonly char Delimiter = '.';
57 public static readonly Type[] EmptyTypes = {};
58 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
59 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
60 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
61 public static readonly object Missing = System.Reflection.Missing.Value;
63 internal const BindingFlags DefaultBindingFlags =
64 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
66 /* implementation of the delegates for MemberFilter */
67 static bool FilterName_impl (MemberInfo m, object filterCriteria)
69 string name = (string) filterCriteria;
70 if (name == null || name.Length == 0 )
71 return false; // because m.Name cannot be null or empty
73 if (name [name.Length-1] == '*')
74 return string.CompareOrdinal (name, 0, m.Name, 0, name.Length-1) == 0;
76 return name.Equals (m.Name);
79 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
81 string name = (string) filterCriteria;
82 if (name == null || name.Length == 0 )
83 return false; // because m.Name cannot be null or empty
85 if (name [name.Length-1] == '*')
86 return string.Compare (name, 0, m.Name, 0, name.Length-1, StringComparison.OrdinalIgnoreCase) == 0;
88 return string.Equals (name, m.Name, StringComparison.OrdinalIgnoreCase);
91 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
93 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
95 return ((int)((MethodInfo)m).Attributes & flags) != 0;
97 return ((int)((FieldInfo)m).Attributes & flags) != 0;
98 if (m is PropertyInfo)
99 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
101 return ((int)((EventInfo)m).Attributes & flags) != 0;
110 /// The assembly where the type is defined.
112 public abstract Assembly Assembly {
117 /// Gets the fully qualified name for the type including the
118 /// assembly name where the type is defined.
120 public abstract string AssemblyQualifiedName {
125 /// Returns the Attributes associated with the type.
127 public TypeAttributes Attributes {
129 return GetAttributeFlagsImpl ();
134 /// Returns the basetype for this type
136 public abstract Type BaseType {
141 /// Returns the class that declares the member.
143 public override Type DeclaringType {
152 public static Binder DefaultBinder {
154 return Binder.DefaultBinder;
159 /// The full name of the type including its namespace
161 public abstract string FullName {
165 public abstract Guid GUID {
169 public bool HasElementType {
171 return HasElementTypeImpl ();
175 public bool IsAbstract {
177 return (Attributes & TypeAttributes.Abstract) != 0;
181 public bool IsAnsiClass {
183 return (Attributes & TypeAttributes.StringFormatMask)
184 == TypeAttributes.AnsiClass;
188 public bool IsArray {
190 return IsArrayImpl ();
194 public bool IsAutoClass {
196 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
200 public bool IsAutoLayout {
202 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
206 public bool IsByRef {
208 return IsByRefImpl ();
212 public bool IsClass {
221 public bool IsCOMObject {
223 return IsCOMObjectImpl ();
228 public virtual bool IsConstructedGenericType {
230 throw new NotImplementedException ();
235 public bool IsContextful {
237 return IsContextfulImpl ();
247 return IsSubclassOf (typeof (Enum));
251 public bool IsExplicitLayout {
253 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
257 public bool IsImport {
259 return (Attributes & TypeAttributes.Import) != 0;
263 public bool IsInterface {
265 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
269 public bool IsLayoutSequential {
271 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
275 public bool IsMarshalByRef {
277 return IsMarshalByRefImpl ();
281 public bool IsNestedAssembly {
283 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
287 public bool IsNestedFamANDAssem {
289 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
293 public bool IsNestedFamily {
295 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
299 public bool IsNestedFamORAssem {
301 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
305 public bool IsNestedPrivate {
307 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
311 public bool IsNestedPublic {
313 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
317 public bool IsNotPublic {
319 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
323 public bool IsPointer {
325 return IsPointerImpl ();
329 public bool IsPrimitive {
331 return IsPrimitiveImpl ();
335 public bool IsPublic {
337 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
341 public bool IsSealed {
343 return (Attributes & TypeAttributes.Sealed) != 0;
351 bool IsSerializable {
353 if ((Attributes & TypeAttributes.Serializable) != 0)
356 // Enums and delegates are always serializable
358 Type type = UnderlyingSystemType;
362 // Fast check for system types
363 if (type.IsSystemType)
364 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
366 // User defined types depend on this behavior
368 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
371 type = type.BaseType;
372 } while (type != null);
378 public bool IsSpecialName {
380 return (Attributes & TypeAttributes.SpecialName) != 0;
384 public bool IsUnicodeClass {
386 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
390 public bool IsValueType {
392 return IsValueTypeImpl ();
396 public override MemberTypes MemberType {
398 return MemberTypes.TypeInfo;
402 public abstract override Module Module {
406 public abstract string Namespace {get;}
408 public override Type ReflectedType {
414 public virtual RuntimeTypeHandle TypeHandle {
415 get { throw new ArgumentException ("Derived class must provide implementation."); }
419 public ConstructorInfo TypeInitializer {
421 return GetConstructorImpl (
422 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
424 CallingConventions.Any,
431 * This has NOTHING to do with getting the base type of an enum. Use
432 * Enum.GetUnderlyingType () for that.
434 public abstract Type UnderlyingSystemType {get;}
436 public override bool Equals (object o)
439 return Equals (o as Type);
444 Type me = UnderlyingSystemType;
447 return me.EqualsInternal (o as Type);
452 public virtual bool Equals (Type o)
454 if ((object)o == (object)this)
456 if ((object)o == null)
458 Type me = UnderlyingSystemType;
459 if ((object)me == null)
462 o = o.UnderlyingSystemType;
463 if ((object)o == null)
465 if ((object)o == (object)this)
467 return me.EqualsInternal (o);
470 public bool Equals (Type o)
477 Type me = UnderlyingSystemType;
480 return me.EqualsInternal (o.UnderlyingSystemType);
484 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
485 public static bool operator == (Type left, Type right)
487 return Object.ReferenceEquals (left, right);
490 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
491 public static bool operator != (Type left, Type right)
493 return !Object.ReferenceEquals (left, right);
496 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
497 public virtual Type GetEnumUnderlyingType () {
499 throw new ArgumentException ("Type is not an enumeration", "enumType");
501 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
503 if (fields == null || fields.Length != 1)
504 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
506 return fields [0].FieldType;
509 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
510 public virtual string[] GetEnumNames () {
512 throw new ArgumentException ("Type is not an enumeration", "enumType");
514 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
516 string [] names = new string [fields.Length];
517 if (0 != names.Length) {
518 for (int i = 0; i < fields.Length; ++i)
519 names [i] = fields [i].Name;
521 var et = GetEnumUnderlyingType ();
522 var values = Array.CreateInstance (et, names.Length);
523 for (int i = 0; i < fields.Length; ++i)
524 values.SetValue (fields [i].GetValue (null), i);
525 MonoEnumInfo.SortEnums (et, values, names);
531 static NotImplementedException CreateNIE () {
532 return new NotImplementedException ();
535 public virtual Array GetEnumValues () {
537 throw new ArgumentException ("Type is not an enumeration", "enumType");
542 bool IsValidEnumType (Type type) {
543 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
546 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
547 public virtual string GetEnumName (object value) {
549 throw new ArgumentException ("Value is null", "value");
550 if (!IsValidEnumType (value.GetType ()))
551 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
553 throw new ArgumentException ("Type is not an enumeration", "enumType");
556 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
558 for (int i = 0; i < fields.Length; ++i) {
559 var fv = fields [i].GetValue (null);
562 //XXX we can't use 'this' as argument as it might be an UserType
563 obj = Enum.ToObject (fv.GetType (), value);
564 } catch (OverflowException) {
566 } catch (InvalidCastException) {
567 throw new ArgumentException ("Value is not valid", "value");
571 return fields [i].Name;
577 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
578 public virtual bool IsEnumDefined (object value) {
580 throw new ArgumentException ("Value is null", "value");
582 throw new ArgumentException ("Type is not an enumeration", "enumType");
584 Type vt = value.GetType ();
585 if (!IsValidEnumType (vt) && vt != typeof (string))
586 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
588 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
590 if (value is string) {
591 for (int i = 0; i < fields.Length; ++i) {
592 if (fields [i].Name.Equals (value))
596 if (vt != this && vt != GetEnumUnderlyingType ())
597 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
600 for (int i = 0; i < fields.Length; ++i) {
601 var fv = fields [i].GetValue (null);
604 //XXX we can't use 'this' as argument as it might be an UserType
605 obj = Enum.ToObject (fv.GetType (), value);
606 } catch (OverflowException) {
608 } catch (InvalidCastException) {
609 throw new ArgumentException ("Value is not valid", "value");
619 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
621 return GetType (typeName, assemblyResolver, typeResolver, false, false);
624 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
626 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
629 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
631 TypeSpec spec = TypeSpec.Parse (typeName);
632 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
635 public virtual bool IsSecurityTransparent
637 get { throw CreateNIE (); }
640 public virtual bool IsSecurityCritical
642 get { throw CreateNIE (); }
645 public virtual bool IsSecuritySafeCritical
647 get { throw CreateNIE (); }
651 [MethodImplAttribute(MethodImplOptions.InternalCall)]
652 internal extern bool EqualsInternal (Type type);
654 [MethodImplAttribute(MethodImplOptions.InternalCall)]
655 private static extern Type internal_from_handle (IntPtr handle);
657 [MethodImplAttribute(MethodImplOptions.InternalCall)]
658 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
660 public static Type GetType(string typeName)
662 if (typeName == null)
663 throw new ArgumentNullException ("TypeName");
665 return internal_from_name (typeName, false, false);
668 public static Type GetType(string typeName, bool throwOnError)
670 if (typeName == null)
671 throw new ArgumentNullException ("TypeName");
673 Type type = internal_from_name (typeName, throwOnError, false);
674 if (throwOnError && type == null)
675 throw new TypeLoadException ("Error loading '" + typeName + "'");
680 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
682 if (typeName == null)
683 throw new ArgumentNullException ("TypeName");
685 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
686 if (throwOnError && t == null)
687 throw new TypeLoadException ("Error loading '" + typeName + "'");
692 public static Type[] GetTypeArray (object[] args) {
694 throw new ArgumentNullException ("args");
697 ret = new Type [args.Length];
698 for (int i = 0; i < args.Length; ++i)
699 ret [i] = args[i].GetType ();
703 [MethodImplAttribute(MethodImplOptions.InternalCall)]
704 internal extern static TypeCode GetTypeCodeInternal (Type type);
709 TypeCode GetTypeCodeImpl () {
711 if (type is MonoType)
712 return GetTypeCodeInternal (type);
713 #if !FULL_AOT_RUNTIME
714 if (type is TypeBuilder)
715 return ((TypeBuilder)type).GetTypeCodeInternal ();
718 type = type.UnderlyingSystemType;
720 if (!type.IsSystemType)
721 return TypeCode.Object;
723 return GetTypeCodeInternal (type);
726 public static TypeCode GetTypeCode (Type type) {
728 /* MS.NET returns this */
729 return TypeCode.Empty;
730 return type.GetTypeCodeImpl ();
733 [MonoTODO("This operation is currently not supported by Mono")]
734 public static Type GetTypeFromCLSID (Guid clsid)
736 throw new NotImplementedException ();
739 [MonoTODO("This operation is currently not supported by Mono")]
740 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
742 throw new NotImplementedException ();
745 [MonoTODO("This operation is currently not supported by Mono")]
746 public static Type GetTypeFromCLSID (Guid clsid, string server)
748 throw new NotImplementedException ();
751 [MonoTODO("This operation is currently not supported by Mono")]
752 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
754 throw new NotImplementedException ();
757 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
759 if (handle.Value == IntPtr.Zero)
760 // This is not consistent with the other GetXXXFromHandle methods, but
761 // MS.NET seems to do this
764 return internal_from_handle (handle.Value);
767 [MonoTODO("Mono does not support COM")]
768 public static Type GetTypeFromProgID (string progID)
770 throw new NotImplementedException ();
773 [MonoTODO("Mono does not support COM")]
774 public static Type GetTypeFromProgID (string progID, bool throwOnError)
776 throw new NotImplementedException ();
779 [MonoTODO("Mono does not support COM")]
780 public static Type GetTypeFromProgID (string progID, string server)
782 throw new NotImplementedException ();
785 [MonoTODO("Mono does not support COM")]
786 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
788 throw new NotImplementedException ();
791 public static RuntimeTypeHandle GetTypeHandle (object o)
794 throw new ArgumentNullException ();
796 return o.GetType().TypeHandle;
799 [MethodImplAttribute(MethodImplOptions.InternalCall)]
800 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
802 [MethodImplAttribute(MethodImplOptions.InternalCall)]
803 internal static extern bool type_is_assignable_from (Type a, Type b);
805 public new Type GetType ()
807 return base.GetType ();
811 public virtual bool IsSubclassOf (Type c)
813 if (c == null || c == this)
816 // Fast check for system types
818 return c.IsSystemType && type_is_subtype_of (this, c, false);
820 // User defined types depend on this behavior
821 for (Type type = BaseType; type != null; type = type.BaseType)
828 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
831 throw new ArgumentNullException ("filter");
833 var ifaces = new List<Type> ();
834 foreach (Type iface in GetInterfaces ()) {
835 if (filter (iface, filterCriteria))
839 return ifaces.ToArray ();
842 public Type GetInterface (string name) {
843 return GetInterface (name, false);
846 public abstract Type GetInterface (string name, bool ignoreCase);
848 [MethodImplAttribute(MethodImplOptions.InternalCall)]
849 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
852 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
854 throw new NotSupportedException ("Derived classes must provide an implementation.");
855 if (interfaceType == null)
856 throw new ArgumentNullException ("interfaceType");
857 if (!interfaceType.IsSystemType)
858 throw new ArgumentException ("interfaceType", "Type is an user type");
859 InterfaceMapping res;
860 if (!interfaceType.IsInterface)
861 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
863 throw new ArgumentException ("'this' type cannot be an interface itself");
864 res.TargetType = this;
865 res.InterfaceType = interfaceType;
866 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
867 if (res.TargetMethods == null)
868 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
873 public abstract Type[] GetInterfaces ();
875 public virtual bool IsAssignableFrom (Type c)
883 #if !FULL_AOT_RUNTIME
884 if (c is TypeBuilder)
885 return ((TypeBuilder)c).IsAssignableTo (this);
888 /* Handle user defined type classes */
890 Type systemType = UnderlyingSystemType;
891 if (!systemType.IsSystemType)
894 Type other = c.UnderlyingSystemType;
895 if (!other.IsSystemType)
898 return systemType.IsAssignableFrom (other);
901 if (!c.IsSystemType) {
902 Type underlyingType = c.UnderlyingSystemType;
903 if (!underlyingType.IsSystemType)
905 return IsAssignableFrom (underlyingType);
908 return type_is_assignable_from (this, c);
911 [MethodImplAttribute(MethodImplOptions.InternalCall)]
912 extern static bool IsInstanceOfType (Type type, object o);
914 public virtual bool IsInstanceOfType (object o)
916 Type type = UnderlyingSystemType;
917 if (!type.IsSystemType)
919 return IsInstanceOfType (type, o);
922 public virtual int GetArrayRank ()
924 throw new NotSupportedException (); // according to MSDN
927 public abstract Type GetElementType ();
929 public EventInfo GetEvent (string name)
931 return GetEvent (name, DefaultBindingFlags);
934 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
936 public virtual EventInfo[] GetEvents ()
938 return GetEvents (DefaultBindingFlags);
941 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
943 public FieldInfo GetField( string name)
945 return GetField (name, DefaultBindingFlags);
948 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
950 public FieldInfo[] GetFields ()
952 return GetFields (DefaultBindingFlags);
955 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
957 public override int GetHashCode()
959 Type t = UnderlyingSystemType;
960 if (t != null && t != this)
961 return t.GetHashCode ();
962 return (int)_impl.Value;
965 public MemberInfo[] GetMember (string name)
967 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
970 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
972 return GetMember (name, MemberTypes.All, bindingAttr);
975 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
978 throw new ArgumentNullException ("name");
979 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
980 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
982 return FindMembers (type, bindingAttr, FilterName, name);
985 public MemberInfo[] GetMembers ()
987 return GetMembers (DefaultBindingFlags);
990 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
992 public MethodInfo GetMethod (string name)
995 throw new ArgumentNullException ("name");
996 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
999 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
1002 throw new ArgumentNullException ("name");
1004 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1007 public MethodInfo GetMethod (string name, Type[] types)
1009 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1012 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1014 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1017 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1018 Type[] types, ParameterModifier[] modifiers)
1020 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1023 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1024 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1027 throw new ArgumentNullException ("name");
1029 throw new ArgumentNullException ("types");
1031 for (int i = 0; i < types.Length; i++)
1032 if (types[i] == null)
1033 throw new ArgumentNullException ("types");
1035 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1038 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1039 CallingConventions callConvention, Type[] types,
1040 ParameterModifier[] modifiers);
1042 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1043 CallingConventions callConvention, Type[] types,
1044 ParameterModifier[] modifiers)
1046 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1049 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1051 throw new System.InvalidOperationException ("can only be called in generic type");
1054 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1056 throw new System.InvalidOperationException ("can only be called in generic type");
1059 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1061 throw new System.InvalidOperationException ("can only be called in generic type");
1065 public MethodInfo[] GetMethods ()
1067 return GetMethods (DefaultBindingFlags);
1070 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1072 public Type GetNestedType (string name)
1074 return GetNestedType (name, DefaultBindingFlags);
1077 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1079 public Type[] GetNestedTypes ()
1081 return GetNestedTypes (DefaultBindingFlags);
1084 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1087 public PropertyInfo[] GetProperties ()
1089 return GetProperties (DefaultBindingFlags);
1092 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1095 public PropertyInfo GetProperty (string name)
1098 throw new ArgumentNullException ("name");
1100 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1103 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1106 throw new ArgumentNullException ("name");
1107 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1110 public PropertyInfo GetProperty (string name, Type returnType)
1113 throw new ArgumentNullException ("name");
1114 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1117 public PropertyInfo GetProperty (string name, Type[] types)
1119 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1122 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1124 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1127 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1129 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1132 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1133 Type[] types, ParameterModifier[] modifiers)
1136 throw new ArgumentNullException ("name");
1138 throw new ArgumentNullException ("types");
1140 foreach (Type t in types) {
1142 throw new ArgumentNullException ("types");
1145 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1148 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1149 Type returnType, Type[] types, ParameterModifier[] modifiers);
1151 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1152 Type returnType, Type[] types, ParameterModifier[] modifiers)
1154 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1157 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1159 CallingConventions callConvention,
1161 ParameterModifier[] modifiers);
1163 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1164 protected abstract bool HasElementTypeImpl ();
1165 protected abstract bool IsArrayImpl ();
1166 protected abstract bool IsByRefImpl ();
1167 protected abstract bool IsCOMObjectImpl ();
1168 protected abstract bool IsPointerImpl ();
1169 protected abstract bool IsPrimitiveImpl ();
1171 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1172 internal static extern bool IsArrayImpl (Type type);
1174 protected virtual bool IsValueTypeImpl ()
1176 if (this == typeof (ValueType) || this == typeof (Enum))
1179 return IsSubclassOf (typeof (ValueType));
1182 protected virtual bool IsContextfulImpl ()
1184 return typeof (ContextBoundObject).IsAssignableFrom (this);
1187 protected virtual bool IsMarshalByRefImpl ()
1189 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1193 public ConstructorInfo GetConstructor (Type[] types)
1195 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1199 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1200 Type[] types, ParameterModifier[] modifiers)
1202 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1206 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1207 CallingConventions callConvention,
1208 Type[] types, ParameterModifier[] modifiers)
1211 throw new ArgumentNullException ("types");
1213 foreach (Type t in types) {
1215 throw new ArgumentNullException ("types");
1218 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1222 public ConstructorInfo[] GetConstructors ()
1224 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1228 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1230 public virtual MemberInfo[] GetDefaultMembers ()
1232 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1233 if (att.Length == 0)
1234 return new MemberInfo [0];
1236 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1237 return (member != null) ? member : new MemberInfo [0];
1240 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1241 MemberFilter filter, object filterCriteria)
1243 MemberInfo[] result;
1244 ArrayList l = new ArrayList ();
1246 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1247 // this.FullName, this.GetType().FullName, this.obj_address());
1248 if ((memberType & MemberTypes.Method) != 0) {
1249 MethodInfo[] c = GetMethods (bindingAttr);
1250 if (filter != null) {
1251 foreach (MemberInfo m in c) {
1252 if (filter (m, filterCriteria))
1259 if ((memberType & MemberTypes.Constructor) != 0) {
1260 ConstructorInfo[] c = GetConstructors (bindingAttr);
1261 if (filter != null) {
1262 foreach (MemberInfo m in c) {
1263 if (filter (m, filterCriteria))
1270 if ((memberType & MemberTypes.Property) != 0) {
1271 PropertyInfo[] c = GetProperties (bindingAttr);
1274 if (filter != null) {
1275 foreach (MemberInfo m in c) {
1276 if (filter (m, filterCriteria))
1284 if ((memberType & MemberTypes.Event) != 0) {
1285 EventInfo[] c = GetEvents (bindingAttr);
1286 if (filter != null) {
1287 foreach (MemberInfo m in c) {
1288 if (filter (m, filterCriteria))
1295 if ((memberType & MemberTypes.Field) != 0) {
1296 FieldInfo[] c = GetFields (bindingAttr);
1297 if (filter != null) {
1298 foreach (MemberInfo m in c) {
1299 if (filter (m, filterCriteria))
1306 if ((memberType & MemberTypes.NestedType) != 0) {
1307 Type[] c = GetNestedTypes (bindingAttr);
1308 if (filter != null) {
1309 foreach (MemberInfo m in c) {
1310 if (filter (m, filterCriteria)) {
1319 switch (memberType) {
1320 case MemberTypes.Constructor :
1321 result = new ConstructorInfo [l.Count];
1323 case MemberTypes.Event :
1324 result = new EventInfo [l.Count];
1326 case MemberTypes.Field :
1327 result = new FieldInfo [l.Count];
1329 case MemberTypes.Method :
1330 result = new MethodInfo [l.Count];
1332 case MemberTypes.NestedType :
1333 case MemberTypes.TypeInfo :
1334 result = new Type [l.Count];
1336 case MemberTypes.Property :
1337 result = new PropertyInfo [l.Count];
1340 result = new MemberInfo [l.Count];
1348 [DebuggerStepThrough]
1349 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1351 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1355 [DebuggerStepThrough]
1356 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1357 object target, object[] args, CultureInfo culture)
1359 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1362 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1363 Binder binder, object target, object[] args,
1364 ParameterModifier[] modifiers,
1365 CultureInfo culture, string[] namedParameters);
1367 public override string ToString()
1372 internal virtual Type InternalResolve ()
1374 return UnderlyingSystemType;
1377 internal bool IsSystemType {
1379 return _impl.Value != IntPtr.Zero;
1384 public virtual Type[] GenericTypeArguments {
1386 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1391 public virtual Type[] GetGenericArguments ()
1393 throw new NotSupportedException ();
1396 public virtual bool ContainsGenericParameters {
1397 get { return false; }
1400 public virtual extern bool IsGenericTypeDefinition {
1401 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1405 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1406 internal extern Type GetGenericTypeDefinition_impl ();
1408 public virtual Type GetGenericTypeDefinition ()
1410 throw new NotSupportedException ("Derived classes must provide an implementation.");
1413 public virtual extern bool IsGenericType {
1414 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1418 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1419 static extern Type MakeGenericType (Type gt, Type [] types);
1421 public virtual Type MakeGenericType (params Type[] typeArguments)
1424 throw new NotSupportedException ();
1425 if (!IsGenericTypeDefinition)
1426 throw new InvalidOperationException ("not a generic type definition");
1427 if (typeArguments == null)
1428 throw new ArgumentNullException ("typeArguments");
1429 if (GetGenericArguments().Length != typeArguments.Length)
1430 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");
1432 bool hasUserType = false;
1434 Type[] systemTypes = new Type[typeArguments.Length];
1435 for (int i = 0; i < typeArguments.Length; ++i) {
1436 Type t = typeArguments [i];
1438 throw new ArgumentNullException ("typeArguments");
1440 if (!(t is MonoType))
1442 systemTypes [i] = t;
1445 #if !FULL_AOT_RUNTIME
1447 return new MonoGenericClass (this, typeArguments);
1451 Type res = MakeGenericType (this, systemTypes);
1453 throw new TypeLoadException ();
1457 public virtual bool IsGenericParameter {
1463 public bool IsNested {
1465 return DeclaringType != null;
1469 public bool IsVisible {
1472 return DeclaringType.IsVisible;
1478 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1479 extern int GetGenericParameterPosition ();
1481 public virtual int GenericParameterPosition {
1483 int res = GetGenericParameterPosition ();
1485 throw new InvalidOperationException ();
1490 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1491 extern GenericParameterAttributes GetGenericParameterAttributes ();
1493 public virtual GenericParameterAttributes GenericParameterAttributes {
1496 throw new NotSupportedException ("Derived classes must provide an implementation.");
1498 if (!IsGenericParameter)
1499 throw new InvalidOperationException ();
1501 return GetGenericParameterAttributes ();
1505 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1506 extern Type[] GetGenericParameterConstraints_impl ();
1508 public virtual Type[] GetGenericParameterConstraints ()
1511 throw new InvalidOperationException ();
1513 if (!IsGenericParameter)
1514 throw new InvalidOperationException ();
1516 return GetGenericParameterConstraints_impl ();
1519 public virtual MethodBase DeclaringMethod {
1525 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1526 extern Type make_array_type (int rank);
1528 public virtual Type MakeArrayType ()
1531 throw new NotSupportedException ("Derived classes must provide an implementation.");
1532 return make_array_type (0);
1535 public virtual Type MakeArrayType (int rank)
1538 throw new NotSupportedException ("Derived classes must provide an implementation.");
1539 if (rank < 1 || rank > 255)
1540 throw new IndexOutOfRangeException ();
1541 return make_array_type (rank);
1544 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1545 extern Type make_byref_type ();
1547 public virtual Type MakeByRefType ()
1550 throw new NotSupportedException ("Derived classes must provide an implementation.");
1552 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1553 return make_byref_type ();
1556 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1557 static extern Type MakePointerType (Type type);
1559 public virtual Type MakePointerType ()
1562 throw new NotSupportedException ("Derived classes must provide an implementation.");
1563 return MakePointerType (this);
1566 public static Type ReflectionOnlyGetType (string typeName,
1567 bool throwIfNotFound,
1570 if (typeName == null)
1571 throw new ArgumentNullException ("typeName");
1572 int idx = typeName.IndexOf (',');
1573 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1574 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1575 string an = typeName.Substring (idx + 1);
1578 a = Assembly.ReflectionOnlyLoad (an);
1580 if (throwIfNotFound)
1584 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1587 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1588 extern void GetPacking (out int packing, out int size);
1590 public virtual StructLayoutAttribute StructLayoutAttribute {
1593 throw new NotSupportedException ();
1595 return GetStructLayoutAttribute ();
1600 internal StructLayoutAttribute GetStructLayoutAttribute ()
1604 if (IsLayoutSequential)
1605 kind = LayoutKind.Sequential;
1606 else if (IsExplicitLayout)
1607 kind = LayoutKind.Explicit;
1609 kind = LayoutKind.Auto;
1611 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1614 attr.CharSet = CharSet.Unicode;
1615 else if (IsAnsiClass)
1616 attr.CharSet = CharSet.Ansi;
1618 attr.CharSet = CharSet.Auto;
1620 if (kind != LayoutKind.Auto) {
1622 GetPacking (out packing, out attr.Size);
1623 // 0 means no data provided, we end up with default value
1625 attr.Pack = packing;
1631 internal object[] GetPseudoCustomAttributes ()
1635 /* IsSerializable returns true for delegates/enums as well */
1636 if ((Attributes & TypeAttributes.Serializable) != 0)
1638 if ((Attributes & TypeAttributes.Import) != 0)
1643 object[] attrs = new object [count];
1646 if ((Attributes & TypeAttributes.Serializable) != 0)
1647 attrs [count ++] = new SerializableAttribute ();
1648 if ((Attributes & TypeAttributes.Import) != 0)
1649 attrs [count ++] = new ComImportAttribute ();
1656 public virtual bool IsEquivalentTo (Type other)
1658 return this == other;
1663 * Return whenever this object is an instance of a user defined subclass
1664 * of System.Type or an instance of TypeDelegator.
1665 * A user defined type is not simply the opposite of a system type.
1666 * It's any class that's neither a SRE or runtime baked type.
1668 internal virtual bool IsUserType {
1674 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1676 throw new NotImplementedException ();
1679 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1681 throw new NotImplementedException ();
1684 void _Type.GetTypeInfoCount (out uint pcTInfo)
1686 throw new NotImplementedException ();
1689 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1691 throw new NotImplementedException ();