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.Threading;
44 using System.Globalization;
49 [ClassInterface (ClassInterfaceType.None)]
51 [ComDefaultInterface (typeof (_Type))]
52 [StructLayout (LayoutKind.Sequential)]
54 public abstract class Type : MemberInfo, IReflect {
56 public abstract class Type : MemberInfo, IReflect, _Type {
59 internal RuntimeTypeHandle _impl;
61 public static readonly char Delimiter = '.';
62 public static readonly Type[] EmptyTypes = {};
63 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
64 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
65 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
66 public static readonly object Missing = System.Reflection.Missing.Value;
68 internal const BindingFlags DefaultBindingFlags =
69 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
71 /* implementation of the delegates for MemberFilter */
72 static bool FilterName_impl (MemberInfo m, object filterCriteria)
74 string name = (string) filterCriteria;
75 if (name == null || name.Length == 0 )
76 return false; // because m.Name cannot be null or empty
78 if (name [name.Length - 1] == '*')
79 return m.Name.StartsWithOrdinalUnchecked (name.Substring (0, name.Length - 1));
81 return m.Name == name;
84 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
86 string name = (string) filterCriteria;
87 if (name == null || name.Length == 0 )
88 return false; // because m.Name cannot be null or empty
90 if (name [name.Length - 1] == '*')
91 return m.Name.StartsWithOrdinalCaseInsensitiveUnchecked (name.Substring (0, name.Length - 1));
93 return string.CompareOrdinalCaseInsensitiveUnchecked (m.Name, name) == 0;
96 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
98 if (!(filterCriteria is int))
99 throw new InvalidFilterCriteriaException ("Int32 value is expected for filter criteria");
101 int flags = (int) filterCriteria;
103 return ((int)((MethodInfo)m).Attributes & flags) != 0;
105 return ((int)((FieldInfo)m).Attributes & flags) != 0;
106 if (m is PropertyInfo)
107 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
109 return ((int)((EventInfo)m).Attributes & flags) != 0;
118 /// The assembly where the type is defined.
120 public abstract Assembly Assembly {
125 /// Gets the fully qualified name for the type including the
126 /// assembly name where the type is defined.
128 public abstract string AssemblyQualifiedName {
133 /// Returns the Attributes associated with the type.
135 public TypeAttributes Attributes {
137 return GetAttributeFlagsImpl ();
142 /// Returns the basetype for this type
144 public abstract Type BaseType {
149 /// Returns the class that declares the member.
151 public override Type DeclaringType {
160 public static Binder DefaultBinder {
162 return Binder.DefaultBinder;
167 /// The full name of the type including its namespace
169 public abstract string FullName {
173 public abstract Guid GUID {
177 public bool HasElementType {
179 return HasElementTypeImpl ();
183 public bool IsAbstract {
185 return (Attributes & TypeAttributes.Abstract) != 0;
189 public bool IsAnsiClass {
191 return (Attributes & TypeAttributes.StringFormatMask)
192 == TypeAttributes.AnsiClass;
196 public bool IsArray {
198 return IsArrayImpl ();
202 public bool IsAutoClass {
204 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
208 public bool IsAutoLayout {
210 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
214 public bool IsByRef {
216 return IsByRefImpl ();
220 public bool IsClass {
229 public bool IsCOMObject {
231 return IsCOMObjectImpl ();
236 public virtual bool IsConstructedGenericType {
238 throw new NotImplementedException ();
243 public bool IsContextful {
245 return IsContextfulImpl ();
255 return IsSubclassOf (typeof (Enum));
259 public bool IsExplicitLayout {
261 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
265 public bool IsImport {
267 return (Attributes & TypeAttributes.Import) != 0;
271 public bool IsInterface {
273 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
277 public bool IsLayoutSequential {
279 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
283 public bool IsMarshalByRef {
285 return IsMarshalByRefImpl ();
289 public bool IsNestedAssembly {
291 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
295 public bool IsNestedFamANDAssem {
297 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
301 public bool IsNestedFamily {
303 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
307 public bool IsNestedFamORAssem {
309 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
313 public bool IsNestedPrivate {
315 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
319 public bool IsNestedPublic {
321 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
325 public bool IsNotPublic {
327 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
331 public bool IsPointer {
333 return IsPointerImpl ();
337 public bool IsPrimitive {
339 return IsPrimitiveImpl ();
343 public bool IsPublic {
345 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
349 public bool IsSealed {
351 return (Attributes & TypeAttributes.Sealed) != 0;
359 bool IsSerializable {
361 if ((Attributes & TypeAttributes.Serializable) != 0)
364 // Enums and delegates are always serializable
366 Type type = UnderlyingSystemType;
370 // Fast check for system types
371 if (type.IsSystemType)
372 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
374 // User defined types depend on this behavior
376 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
379 type = type.BaseType;
380 } while (type != null);
386 public bool IsSpecialName {
388 return (Attributes & TypeAttributes.SpecialName) != 0;
392 public bool IsUnicodeClass {
394 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
398 public bool IsValueType {
400 return IsValueTypeImpl ();
404 public override MemberTypes MemberType {
406 return MemberTypes.TypeInfo;
410 public abstract override Module Module {
414 public abstract string Namespace {get;}
416 public override Type ReflectedType {
422 public virtual RuntimeTypeHandle TypeHandle {
423 get { throw new ArgumentException ("Derived class must provide implementation."); }
427 public ConstructorInfo TypeInitializer {
429 return GetConstructorImpl (
430 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
432 CallingConventions.Any,
439 * This has NOTHING to do with getting the base type of an enum. Use
440 * Enum.GetUnderlyingType () for that.
442 public abstract Type UnderlyingSystemType {get;}
444 public override bool Equals (object o)
447 return Equals (o as Type);
452 Type me = UnderlyingSystemType;
455 return me.EqualsInternal (o as Type);
460 public virtual bool Equals (Type o)
462 if ((object)o == (object)this)
464 if ((object)o == null)
466 Type me = UnderlyingSystemType;
467 if ((object)me == null)
470 o = o.UnderlyingSystemType;
471 if ((object)o == null)
473 if ((object)o == (object)this)
475 return me.EqualsInternal (o);
478 public bool Equals (Type o)
485 Type me = UnderlyingSystemType;
488 return me.EqualsInternal (o.UnderlyingSystemType);
492 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
493 public static bool operator == (Type left, Type right)
495 return Object.ReferenceEquals (left, right);
498 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
499 public static bool operator != (Type left, Type right)
501 return !Object.ReferenceEquals (left, right);
504 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
505 public virtual Type GetEnumUnderlyingType () {
507 throw new ArgumentException ("Type is not an enumeration", "enumType");
509 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
511 if (fields == null || fields.Length != 1)
512 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
514 return fields [0].FieldType;
517 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
518 public virtual string[] GetEnumNames () {
520 throw new ArgumentException ("Type is not an enumeration", "enumType");
522 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
524 string [] names = new string [fields.Length];
525 if (0 != names.Length) {
526 for (int i = 0; i < fields.Length; ++i)
527 names [i] = fields [i].Name;
529 var et = GetEnumUnderlyingType ();
530 var values = Array.CreateInstance (et, names.Length);
531 for (int i = 0; i < fields.Length; ++i)
532 values.SetValue (fields [i].GetValue (null), i);
533 MonoEnumInfo.SortEnums (et, values, names);
539 static NotImplementedException CreateNIE () {
540 return new NotImplementedException ();
543 public virtual Array GetEnumValues () {
545 throw new ArgumentException ("Type is not an enumeration", "enumType");
550 bool IsValidEnumType (Type type) {
551 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
554 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
555 public virtual string GetEnumName (object value) {
557 throw new ArgumentException ("Value is null", "value");
558 if (!IsValidEnumType (value.GetType ()))
559 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
561 throw new ArgumentException ("Type is not an enumeration", "enumType");
564 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
566 for (int i = 0; i < fields.Length; ++i) {
567 var fv = fields [i].GetValue (null);
570 //XXX we can't use 'this' as argument as it might be an UserType
571 obj = Enum.ToObject (fv.GetType (), value);
572 } catch (OverflowException) {
574 } catch (InvalidCastException) {
575 throw new ArgumentException ("Value is not valid", "value");
579 return fields [i].Name;
585 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
586 public virtual bool IsEnumDefined (object value) {
588 throw new ArgumentException ("Value is null", "value");
590 throw new ArgumentException ("Type is not an enumeration", "enumType");
592 Type vt = value.GetType ();
593 if (!IsValidEnumType (vt) && vt != typeof (string))
594 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
596 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
598 if (value is string) {
599 for (int i = 0; i < fields.Length; ++i) {
600 if (fields [i].Name.Equals (value))
604 if (vt != this && vt != GetEnumUnderlyingType ())
605 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
608 for (int i = 0; i < fields.Length; ++i) {
609 var fv = fields [i].GetValue (null);
612 //XXX we can't use 'this' as argument as it might be an UserType
613 obj = Enum.ToObject (fv.GetType (), value);
614 } catch (OverflowException) {
616 } catch (InvalidCastException) {
617 throw new ArgumentException ("Value is not valid", "value");
627 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
629 return GetType (typeName, assemblyResolver, typeResolver, false, false);
632 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
634 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
637 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
639 TypeSpec spec = TypeSpec.Parse (typeName);
640 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
643 public virtual bool IsSecurityTransparent
645 get { throw CreateNIE (); }
648 public virtual bool IsSecurityCritical
650 get { throw CreateNIE (); }
653 public virtual bool IsSecuritySafeCritical
655 get { throw CreateNIE (); }
659 [MethodImplAttribute(MethodImplOptions.InternalCall)]
660 internal extern bool EqualsInternal (Type type);
662 [MethodImplAttribute(MethodImplOptions.InternalCall)]
663 private static extern Type internal_from_handle (IntPtr handle);
665 [MethodImplAttribute(MethodImplOptions.InternalCall)]
666 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
668 public static Type GetType(string typeName)
670 if (typeName == null)
671 throw new ArgumentNullException ("TypeName");
673 return internal_from_name (typeName, false, false);
676 public static Type GetType(string typeName, bool throwOnError)
678 if (typeName == null)
679 throw new ArgumentNullException ("TypeName");
681 Type type = internal_from_name (typeName, throwOnError, false);
682 if (throwOnError && type == null)
683 throw new TypeLoadException ("Error loading '" + typeName + "'");
688 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
690 if (typeName == null)
691 throw new ArgumentNullException ("TypeName");
693 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
694 if (throwOnError && t == null)
695 throw new TypeLoadException ("Error loading '" + typeName + "'");
700 public static Type[] GetTypeArray (object[] args) {
702 throw new ArgumentNullException ("args");
705 ret = new Type [args.Length];
706 for (int i = 0; i < args.Length; ++i)
707 ret [i] = args[i].GetType ();
711 [MethodImplAttribute(MethodImplOptions.InternalCall)]
712 internal extern static TypeCode GetTypeCodeInternal (Type type);
717 TypeCode GetTypeCodeImpl () {
719 if (type is MonoType)
720 return GetTypeCodeInternal (type);
721 #if !FULL_AOT_RUNTIME
722 if (type is TypeBuilder)
723 return ((TypeBuilder)type).GetTypeCodeInternal ();
726 type = type.UnderlyingSystemType;
728 if (!type.IsSystemType)
729 return TypeCode.Object;
731 return GetTypeCodeInternal (type);
734 public static TypeCode GetTypeCode (Type type) {
736 /* MS.NET returns this */
737 return TypeCode.Empty;
738 return type.GetTypeCodeImpl ();
741 #if !FULL_AOT_RUNTIME
742 private static Dictionary<Guid, Type> clsid_types;
743 private static AssemblyBuilder clsid_assemblybuilder;
746 [MonoTODO("COM servers only work on Windows")]
747 public static Type GetTypeFromCLSID (Guid clsid)
749 return GetTypeFromCLSID (clsid, null, true);
752 [MonoTODO("COM servers only work on Windows")]
753 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
755 return GetTypeFromCLSID (clsid, null, throwOnError);
758 [MonoTODO("COM servers only work on Windows")]
759 public static Type GetTypeFromCLSID (Guid clsid, string server)
761 return GetTypeFromCLSID (clsid, server, true);
764 [MonoTODO("COM servers only work on Windows")]
765 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
767 #if !FULL_AOT_RUNTIME
770 if (clsid_types == null)
772 Dictionary<Guid, Type> new_clsid_types = new Dictionary<Guid, Type> ();
773 Interlocked.CompareExchange<Dictionary<Guid, Type>>(
774 ref clsid_types, new_clsid_types, null);
778 if (clsid_types.TryGetValue(clsid, out result))
781 if (clsid_assemblybuilder == null)
783 AssemblyName assemblyname = new AssemblyName ();
784 assemblyname.Name = "GetTypeFromCLSIDDummyAssembly";
785 clsid_assemblybuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (
786 assemblyname, AssemblyBuilderAccess.Run);
788 ModuleBuilder modulebuilder = clsid_assemblybuilder.DefineDynamicModule (
791 TypeBuilder typebuilder = modulebuilder.DefineType ("System.__ComObject",
792 TypeAttributes.Public | TypeAttributes.Class, typeof(System.__ComObject));
794 Type[] guidattrtypes = new Type[] { typeof(string) };
796 CustomAttributeBuilder customattr = new CustomAttributeBuilder (
797 typeof(GuidAttribute).GetConstructor (guidattrtypes),
798 new object[] { clsid.ToString () });
800 typebuilder.SetCustomAttribute (customattr);
802 customattr = new CustomAttributeBuilder (
803 typeof(ComImportAttribute).GetConstructor (EmptyTypes),
806 typebuilder.SetCustomAttribute (customattr);
808 result = typebuilder.CreateType ();
810 clsid_types.Add(clsid, result);
815 throw new NotImplementedException ();
819 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
821 if (handle.Value == IntPtr.Zero)
822 // This is not consistent with the other GetXXXFromHandle methods, but
823 // MS.NET seems to do this
826 return internal_from_handle (handle.Value);
829 [MonoTODO("Mono does not support COM")]
830 public static Type GetTypeFromProgID (string progID)
832 throw new NotImplementedException ();
835 [MonoTODO("Mono does not support COM")]
836 public static Type GetTypeFromProgID (string progID, bool throwOnError)
838 throw new NotImplementedException ();
841 [MonoTODO("Mono does not support COM")]
842 public static Type GetTypeFromProgID (string progID, string server)
844 throw new NotImplementedException ();
847 [MonoTODO("Mono does not support COM")]
848 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
850 throw new NotImplementedException ();
853 public static RuntimeTypeHandle GetTypeHandle (object o)
856 throw new ArgumentNullException ();
858 return o.GetType().TypeHandle;
861 [MethodImplAttribute(MethodImplOptions.InternalCall)]
862 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
864 [MethodImplAttribute(MethodImplOptions.InternalCall)]
865 internal static extern bool type_is_assignable_from (Type a, Type b);
867 public new Type GetType ()
869 return base.GetType ();
873 public virtual bool IsSubclassOf (Type c)
875 if (c == null || c == this)
878 // Fast check for system types
880 return c.IsSystemType && type_is_subtype_of (this, c, false);
882 // User defined types depend on this behavior
883 for (Type type = BaseType; type != null; type = type.BaseType)
890 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
893 throw new ArgumentNullException ("filter");
895 var ifaces = new List<Type> ();
896 foreach (Type iface in GetInterfaces ()) {
897 if (filter (iface, filterCriteria))
901 return ifaces.ToArray ();
904 public Type GetInterface (string name) {
905 return GetInterface (name, false);
908 public abstract Type GetInterface (string name, bool ignoreCase);
910 [MethodImplAttribute(MethodImplOptions.InternalCall)]
911 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
914 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
916 throw new NotSupportedException ("Derived classes must provide an implementation.");
917 if (interfaceType == null)
918 throw new ArgumentNullException ("interfaceType");
919 if (!interfaceType.IsSystemType)
920 throw new ArgumentException ("interfaceType", "Type is an user type");
921 InterfaceMapping res;
922 if (!interfaceType.IsInterface)
923 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
925 throw new ArgumentException ("'this' type cannot be an interface itself");
926 res.TargetType = this;
927 res.InterfaceType = interfaceType;
928 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
929 if (res.TargetMethods == null)
930 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
935 public abstract Type[] GetInterfaces ();
937 public virtual bool IsAssignableFrom (Type c)
945 #if !FULL_AOT_RUNTIME
946 if (c is TypeBuilder)
947 return ((TypeBuilder)c).IsAssignableTo (this);
950 /* Handle user defined type classes */
952 Type systemType = UnderlyingSystemType;
953 if (!systemType.IsSystemType)
956 Type other = c.UnderlyingSystemType;
957 if (!other.IsSystemType)
960 return systemType.IsAssignableFrom (other);
963 if (!c.IsSystemType) {
964 Type underlyingType = c.UnderlyingSystemType;
965 if (!underlyingType.IsSystemType)
967 return IsAssignableFrom (underlyingType);
970 return type_is_assignable_from (this, c);
973 [MethodImplAttribute(MethodImplOptions.InternalCall)]
974 extern static bool IsInstanceOfType (Type type, object o);
976 public virtual bool IsInstanceOfType (object o)
978 Type type = UnderlyingSystemType;
979 if (!type.IsSystemType)
981 return IsInstanceOfType (type, o);
984 public virtual int GetArrayRank ()
986 throw new NotSupportedException (); // according to MSDN
989 public abstract Type GetElementType ();
991 public EventInfo GetEvent (string name)
993 return GetEvent (name, DefaultBindingFlags);
996 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
998 public virtual EventInfo[] GetEvents ()
1000 return GetEvents (DefaultBindingFlags);
1003 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
1005 public FieldInfo GetField( string name)
1007 return GetField (name, DefaultBindingFlags);
1010 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
1012 public FieldInfo[] GetFields ()
1014 return GetFields (DefaultBindingFlags);
1017 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
1019 public override int GetHashCode()
1021 Type t = UnderlyingSystemType;
1022 if (t != null && t != this)
1023 return t.GetHashCode ();
1024 return (int)_impl.Value;
1027 public MemberInfo[] GetMember (string name)
1029 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
1032 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
1034 return GetMember (name, MemberTypes.All, bindingAttr);
1037 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
1040 throw new ArgumentNullException ("name");
1041 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
1042 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
1044 return FindMembers (type, bindingAttr, FilterName, name);
1047 public MemberInfo[] GetMembers ()
1049 return GetMembers (DefaultBindingFlags);
1052 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
1054 public MethodInfo GetMethod (string name)
1057 throw new ArgumentNullException ("name");
1058 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
1061 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
1064 throw new ArgumentNullException ("name");
1066 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1069 public MethodInfo GetMethod (string name, Type[] types)
1071 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1074 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1076 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1079 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1080 Type[] types, ParameterModifier[] modifiers)
1082 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1085 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1086 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1089 throw new ArgumentNullException ("name");
1091 throw new ArgumentNullException ("types");
1093 for (int i = 0; i < types.Length; i++)
1094 if (types[i] == null)
1095 throw new ArgumentNullException ("types");
1097 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1100 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1101 CallingConventions callConvention, Type[] types,
1102 ParameterModifier[] modifiers);
1104 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1105 CallingConventions callConvention, Type[] types,
1106 ParameterModifier[] modifiers)
1108 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1111 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1113 throw new System.InvalidOperationException ("can only be called in generic type");
1116 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1118 throw new System.InvalidOperationException ("can only be called in generic type");
1121 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1123 throw new System.InvalidOperationException ("can only be called in generic type");
1127 public MethodInfo[] GetMethods ()
1129 return GetMethods (DefaultBindingFlags);
1132 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1134 public Type GetNestedType (string name)
1136 return GetNestedType (name, DefaultBindingFlags);
1139 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1141 public Type[] GetNestedTypes ()
1143 return GetNestedTypes (DefaultBindingFlags);
1146 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1149 public PropertyInfo[] GetProperties ()
1151 return GetProperties (DefaultBindingFlags);
1154 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1157 public PropertyInfo GetProperty (string name)
1160 throw new ArgumentNullException ("name");
1162 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1165 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1168 throw new ArgumentNullException ("name");
1169 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1172 public PropertyInfo GetProperty (string name, Type returnType)
1175 throw new ArgumentNullException ("name");
1176 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1179 public PropertyInfo GetProperty (string name, Type[] types)
1181 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1184 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1186 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1189 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1191 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1194 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1195 Type[] types, ParameterModifier[] modifiers)
1198 throw new ArgumentNullException ("name");
1200 throw new ArgumentNullException ("types");
1202 foreach (Type t in types) {
1204 throw new ArgumentNullException ("types");
1207 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1210 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1211 Type returnType, Type[] types, ParameterModifier[] modifiers);
1213 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1214 Type returnType, Type[] types, ParameterModifier[] modifiers)
1216 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1219 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1221 CallingConventions callConvention,
1223 ParameterModifier[] modifiers);
1225 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1226 protected abstract bool HasElementTypeImpl ();
1227 protected abstract bool IsArrayImpl ();
1228 protected abstract bool IsByRefImpl ();
1229 protected abstract bool IsCOMObjectImpl ();
1230 protected abstract bool IsPointerImpl ();
1231 protected abstract bool IsPrimitiveImpl ();
1233 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1234 internal static extern bool IsArrayImpl (Type type);
1236 protected virtual bool IsValueTypeImpl ()
1238 if (this == typeof (ValueType) || this == typeof (Enum))
1241 return IsSubclassOf (typeof (ValueType));
1244 protected virtual bool IsContextfulImpl ()
1246 return typeof (ContextBoundObject).IsAssignableFrom (this);
1249 protected virtual bool IsMarshalByRefImpl ()
1251 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1255 public ConstructorInfo GetConstructor (Type[] types)
1257 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1261 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1262 Type[] types, ParameterModifier[] modifiers)
1264 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1268 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1269 CallingConventions callConvention,
1270 Type[] types, ParameterModifier[] modifiers)
1273 throw new ArgumentNullException ("types");
1275 foreach (Type t in types) {
1277 throw new ArgumentNullException ("types");
1280 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1284 public ConstructorInfo[] GetConstructors ()
1286 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1290 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1292 public virtual MemberInfo[] GetDefaultMembers ()
1294 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1295 if (att.Length == 0)
1296 return EmptyArray<MemberInfo>.Value;
1298 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1299 return (member != null) ? member : EmptyArray<MemberInfo>.Value;
1302 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1303 MemberFilter filter, object filterCriteria)
1305 MemberInfo[] result;
1306 ArrayList l = new ArrayList ();
1308 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1309 // this.FullName, this.GetType().FullName, this.obj_address());
1310 if ((memberType & MemberTypes.Method) != 0) {
1311 MethodInfo[] c = GetMethods (bindingAttr);
1312 if (filter != null) {
1313 foreach (MemberInfo m in c) {
1314 if (filter (m, filterCriteria))
1321 if ((memberType & MemberTypes.Constructor) != 0) {
1322 ConstructorInfo[] c = GetConstructors (bindingAttr);
1323 if (filter != null) {
1324 foreach (MemberInfo m in c) {
1325 if (filter (m, filterCriteria))
1332 if ((memberType & MemberTypes.Property) != 0) {
1333 PropertyInfo[] c = GetProperties (bindingAttr);
1336 if (filter != null) {
1337 foreach (MemberInfo m in c) {
1338 if (filter (m, filterCriteria))
1346 if ((memberType & MemberTypes.Event) != 0) {
1347 EventInfo[] c = GetEvents (bindingAttr);
1348 if (filter != null) {
1349 foreach (MemberInfo m in c) {
1350 if (filter (m, filterCriteria))
1357 if ((memberType & MemberTypes.Field) != 0) {
1358 FieldInfo[] c = GetFields (bindingAttr);
1359 if (filter != null) {
1360 foreach (MemberInfo m in c) {
1361 if (filter (m, filterCriteria))
1368 if ((memberType & MemberTypes.NestedType) != 0) {
1369 Type[] c = GetNestedTypes (bindingAttr);
1370 if (filter != null) {
1371 foreach (MemberInfo m in c) {
1372 if (filter (m, filterCriteria)) {
1381 switch (memberType) {
1382 case MemberTypes.Constructor :
1383 result = new ConstructorInfo [l.Count];
1385 case MemberTypes.Event :
1386 result = new EventInfo [l.Count];
1388 case MemberTypes.Field :
1389 result = new FieldInfo [l.Count];
1391 case MemberTypes.Method :
1392 result = new MethodInfo [l.Count];
1394 case MemberTypes.NestedType :
1395 case MemberTypes.TypeInfo :
1396 result = new Type [l.Count];
1398 case MemberTypes.Property :
1399 result = new PropertyInfo [l.Count];
1402 result = new MemberInfo [l.Count];
1410 [DebuggerStepThrough]
1411 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1413 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1417 [DebuggerStepThrough]
1418 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1419 object target, object[] args, CultureInfo culture)
1421 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1424 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1425 Binder binder, object target, object[] args,
1426 ParameterModifier[] modifiers,
1427 CultureInfo culture, string[] namedParameters);
1429 public override string ToString()
1434 internal static bool ShouldPrintFullName (Type type)
1436 return type.IsGenericType || (type.IsClass && (!type.IsPointer ||
1437 (!type.GetElementType ().IsPrimitive && !type.GetElementType ().IsNested)));
1440 internal virtual Type InternalResolve ()
1442 return UnderlyingSystemType;
1445 internal bool IsSystemType {
1447 return _impl.Value != IntPtr.Zero;
1452 public virtual Type[] GenericTypeArguments {
1454 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1459 public virtual Type[] GetGenericArguments ()
1461 throw new NotSupportedException ();
1464 public virtual bool ContainsGenericParameters {
1465 get { return false; }
1468 public virtual extern bool IsGenericTypeDefinition {
1469 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1473 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1474 internal extern Type GetGenericTypeDefinition_impl ();
1476 public virtual Type GetGenericTypeDefinition ()
1478 throw new NotSupportedException ("Derived classes must provide an implementation.");
1481 public virtual extern bool IsGenericType {
1482 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1486 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1487 static extern Type MakeGenericType (Type gt, Type [] types);
1489 public virtual Type MakeGenericType (params Type[] typeArguments)
1492 throw new NotSupportedException ();
1493 if (!IsGenericTypeDefinition)
1494 throw new InvalidOperationException ("not a generic type definition");
1495 if (typeArguments == null)
1496 throw new ArgumentNullException ("typeArguments");
1497 if (GetGenericArguments().Length != typeArguments.Length)
1498 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");
1500 bool hasUserType = false;
1502 Type[] systemTypes = new Type[typeArguments.Length];
1503 for (int i = 0; i < typeArguments.Length; ++i) {
1504 Type t = typeArguments [i];
1506 throw new ArgumentNullException ("typeArguments");
1508 if (!(t is MonoType))
1510 systemTypes [i] = t;
1514 #if FULL_AOT_RUNTIME
1515 throw new NotSupportedException ("User types are not supported under full aot");
1517 return new MonoGenericClass (this, typeArguments);
1521 Type res = MakeGenericType (this, systemTypes);
1523 throw new TypeLoadException ();
1527 public virtual bool IsGenericParameter {
1533 public bool IsNested {
1535 return DeclaringType != null;
1539 public bool IsVisible {
1542 return DeclaringType.IsVisible;
1548 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1549 extern int GetGenericParameterPosition ();
1551 public virtual int GenericParameterPosition {
1553 int res = GetGenericParameterPosition ();
1555 throw new InvalidOperationException ();
1560 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1561 extern GenericParameterAttributes GetGenericParameterAttributes ();
1563 public virtual GenericParameterAttributes GenericParameterAttributes {
1566 throw new NotSupportedException ("Derived classes must provide an implementation.");
1568 if (!IsGenericParameter)
1569 throw new InvalidOperationException ();
1571 return GetGenericParameterAttributes ();
1575 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1576 extern Type[] GetGenericParameterConstraints_impl ();
1578 public virtual Type[] GetGenericParameterConstraints ()
1581 throw new InvalidOperationException ();
1583 if (!IsGenericParameter)
1584 throw new InvalidOperationException ();
1586 return GetGenericParameterConstraints_impl ();
1589 public virtual MethodBase DeclaringMethod {
1595 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1596 extern Type make_array_type (int rank);
1598 public virtual Type MakeArrayType ()
1601 throw new NotSupportedException ("Derived classes must provide an implementation.");
1602 return make_array_type (0);
1605 public virtual Type MakeArrayType (int rank)
1608 throw new NotSupportedException ("Derived classes must provide an implementation.");
1609 if (rank < 1 || rank > 255)
1610 throw new IndexOutOfRangeException ();
1611 return make_array_type (rank);
1614 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1615 extern Type make_byref_type ();
1617 public virtual Type MakeByRefType ()
1620 throw new NotSupportedException ("Derived classes must provide an implementation.");
1622 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1623 return make_byref_type ();
1626 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1627 static extern Type MakePointerType (Type type);
1629 public virtual Type MakePointerType ()
1632 throw new NotSupportedException ("Derived classes must provide an implementation.");
1633 return MakePointerType (this);
1636 public static Type ReflectionOnlyGetType (string typeName,
1637 bool throwIfNotFound,
1640 if (typeName == null)
1641 throw new ArgumentNullException ("typeName");
1642 int idx = typeName.IndexOf (',');
1643 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1644 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1645 string an = typeName.Substring (idx + 1);
1648 a = Assembly.ReflectionOnlyLoad (an);
1650 if (throwIfNotFound)
1654 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1657 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1658 extern void GetPacking (out int packing, out int size);
1660 public virtual StructLayoutAttribute StructLayoutAttribute {
1663 throw new NotSupportedException ();
1665 return GetStructLayoutAttribute ();
1670 internal StructLayoutAttribute GetStructLayoutAttribute ()
1674 if (IsLayoutSequential)
1675 kind = LayoutKind.Sequential;
1676 else if (IsExplicitLayout)
1677 kind = LayoutKind.Explicit;
1679 kind = LayoutKind.Auto;
1681 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1684 attr.CharSet = CharSet.Unicode;
1685 else if (IsAnsiClass)
1686 attr.CharSet = CharSet.Ansi;
1688 attr.CharSet = CharSet.Auto;
1690 if (kind != LayoutKind.Auto) {
1692 GetPacking (out packing, out attr.Size);
1693 // 0 means no data provided, we end up with default value
1695 attr.Pack = packing;
1701 internal object[] GetPseudoCustomAttributes ()
1705 /* IsSerializable returns true for delegates/enums as well */
1706 if ((Attributes & TypeAttributes.Serializable) != 0)
1708 if ((Attributes & TypeAttributes.Import) != 0)
1713 object[] attrs = new object [count];
1716 if ((Attributes & TypeAttributes.Serializable) != 0)
1717 attrs [count ++] = new SerializableAttribute ();
1718 if ((Attributes & TypeAttributes.Import) != 0)
1719 attrs [count ++] = new ComImportAttribute ();
1726 public virtual bool IsEquivalentTo (Type other)
1728 return this == other;
1733 * Return whenever this object is an instance of a user defined subclass
1734 * of System.Type or an instance of TypeDelegator.
1735 * A user defined type is not simply the opposite of a system type.
1736 * It's any class that's neither a SRE or runtime baked type.
1738 internal virtual bool IsUserType {
1745 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1747 throw new NotImplementedException ();
1750 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1752 throw new NotImplementedException ();
1755 void _Type.GetTypeInfoCount (out uint pcTInfo)
1757 throw new NotImplementedException ();
1760 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1762 throw new NotImplementedException ();