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 if (!(filterCriteria is int))
98 throw new InvalidFilterCriteriaException ("Int32 value is expected for filter criteria");
100 int flags = (int) filterCriteria;
102 return ((int)((MethodInfo)m).Attributes & flags) != 0;
104 return ((int)((FieldInfo)m).Attributes & flags) != 0;
105 if (m is PropertyInfo)
106 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
108 return ((int)((EventInfo)m).Attributes & flags) != 0;
117 /// The assembly where the type is defined.
119 public abstract Assembly Assembly {
124 /// Gets the fully qualified name for the type including the
125 /// assembly name where the type is defined.
127 public abstract string AssemblyQualifiedName {
132 /// Returns the Attributes associated with the type.
134 public TypeAttributes Attributes {
136 return GetAttributeFlagsImpl ();
141 /// Returns the basetype for this type
143 public abstract Type BaseType {
148 /// Returns the class that declares the member.
150 public override Type DeclaringType {
159 public static Binder DefaultBinder {
161 return Binder.DefaultBinder;
166 /// The full name of the type including its namespace
168 public abstract string FullName {
172 public abstract Guid GUID {
176 public bool HasElementType {
178 return HasElementTypeImpl ();
182 public bool IsAbstract {
184 return (Attributes & TypeAttributes.Abstract) != 0;
188 public bool IsAnsiClass {
190 return (Attributes & TypeAttributes.StringFormatMask)
191 == TypeAttributes.AnsiClass;
195 public bool IsArray {
197 return IsArrayImpl ();
201 public bool IsAutoClass {
203 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
207 public bool IsAutoLayout {
209 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
213 public bool IsByRef {
215 return IsByRefImpl ();
219 public bool IsClass {
228 public bool IsCOMObject {
230 return IsCOMObjectImpl ();
235 public virtual bool IsConstructedGenericType {
237 throw new NotImplementedException ();
242 public bool IsContextful {
244 return IsContextfulImpl ();
254 return IsSubclassOf (typeof (Enum));
258 public bool IsExplicitLayout {
260 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
264 public bool IsImport {
266 return (Attributes & TypeAttributes.Import) != 0;
270 public bool IsInterface {
272 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
276 public bool IsLayoutSequential {
278 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
282 public bool IsMarshalByRef {
284 return IsMarshalByRefImpl ();
288 public bool IsNestedAssembly {
290 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
294 public bool IsNestedFamANDAssem {
296 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
300 public bool IsNestedFamily {
302 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
306 public bool IsNestedFamORAssem {
308 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
312 public bool IsNestedPrivate {
314 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
318 public bool IsNestedPublic {
320 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
324 public bool IsNotPublic {
326 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
330 public bool IsPointer {
332 return IsPointerImpl ();
336 public bool IsPrimitive {
338 return IsPrimitiveImpl ();
342 public bool IsPublic {
344 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
348 public bool IsSealed {
350 return (Attributes & TypeAttributes.Sealed) != 0;
358 bool IsSerializable {
360 if ((Attributes & TypeAttributes.Serializable) != 0)
363 // Enums and delegates are always serializable
365 Type type = UnderlyingSystemType;
369 // Fast check for system types
370 if (type.IsSystemType)
371 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
373 // User defined types depend on this behavior
375 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
378 type = type.BaseType;
379 } while (type != null);
385 public bool IsSpecialName {
387 return (Attributes & TypeAttributes.SpecialName) != 0;
391 public bool IsUnicodeClass {
393 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
397 public bool IsValueType {
399 return IsValueTypeImpl ();
403 public override MemberTypes MemberType {
405 return MemberTypes.TypeInfo;
409 public abstract override Module Module {
413 public abstract string Namespace {get;}
415 public override Type ReflectedType {
421 public virtual RuntimeTypeHandle TypeHandle {
422 get { throw new ArgumentException ("Derived class must provide implementation."); }
426 public ConstructorInfo TypeInitializer {
428 return GetConstructorImpl (
429 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
431 CallingConventions.Any,
438 * This has NOTHING to do with getting the base type of an enum. Use
439 * Enum.GetUnderlyingType () for that.
441 public abstract Type UnderlyingSystemType {get;}
443 public override bool Equals (object o)
446 return Equals (o as Type);
451 Type me = UnderlyingSystemType;
454 return me.EqualsInternal (o as Type);
459 public virtual bool Equals (Type o)
461 if ((object)o == (object)this)
463 if ((object)o == null)
465 Type me = UnderlyingSystemType;
466 if ((object)me == null)
469 o = o.UnderlyingSystemType;
470 if ((object)o == null)
472 if ((object)o == (object)this)
474 return me.EqualsInternal (o);
477 public bool Equals (Type o)
484 Type me = UnderlyingSystemType;
487 return me.EqualsInternal (o.UnderlyingSystemType);
491 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
492 public static bool operator == (Type left, Type right)
494 return Object.ReferenceEquals (left, right);
497 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
498 public static bool operator != (Type left, Type right)
500 return !Object.ReferenceEquals (left, right);
503 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
504 public virtual Type GetEnumUnderlyingType () {
506 throw new ArgumentException ("Type is not an enumeration", "enumType");
508 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
510 if (fields == null || fields.Length != 1)
511 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
513 return fields [0].FieldType;
516 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
517 public virtual string[] GetEnumNames () {
519 throw new ArgumentException ("Type is not an enumeration", "enumType");
521 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
523 string [] names = new string [fields.Length];
524 if (0 != names.Length) {
525 for (int i = 0; i < fields.Length; ++i)
526 names [i] = fields [i].Name;
528 var et = GetEnumUnderlyingType ();
529 var values = Array.CreateInstance (et, names.Length);
530 for (int i = 0; i < fields.Length; ++i)
531 values.SetValue (fields [i].GetValue (null), i);
532 MonoEnumInfo.SortEnums (et, values, names);
538 static NotImplementedException CreateNIE () {
539 return new NotImplementedException ();
542 public virtual Array GetEnumValues () {
544 throw new ArgumentException ("Type is not an enumeration", "enumType");
549 bool IsValidEnumType (Type type) {
550 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
553 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
554 public virtual string GetEnumName (object value) {
556 throw new ArgumentException ("Value is null", "value");
557 if (!IsValidEnumType (value.GetType ()))
558 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
560 throw new ArgumentException ("Type is not an enumeration", "enumType");
563 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
565 for (int i = 0; i < fields.Length; ++i) {
566 var fv = fields [i].GetValue (null);
569 //XXX we can't use 'this' as argument as it might be an UserType
570 obj = Enum.ToObject (fv.GetType (), value);
571 } catch (OverflowException) {
573 } catch (InvalidCastException) {
574 throw new ArgumentException ("Value is not valid", "value");
578 return fields [i].Name;
584 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
585 public virtual bool IsEnumDefined (object value) {
587 throw new ArgumentException ("Value is null", "value");
589 throw new ArgumentException ("Type is not an enumeration", "enumType");
591 Type vt = value.GetType ();
592 if (!IsValidEnumType (vt) && vt != typeof (string))
593 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
595 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
597 if (value is string) {
598 for (int i = 0; i < fields.Length; ++i) {
599 if (fields [i].Name.Equals (value))
603 if (vt != this && vt != GetEnumUnderlyingType ())
604 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
607 for (int i = 0; i < fields.Length; ++i) {
608 var fv = fields [i].GetValue (null);
611 //XXX we can't use 'this' as argument as it might be an UserType
612 obj = Enum.ToObject (fv.GetType (), value);
613 } catch (OverflowException) {
615 } catch (InvalidCastException) {
616 throw new ArgumentException ("Value is not valid", "value");
626 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
628 return GetType (typeName, assemblyResolver, typeResolver, false, false);
631 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
633 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
636 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
638 TypeSpec spec = TypeSpec.Parse (typeName);
639 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
642 public virtual bool IsSecurityTransparent
644 get { throw CreateNIE (); }
647 public virtual bool IsSecurityCritical
649 get { throw CreateNIE (); }
652 public virtual bool IsSecuritySafeCritical
654 get { throw CreateNIE (); }
658 [MethodImplAttribute(MethodImplOptions.InternalCall)]
659 internal extern bool EqualsInternal (Type type);
661 [MethodImplAttribute(MethodImplOptions.InternalCall)]
662 private static extern Type internal_from_handle (IntPtr handle);
664 [MethodImplAttribute(MethodImplOptions.InternalCall)]
665 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
667 public static Type GetType(string typeName)
669 if (typeName == null)
670 throw new ArgumentNullException ("TypeName");
672 return internal_from_name (typeName, false, false);
675 public static Type GetType(string typeName, bool throwOnError)
677 if (typeName == null)
678 throw new ArgumentNullException ("TypeName");
680 Type type = internal_from_name (typeName, throwOnError, false);
681 if (throwOnError && type == null)
682 throw new TypeLoadException ("Error loading '" + typeName + "'");
687 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
689 if (typeName == null)
690 throw new ArgumentNullException ("TypeName");
692 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
693 if (throwOnError && t == null)
694 throw new TypeLoadException ("Error loading '" + typeName + "'");
699 public static Type[] GetTypeArray (object[] args) {
701 throw new ArgumentNullException ("args");
704 ret = new Type [args.Length];
705 for (int i = 0; i < args.Length; ++i)
706 ret [i] = args[i].GetType ();
710 [MethodImplAttribute(MethodImplOptions.InternalCall)]
711 internal extern static TypeCode GetTypeCodeInternal (Type type);
716 TypeCode GetTypeCodeImpl () {
718 if (type is MonoType)
719 return GetTypeCodeInternal (type);
720 #if !FULL_AOT_RUNTIME
721 if (type is TypeBuilder)
722 return ((TypeBuilder)type).GetTypeCodeInternal ();
725 type = type.UnderlyingSystemType;
727 if (!type.IsSystemType)
728 return TypeCode.Object;
730 return GetTypeCodeInternal (type);
733 public static TypeCode GetTypeCode (Type type) {
735 /* MS.NET returns this */
736 return TypeCode.Empty;
737 return type.GetTypeCodeImpl ();
740 [MonoTODO("This operation is currently not supported by Mono")]
741 public static Type GetTypeFromCLSID (Guid clsid)
743 throw new NotImplementedException ();
746 [MonoTODO("This operation is currently not supported by Mono")]
747 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
749 throw new NotImplementedException ();
752 [MonoTODO("This operation is currently not supported by Mono")]
753 public static Type GetTypeFromCLSID (Guid clsid, string server)
755 throw new NotImplementedException ();
758 [MonoTODO("This operation is currently not supported by Mono")]
759 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
761 throw new NotImplementedException ();
764 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
766 if (handle.Value == IntPtr.Zero)
767 // This is not consistent with the other GetXXXFromHandle methods, but
768 // MS.NET seems to do this
771 return internal_from_handle (handle.Value);
774 [MonoTODO("Mono does not support COM")]
775 public static Type GetTypeFromProgID (string progID)
777 throw new NotImplementedException ();
780 [MonoTODO("Mono does not support COM")]
781 public static Type GetTypeFromProgID (string progID, bool throwOnError)
783 throw new NotImplementedException ();
786 [MonoTODO("Mono does not support COM")]
787 public static Type GetTypeFromProgID (string progID, string server)
789 throw new NotImplementedException ();
792 [MonoTODO("Mono does not support COM")]
793 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
795 throw new NotImplementedException ();
798 public static RuntimeTypeHandle GetTypeHandle (object o)
801 throw new ArgumentNullException ();
803 return o.GetType().TypeHandle;
806 [MethodImplAttribute(MethodImplOptions.InternalCall)]
807 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
809 [MethodImplAttribute(MethodImplOptions.InternalCall)]
810 internal static extern bool type_is_assignable_from (Type a, Type b);
812 public new Type GetType ()
814 return base.GetType ();
818 public virtual bool IsSubclassOf (Type c)
820 if (c == null || c == this)
823 // Fast check for system types
825 return c.IsSystemType && type_is_subtype_of (this, c, false);
827 // User defined types depend on this behavior
828 for (Type type = BaseType; type != null; type = type.BaseType)
835 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
838 throw new ArgumentNullException ("filter");
840 var ifaces = new List<Type> ();
841 foreach (Type iface in GetInterfaces ()) {
842 if (filter (iface, filterCriteria))
846 return ifaces.ToArray ();
849 public Type GetInterface (string name) {
850 return GetInterface (name, false);
853 public abstract Type GetInterface (string name, bool ignoreCase);
855 [MethodImplAttribute(MethodImplOptions.InternalCall)]
856 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
859 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
861 throw new NotSupportedException ("Derived classes must provide an implementation.");
862 if (interfaceType == null)
863 throw new ArgumentNullException ("interfaceType");
864 if (!interfaceType.IsSystemType)
865 throw new ArgumentException ("interfaceType", "Type is an user type");
866 InterfaceMapping res;
867 if (!interfaceType.IsInterface)
868 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
870 throw new ArgumentException ("'this' type cannot be an interface itself");
871 res.TargetType = this;
872 res.InterfaceType = interfaceType;
873 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
874 if (res.TargetMethods == null)
875 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
880 public abstract Type[] GetInterfaces ();
882 public virtual bool IsAssignableFrom (Type c)
890 #if !FULL_AOT_RUNTIME
891 if (c is TypeBuilder)
892 return ((TypeBuilder)c).IsAssignableTo (this);
895 /* Handle user defined type classes */
897 Type systemType = UnderlyingSystemType;
898 if (!systemType.IsSystemType)
901 Type other = c.UnderlyingSystemType;
902 if (!other.IsSystemType)
905 return systemType.IsAssignableFrom (other);
908 if (!c.IsSystemType) {
909 Type underlyingType = c.UnderlyingSystemType;
910 if (!underlyingType.IsSystemType)
912 return IsAssignableFrom (underlyingType);
915 return type_is_assignable_from (this, c);
918 [MethodImplAttribute(MethodImplOptions.InternalCall)]
919 extern static bool IsInstanceOfType (Type type, object o);
921 public virtual bool IsInstanceOfType (object o)
923 Type type = UnderlyingSystemType;
924 if (!type.IsSystemType)
926 return IsInstanceOfType (type, o);
929 public virtual int GetArrayRank ()
931 throw new NotSupportedException (); // according to MSDN
934 public abstract Type GetElementType ();
936 public EventInfo GetEvent (string name)
938 return GetEvent (name, DefaultBindingFlags);
941 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
943 public virtual EventInfo[] GetEvents ()
945 return GetEvents (DefaultBindingFlags);
948 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
950 public FieldInfo GetField( string name)
952 return GetField (name, DefaultBindingFlags);
955 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
957 public FieldInfo[] GetFields ()
959 return GetFields (DefaultBindingFlags);
962 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
964 public override int GetHashCode()
966 Type t = UnderlyingSystemType;
967 if (t != null && t != this)
968 return t.GetHashCode ();
969 return (int)_impl.Value;
972 public MemberInfo[] GetMember (string name)
974 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
977 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
979 return GetMember (name, MemberTypes.All, bindingAttr);
982 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
985 throw new ArgumentNullException ("name");
986 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
987 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
989 return FindMembers (type, bindingAttr, FilterName, name);
992 public MemberInfo[] GetMembers ()
994 return GetMembers (DefaultBindingFlags);
997 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
999 public MethodInfo GetMethod (string name)
1002 throw new ArgumentNullException ("name");
1003 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
1006 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
1009 throw new ArgumentNullException ("name");
1011 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1014 public MethodInfo GetMethod (string name, Type[] types)
1016 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1019 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1021 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1024 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1025 Type[] types, ParameterModifier[] modifiers)
1027 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1030 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1031 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1034 throw new ArgumentNullException ("name");
1036 throw new ArgumentNullException ("types");
1038 for (int i = 0; i < types.Length; i++)
1039 if (types[i] == null)
1040 throw new ArgumentNullException ("types");
1042 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1045 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1046 CallingConventions callConvention, Type[] types,
1047 ParameterModifier[] modifiers);
1049 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1050 CallingConventions callConvention, Type[] types,
1051 ParameterModifier[] modifiers)
1053 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1056 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1058 throw new System.InvalidOperationException ("can only be called in generic type");
1061 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1063 throw new System.InvalidOperationException ("can only be called in generic type");
1066 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1068 throw new System.InvalidOperationException ("can only be called in generic type");
1072 public MethodInfo[] GetMethods ()
1074 return GetMethods (DefaultBindingFlags);
1077 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1079 public Type GetNestedType (string name)
1081 return GetNestedType (name, DefaultBindingFlags);
1084 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1086 public Type[] GetNestedTypes ()
1088 return GetNestedTypes (DefaultBindingFlags);
1091 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1094 public PropertyInfo[] GetProperties ()
1096 return GetProperties (DefaultBindingFlags);
1099 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1102 public PropertyInfo GetProperty (string name)
1105 throw new ArgumentNullException ("name");
1107 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1110 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1113 throw new ArgumentNullException ("name");
1114 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1117 public PropertyInfo GetProperty (string name, Type returnType)
1120 throw new ArgumentNullException ("name");
1121 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1124 public PropertyInfo GetProperty (string name, Type[] types)
1126 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1129 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1131 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1134 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1136 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1139 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1140 Type[] types, ParameterModifier[] modifiers)
1143 throw new ArgumentNullException ("name");
1145 throw new ArgumentNullException ("types");
1147 foreach (Type t in types) {
1149 throw new ArgumentNullException ("types");
1152 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1155 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1156 Type returnType, Type[] types, ParameterModifier[] modifiers);
1158 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1159 Type returnType, Type[] types, ParameterModifier[] modifiers)
1161 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1164 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1166 CallingConventions callConvention,
1168 ParameterModifier[] modifiers);
1170 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1171 protected abstract bool HasElementTypeImpl ();
1172 protected abstract bool IsArrayImpl ();
1173 protected abstract bool IsByRefImpl ();
1174 protected abstract bool IsCOMObjectImpl ();
1175 protected abstract bool IsPointerImpl ();
1176 protected abstract bool IsPrimitiveImpl ();
1178 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1179 internal static extern bool IsArrayImpl (Type type);
1181 protected virtual bool IsValueTypeImpl ()
1183 if (this == typeof (ValueType) || this == typeof (Enum))
1186 return IsSubclassOf (typeof (ValueType));
1189 protected virtual bool IsContextfulImpl ()
1191 return typeof (ContextBoundObject).IsAssignableFrom (this);
1194 protected virtual bool IsMarshalByRefImpl ()
1196 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1200 public ConstructorInfo GetConstructor (Type[] types)
1202 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1206 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1207 Type[] types, ParameterModifier[] modifiers)
1209 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1213 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1214 CallingConventions callConvention,
1215 Type[] types, ParameterModifier[] modifiers)
1218 throw new ArgumentNullException ("types");
1220 foreach (Type t in types) {
1222 throw new ArgumentNullException ("types");
1225 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1229 public ConstructorInfo[] GetConstructors ()
1231 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1235 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1237 public virtual MemberInfo[] GetDefaultMembers ()
1239 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1240 if (att.Length == 0)
1241 return EmptyArray<MemberInfo>.Value;
1243 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1244 return (member != null) ? member : EmptyArray<MemberInfo>.Value;
1247 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1248 MemberFilter filter, object filterCriteria)
1250 MemberInfo[] result;
1251 ArrayList l = new ArrayList ();
1253 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1254 // this.FullName, this.GetType().FullName, this.obj_address());
1255 if ((memberType & MemberTypes.Method) != 0) {
1256 MethodInfo[] c = GetMethods (bindingAttr);
1257 if (filter != null) {
1258 foreach (MemberInfo m in c) {
1259 if (filter (m, filterCriteria))
1266 if ((memberType & MemberTypes.Constructor) != 0) {
1267 ConstructorInfo[] c = GetConstructors (bindingAttr);
1268 if (filter != null) {
1269 foreach (MemberInfo m in c) {
1270 if (filter (m, filterCriteria))
1277 if ((memberType & MemberTypes.Property) != 0) {
1278 PropertyInfo[] c = GetProperties (bindingAttr);
1281 if (filter != null) {
1282 foreach (MemberInfo m in c) {
1283 if (filter (m, filterCriteria))
1291 if ((memberType & MemberTypes.Event) != 0) {
1292 EventInfo[] c = GetEvents (bindingAttr);
1293 if (filter != null) {
1294 foreach (MemberInfo m in c) {
1295 if (filter (m, filterCriteria))
1302 if ((memberType & MemberTypes.Field) != 0) {
1303 FieldInfo[] c = GetFields (bindingAttr);
1304 if (filter != null) {
1305 foreach (MemberInfo m in c) {
1306 if (filter (m, filterCriteria))
1313 if ((memberType & MemberTypes.NestedType) != 0) {
1314 Type[] c = GetNestedTypes (bindingAttr);
1315 if (filter != null) {
1316 foreach (MemberInfo m in c) {
1317 if (filter (m, filterCriteria)) {
1326 switch (memberType) {
1327 case MemberTypes.Constructor :
1328 result = new ConstructorInfo [l.Count];
1330 case MemberTypes.Event :
1331 result = new EventInfo [l.Count];
1333 case MemberTypes.Field :
1334 result = new FieldInfo [l.Count];
1336 case MemberTypes.Method :
1337 result = new MethodInfo [l.Count];
1339 case MemberTypes.NestedType :
1340 case MemberTypes.TypeInfo :
1341 result = new Type [l.Count];
1343 case MemberTypes.Property :
1344 result = new PropertyInfo [l.Count];
1347 result = new MemberInfo [l.Count];
1355 [DebuggerStepThrough]
1356 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1358 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1362 [DebuggerStepThrough]
1363 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1364 object target, object[] args, CultureInfo culture)
1366 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1369 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1370 Binder binder, object target, object[] args,
1371 ParameterModifier[] modifiers,
1372 CultureInfo culture, string[] namedParameters);
1374 public override string ToString()
1379 internal virtual Type InternalResolve ()
1381 return UnderlyingSystemType;
1384 internal bool IsSystemType {
1386 return _impl.Value != IntPtr.Zero;
1391 public virtual Type[] GenericTypeArguments {
1393 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1398 public virtual Type[] GetGenericArguments ()
1400 throw new NotSupportedException ();
1403 public virtual bool ContainsGenericParameters {
1404 get { return false; }
1407 public virtual extern bool IsGenericTypeDefinition {
1408 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1412 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1413 internal extern Type GetGenericTypeDefinition_impl ();
1415 public virtual Type GetGenericTypeDefinition ()
1417 throw new NotSupportedException ("Derived classes must provide an implementation.");
1420 public virtual extern bool IsGenericType {
1421 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1425 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1426 static extern Type MakeGenericType (Type gt, Type [] types);
1428 public virtual Type MakeGenericType (params Type[] typeArguments)
1431 throw new NotSupportedException ();
1432 if (!IsGenericTypeDefinition)
1433 throw new InvalidOperationException ("not a generic type definition");
1434 if (typeArguments == null)
1435 throw new ArgumentNullException ("typeArguments");
1436 if (GetGenericArguments().Length != typeArguments.Length)
1437 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");
1439 bool hasUserType = false;
1441 Type[] systemTypes = new Type[typeArguments.Length];
1442 for (int i = 0; i < typeArguments.Length; ++i) {
1443 Type t = typeArguments [i];
1445 throw new ArgumentNullException ("typeArguments");
1447 if (!(t is MonoType))
1449 systemTypes [i] = t;
1453 #if FULL_AOT_RUNTIME
1454 throw new NotSupportedException ("User types are not supported under full aot");
1456 return new MonoGenericClass (this, typeArguments);
1460 Type res = MakeGenericType (this, systemTypes);
1462 throw new TypeLoadException ();
1466 public virtual bool IsGenericParameter {
1472 public bool IsNested {
1474 return DeclaringType != null;
1478 public bool IsVisible {
1481 return DeclaringType.IsVisible;
1487 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1488 extern int GetGenericParameterPosition ();
1490 public virtual int GenericParameterPosition {
1492 int res = GetGenericParameterPosition ();
1494 throw new InvalidOperationException ();
1499 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1500 extern GenericParameterAttributes GetGenericParameterAttributes ();
1502 public virtual GenericParameterAttributes GenericParameterAttributes {
1505 throw new NotSupportedException ("Derived classes must provide an implementation.");
1507 if (!IsGenericParameter)
1508 throw new InvalidOperationException ();
1510 return GetGenericParameterAttributes ();
1514 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1515 extern Type[] GetGenericParameterConstraints_impl ();
1517 public virtual Type[] GetGenericParameterConstraints ()
1520 throw new InvalidOperationException ();
1522 if (!IsGenericParameter)
1523 throw new InvalidOperationException ();
1525 return GetGenericParameterConstraints_impl ();
1528 public virtual MethodBase DeclaringMethod {
1534 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1535 extern Type make_array_type (int rank);
1537 public virtual Type MakeArrayType ()
1540 throw new NotSupportedException ("Derived classes must provide an implementation.");
1541 return make_array_type (0);
1544 public virtual Type MakeArrayType (int rank)
1547 throw new NotSupportedException ("Derived classes must provide an implementation.");
1548 if (rank < 1 || rank > 255)
1549 throw new IndexOutOfRangeException ();
1550 return make_array_type (rank);
1553 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1554 extern Type make_byref_type ();
1556 public virtual Type MakeByRefType ()
1559 throw new NotSupportedException ("Derived classes must provide an implementation.");
1561 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1562 return make_byref_type ();
1565 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1566 static extern Type MakePointerType (Type type);
1568 public virtual Type MakePointerType ()
1571 throw new NotSupportedException ("Derived classes must provide an implementation.");
1572 return MakePointerType (this);
1575 public static Type ReflectionOnlyGetType (string typeName,
1576 bool throwIfNotFound,
1579 if (typeName == null)
1580 throw new ArgumentNullException ("typeName");
1581 int idx = typeName.IndexOf (',');
1582 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1583 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1584 string an = typeName.Substring (idx + 1);
1587 a = Assembly.ReflectionOnlyLoad (an);
1589 if (throwIfNotFound)
1593 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1596 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1597 extern void GetPacking (out int packing, out int size);
1599 public virtual StructLayoutAttribute StructLayoutAttribute {
1602 throw new NotSupportedException ();
1604 return GetStructLayoutAttribute ();
1609 internal StructLayoutAttribute GetStructLayoutAttribute ()
1613 if (IsLayoutSequential)
1614 kind = LayoutKind.Sequential;
1615 else if (IsExplicitLayout)
1616 kind = LayoutKind.Explicit;
1618 kind = LayoutKind.Auto;
1620 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1623 attr.CharSet = CharSet.Unicode;
1624 else if (IsAnsiClass)
1625 attr.CharSet = CharSet.Ansi;
1627 attr.CharSet = CharSet.Auto;
1629 if (kind != LayoutKind.Auto) {
1631 GetPacking (out packing, out attr.Size);
1632 // 0 means no data provided, we end up with default value
1634 attr.Pack = packing;
1640 internal object[] GetPseudoCustomAttributes ()
1644 /* IsSerializable returns true for delegates/enums as well */
1645 if ((Attributes & TypeAttributes.Serializable) != 0)
1647 if ((Attributes & TypeAttributes.Import) != 0)
1652 object[] attrs = new object [count];
1655 if ((Attributes & TypeAttributes.Serializable) != 0)
1656 attrs [count ++] = new SerializableAttribute ();
1657 if ((Attributes & TypeAttributes.Import) != 0)
1658 attrs [count ++] = new ComImportAttribute ();
1665 public virtual bool IsEquivalentTo (Type other)
1667 return this == other;
1672 * Return whenever this object is an instance of a user defined subclass
1673 * of System.Type or an instance of TypeDelegator.
1674 * A user defined type is not simply the opposite of a system type.
1675 * It's any class that's neither a SRE or runtime baked type.
1677 internal virtual bool IsUserType {
1684 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1686 throw new NotImplementedException ();
1689 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1691 throw new NotImplementedException ();
1694 void _Type.GetTypeInfoCount (out uint pcTInfo)
1696 throw new NotImplementedException ();
1699 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1701 throw new NotImplementedException ();