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 #if !FULL_AOT_RUNTIME
744 private static AssemblyBuilder clsid_assemblybuilder;
747 [MonoTODO("COM servers only work on Windows")]
748 public static Type GetTypeFromCLSID (Guid clsid)
750 return GetTypeFromCLSID (clsid, null, true);
753 [MonoTODO("COM servers only work on Windows")]
754 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
756 return GetTypeFromCLSID (clsid, null, throwOnError);
759 [MonoTODO("COM servers only work on Windows")]
760 public static Type GetTypeFromCLSID (Guid clsid, string server)
762 return GetTypeFromCLSID (clsid, server, true);
765 [MonoTODO("COM servers only work on Windows")]
766 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
768 #if !FULL_AOT_RUNTIME
771 if (clsid_types == null)
773 Dictionary<Guid, Type> new_clsid_types = new Dictionary<Guid, Type> ();
774 Interlocked.CompareExchange<Dictionary<Guid, Type>>(
775 ref clsid_types, new_clsid_types, null);
779 if (clsid_types.TryGetValue(clsid, out result))
782 if (clsid_assemblybuilder == null)
784 AssemblyName assemblyname = new AssemblyName ();
785 assemblyname.Name = "GetTypeFromCLSIDDummyAssembly";
786 clsid_assemblybuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (
787 assemblyname, AssemblyBuilderAccess.Run);
789 ModuleBuilder modulebuilder = clsid_assemblybuilder.DefineDynamicModule (
792 TypeBuilder typebuilder = modulebuilder.DefineType ("System.__ComObject",
793 TypeAttributes.Public | TypeAttributes.Class, typeof(System.__ComObject));
795 Type[] guidattrtypes = new Type[] { typeof(string) };
797 CustomAttributeBuilder customattr = new CustomAttributeBuilder (
798 typeof(GuidAttribute).GetConstructor (guidattrtypes),
799 new object[] { clsid.ToString () });
801 typebuilder.SetCustomAttribute (customattr);
803 customattr = new CustomAttributeBuilder (
804 typeof(ComImportAttribute).GetConstructor (EmptyTypes),
807 typebuilder.SetCustomAttribute (customattr);
809 result = typebuilder.CreateType ();
811 clsid_types.Add(clsid, result);
816 throw new NotImplementedException ();
820 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
822 if (handle.Value == IntPtr.Zero)
823 // This is not consistent with the other GetXXXFromHandle methods, but
824 // MS.NET seems to do this
827 return internal_from_handle (handle.Value);
830 [MonoTODO("Mono does not support COM")]
831 public static Type GetTypeFromProgID (string progID)
833 throw new NotImplementedException ();
836 [MonoTODO("Mono does not support COM")]
837 public static Type GetTypeFromProgID (string progID, bool throwOnError)
839 throw new NotImplementedException ();
842 [MonoTODO("Mono does not support COM")]
843 public static Type GetTypeFromProgID (string progID, string server)
845 throw new NotImplementedException ();
848 [MonoTODO("Mono does not support COM")]
849 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
851 throw new NotImplementedException ();
854 public static RuntimeTypeHandle GetTypeHandle (object o)
857 throw new ArgumentNullException ();
859 return o.GetType().TypeHandle;
862 [MethodImplAttribute(MethodImplOptions.InternalCall)]
863 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
865 [MethodImplAttribute(MethodImplOptions.InternalCall)]
866 internal static extern bool type_is_assignable_from (Type a, Type b);
868 public new Type GetType ()
870 return base.GetType ();
874 public virtual bool IsSubclassOf (Type c)
876 if (c == null || c == this)
879 // Fast check for system types
881 return c.IsSystemType && type_is_subtype_of (this, c, false);
883 // User defined types depend on this behavior
884 for (Type type = BaseType; type != null; type = type.BaseType)
891 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
894 throw new ArgumentNullException ("filter");
896 var ifaces = new List<Type> ();
897 foreach (Type iface in GetInterfaces ()) {
898 if (filter (iface, filterCriteria))
902 return ifaces.ToArray ();
905 public Type GetInterface (string name) {
906 return GetInterface (name, false);
909 public abstract Type GetInterface (string name, bool ignoreCase);
911 [MethodImplAttribute(MethodImplOptions.InternalCall)]
912 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
915 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
917 throw new NotSupportedException ("Derived classes must provide an implementation.");
918 if (interfaceType == null)
919 throw new ArgumentNullException ("interfaceType");
920 if (!interfaceType.IsSystemType)
921 throw new ArgumentException ("interfaceType", "Type is an user type");
922 InterfaceMapping res;
923 if (!interfaceType.IsInterface)
924 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
926 throw new ArgumentException ("'this' type cannot be an interface itself");
927 res.TargetType = this;
928 res.InterfaceType = interfaceType;
929 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
930 if (res.TargetMethods == null)
931 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
936 public abstract Type[] GetInterfaces ();
938 public virtual bool IsAssignableFrom (Type c)
946 #if !FULL_AOT_RUNTIME
947 if (c is TypeBuilder)
948 return ((TypeBuilder)c).IsAssignableTo (this);
951 /* Handle user defined type classes */
953 Type systemType = UnderlyingSystemType;
954 if (!systemType.IsSystemType)
957 Type other = c.UnderlyingSystemType;
958 if (!other.IsSystemType)
961 return systemType.IsAssignableFrom (other);
964 if (!c.IsSystemType) {
965 Type underlyingType = c.UnderlyingSystemType;
966 if (!underlyingType.IsSystemType)
968 return IsAssignableFrom (underlyingType);
971 return type_is_assignable_from (this, c);
974 [MethodImplAttribute(MethodImplOptions.InternalCall)]
975 extern static bool IsInstanceOfType (Type type, object o);
977 public virtual bool IsInstanceOfType (object o)
979 Type type = UnderlyingSystemType;
980 if (!type.IsSystemType)
982 return IsInstanceOfType (type, o);
985 public virtual int GetArrayRank ()
987 throw new NotSupportedException (); // according to MSDN
990 public abstract Type GetElementType ();
992 public EventInfo GetEvent (string name)
994 return GetEvent (name, DefaultBindingFlags);
997 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
999 public virtual EventInfo[] GetEvents ()
1001 return GetEvents (DefaultBindingFlags);
1004 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
1006 public FieldInfo GetField( string name)
1008 return GetField (name, DefaultBindingFlags);
1011 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
1013 public FieldInfo[] GetFields ()
1015 return GetFields (DefaultBindingFlags);
1018 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
1020 public override int GetHashCode()
1022 Type t = UnderlyingSystemType;
1023 if (t != null && t != this)
1024 return t.GetHashCode ();
1025 return (int)_impl.Value;
1028 public MemberInfo[] GetMember (string name)
1030 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
1033 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
1035 return GetMember (name, MemberTypes.All, bindingAttr);
1038 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
1041 throw new ArgumentNullException ("name");
1042 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
1043 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
1045 return FindMembers (type, bindingAttr, FilterName, name);
1048 public MemberInfo[] GetMembers ()
1050 return GetMembers (DefaultBindingFlags);
1053 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
1055 public MethodInfo GetMethod (string name)
1058 throw new ArgumentNullException ("name");
1059 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
1062 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
1065 throw new ArgumentNullException ("name");
1067 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1070 public MethodInfo GetMethod (string name, Type[] types)
1072 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1075 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1077 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1080 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1081 Type[] types, ParameterModifier[] modifiers)
1083 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1086 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1087 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1090 throw new ArgumentNullException ("name");
1092 throw new ArgumentNullException ("types");
1094 for (int i = 0; i < types.Length; i++)
1095 if (types[i] == null)
1096 throw new ArgumentNullException ("types");
1098 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1101 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1102 CallingConventions callConvention, Type[] types,
1103 ParameterModifier[] modifiers);
1105 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1106 CallingConventions callConvention, Type[] types,
1107 ParameterModifier[] modifiers)
1109 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1112 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1114 throw new System.InvalidOperationException ("can only be called in generic type");
1117 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1119 throw new System.InvalidOperationException ("can only be called in generic type");
1122 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1124 throw new System.InvalidOperationException ("can only be called in generic type");
1128 public MethodInfo[] GetMethods ()
1130 return GetMethods (DefaultBindingFlags);
1133 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1135 public Type GetNestedType (string name)
1137 return GetNestedType (name, DefaultBindingFlags);
1140 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1142 public Type[] GetNestedTypes ()
1144 return GetNestedTypes (DefaultBindingFlags);
1147 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1150 public PropertyInfo[] GetProperties ()
1152 return GetProperties (DefaultBindingFlags);
1155 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1158 public PropertyInfo GetProperty (string name)
1161 throw new ArgumentNullException ("name");
1163 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1166 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1169 throw new ArgumentNullException ("name");
1170 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1173 public PropertyInfo GetProperty (string name, Type returnType)
1176 throw new ArgumentNullException ("name");
1177 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1180 public PropertyInfo GetProperty (string name, Type[] types)
1182 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1185 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1187 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1190 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1192 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1195 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1196 Type[] types, ParameterModifier[] modifiers)
1199 throw new ArgumentNullException ("name");
1201 throw new ArgumentNullException ("types");
1203 foreach (Type t in types) {
1205 throw new ArgumentNullException ("types");
1208 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1211 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1212 Type returnType, Type[] types, ParameterModifier[] modifiers);
1214 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1215 Type returnType, Type[] types, ParameterModifier[] modifiers)
1217 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1220 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1222 CallingConventions callConvention,
1224 ParameterModifier[] modifiers);
1226 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1227 protected abstract bool HasElementTypeImpl ();
1228 protected abstract bool IsArrayImpl ();
1229 protected abstract bool IsByRefImpl ();
1230 protected abstract bool IsCOMObjectImpl ();
1231 protected abstract bool IsPointerImpl ();
1232 protected abstract bool IsPrimitiveImpl ();
1234 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1235 internal static extern bool IsArrayImpl (Type type);
1237 protected virtual bool IsValueTypeImpl ()
1239 if (this == typeof (ValueType) || this == typeof (Enum))
1242 return IsSubclassOf (typeof (ValueType));
1245 protected virtual bool IsContextfulImpl ()
1247 return typeof (ContextBoundObject).IsAssignableFrom (this);
1250 protected virtual bool IsMarshalByRefImpl ()
1252 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1256 public ConstructorInfo GetConstructor (Type[] types)
1258 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1262 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1263 Type[] types, ParameterModifier[] modifiers)
1265 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1269 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1270 CallingConventions callConvention,
1271 Type[] types, ParameterModifier[] modifiers)
1274 throw new ArgumentNullException ("types");
1276 foreach (Type t in types) {
1278 throw new ArgumentNullException ("types");
1281 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1285 public ConstructorInfo[] GetConstructors ()
1287 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1291 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1293 public virtual MemberInfo[] GetDefaultMembers ()
1295 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1296 if (att.Length == 0)
1297 return EmptyArray<MemberInfo>.Value;
1299 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1300 return (member != null) ? member : EmptyArray<MemberInfo>.Value;
1303 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1304 MemberFilter filter, object filterCriteria)
1306 MemberInfo[] result;
1307 ArrayList l = new ArrayList ();
1309 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1310 // this.FullName, this.GetType().FullName, this.obj_address());
1311 if ((memberType & MemberTypes.Method) != 0) {
1312 MethodInfo[] c = GetMethods (bindingAttr);
1313 if (filter != null) {
1314 foreach (MemberInfo m in c) {
1315 if (filter (m, filterCriteria))
1322 if ((memberType & MemberTypes.Constructor) != 0) {
1323 ConstructorInfo[] c = GetConstructors (bindingAttr);
1324 if (filter != null) {
1325 foreach (MemberInfo m in c) {
1326 if (filter (m, filterCriteria))
1333 if ((memberType & MemberTypes.Property) != 0) {
1334 PropertyInfo[] c = GetProperties (bindingAttr);
1337 if (filter != null) {
1338 foreach (MemberInfo m in c) {
1339 if (filter (m, filterCriteria))
1347 if ((memberType & MemberTypes.Event) != 0) {
1348 EventInfo[] c = GetEvents (bindingAttr);
1349 if (filter != null) {
1350 foreach (MemberInfo m in c) {
1351 if (filter (m, filterCriteria))
1358 if ((memberType & MemberTypes.Field) != 0) {
1359 FieldInfo[] c = GetFields (bindingAttr);
1360 if (filter != null) {
1361 foreach (MemberInfo m in c) {
1362 if (filter (m, filterCriteria))
1369 if ((memberType & MemberTypes.NestedType) != 0) {
1370 Type[] c = GetNestedTypes (bindingAttr);
1371 if (filter != null) {
1372 foreach (MemberInfo m in c) {
1373 if (filter (m, filterCriteria)) {
1382 switch (memberType) {
1383 case MemberTypes.Constructor :
1384 result = new ConstructorInfo [l.Count];
1386 case MemberTypes.Event :
1387 result = new EventInfo [l.Count];
1389 case MemberTypes.Field :
1390 result = new FieldInfo [l.Count];
1392 case MemberTypes.Method :
1393 result = new MethodInfo [l.Count];
1395 case MemberTypes.NestedType :
1396 case MemberTypes.TypeInfo :
1397 result = new Type [l.Count];
1399 case MemberTypes.Property :
1400 result = new PropertyInfo [l.Count];
1403 result = new MemberInfo [l.Count];
1411 [DebuggerStepThrough]
1412 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1414 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1418 [DebuggerStepThrough]
1419 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1420 object target, object[] args, CultureInfo culture)
1422 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1425 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1426 Binder binder, object target, object[] args,
1427 ParameterModifier[] modifiers,
1428 CultureInfo culture, string[] namedParameters);
1430 public override string ToString()
1435 internal virtual Type InternalResolve ()
1437 return UnderlyingSystemType;
1440 internal bool IsSystemType {
1442 return _impl.Value != IntPtr.Zero;
1447 public virtual Type[] GenericTypeArguments {
1449 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1454 public virtual Type[] GetGenericArguments ()
1456 throw new NotSupportedException ();
1459 public virtual bool ContainsGenericParameters {
1460 get { return false; }
1463 public virtual extern bool IsGenericTypeDefinition {
1464 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1468 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1469 internal extern Type GetGenericTypeDefinition_impl ();
1471 public virtual Type GetGenericTypeDefinition ()
1473 throw new NotSupportedException ("Derived classes must provide an implementation.");
1476 public virtual extern bool IsGenericType {
1477 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1481 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1482 static extern Type MakeGenericType (Type gt, Type [] types);
1484 public virtual Type MakeGenericType (params Type[] typeArguments)
1487 throw new NotSupportedException ();
1488 if (!IsGenericTypeDefinition)
1489 throw new InvalidOperationException ("not a generic type definition");
1490 if (typeArguments == null)
1491 throw new ArgumentNullException ("typeArguments");
1492 if (GetGenericArguments().Length != typeArguments.Length)
1493 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");
1495 bool hasUserType = false;
1497 Type[] systemTypes = new Type[typeArguments.Length];
1498 for (int i = 0; i < typeArguments.Length; ++i) {
1499 Type t = typeArguments [i];
1501 throw new ArgumentNullException ("typeArguments");
1503 if (!(t is MonoType))
1505 systemTypes [i] = t;
1509 #if FULL_AOT_RUNTIME
1510 throw new NotSupportedException ("User types are not supported under full aot");
1512 return new MonoGenericClass (this, typeArguments);
1516 Type res = MakeGenericType (this, systemTypes);
1518 throw new TypeLoadException ();
1522 public virtual bool IsGenericParameter {
1528 public bool IsNested {
1530 return DeclaringType != null;
1534 public bool IsVisible {
1537 return DeclaringType.IsVisible;
1543 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1544 extern int GetGenericParameterPosition ();
1546 public virtual int GenericParameterPosition {
1548 int res = GetGenericParameterPosition ();
1550 throw new InvalidOperationException ();
1555 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1556 extern GenericParameterAttributes GetGenericParameterAttributes ();
1558 public virtual GenericParameterAttributes GenericParameterAttributes {
1561 throw new NotSupportedException ("Derived classes must provide an implementation.");
1563 if (!IsGenericParameter)
1564 throw new InvalidOperationException ();
1566 return GetGenericParameterAttributes ();
1570 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1571 extern Type[] GetGenericParameterConstraints_impl ();
1573 public virtual Type[] GetGenericParameterConstraints ()
1576 throw new InvalidOperationException ();
1578 if (!IsGenericParameter)
1579 throw new InvalidOperationException ();
1581 return GetGenericParameterConstraints_impl ();
1584 public virtual MethodBase DeclaringMethod {
1590 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1591 extern Type make_array_type (int rank);
1593 public virtual Type MakeArrayType ()
1596 throw new NotSupportedException ("Derived classes must provide an implementation.");
1597 return make_array_type (0);
1600 public virtual Type MakeArrayType (int rank)
1603 throw new NotSupportedException ("Derived classes must provide an implementation.");
1604 if (rank < 1 || rank > 255)
1605 throw new IndexOutOfRangeException ();
1606 return make_array_type (rank);
1609 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1610 extern Type make_byref_type ();
1612 public virtual Type MakeByRefType ()
1615 throw new NotSupportedException ("Derived classes must provide an implementation.");
1617 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1618 return make_byref_type ();
1621 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1622 static extern Type MakePointerType (Type type);
1624 public virtual Type MakePointerType ()
1627 throw new NotSupportedException ("Derived classes must provide an implementation.");
1628 return MakePointerType (this);
1631 public static Type ReflectionOnlyGetType (string typeName,
1632 bool throwIfNotFound,
1635 if (typeName == null)
1636 throw new ArgumentNullException ("typeName");
1637 int idx = typeName.IndexOf (',');
1638 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1639 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1640 string an = typeName.Substring (idx + 1);
1643 a = Assembly.ReflectionOnlyLoad (an);
1645 if (throwIfNotFound)
1649 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1652 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1653 extern void GetPacking (out int packing, out int size);
1655 public virtual StructLayoutAttribute StructLayoutAttribute {
1658 throw new NotSupportedException ();
1660 return GetStructLayoutAttribute ();
1665 internal StructLayoutAttribute GetStructLayoutAttribute ()
1669 if (IsLayoutSequential)
1670 kind = LayoutKind.Sequential;
1671 else if (IsExplicitLayout)
1672 kind = LayoutKind.Explicit;
1674 kind = LayoutKind.Auto;
1676 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1679 attr.CharSet = CharSet.Unicode;
1680 else if (IsAnsiClass)
1681 attr.CharSet = CharSet.Ansi;
1683 attr.CharSet = CharSet.Auto;
1685 if (kind != LayoutKind.Auto) {
1687 GetPacking (out packing, out attr.Size);
1688 // 0 means no data provided, we end up with default value
1690 attr.Pack = packing;
1696 internal object[] GetPseudoCustomAttributes ()
1700 /* IsSerializable returns true for delegates/enums as well */
1701 if ((Attributes & TypeAttributes.Serializable) != 0)
1703 if ((Attributes & TypeAttributes.Import) != 0)
1708 object[] attrs = new object [count];
1711 if ((Attributes & TypeAttributes.Serializable) != 0)
1712 attrs [count ++] = new SerializableAttribute ();
1713 if ((Attributes & TypeAttributes.Import) != 0)
1714 attrs [count ++] = new ComImportAttribute ();
1721 public virtual bool IsEquivalentTo (Type other)
1723 return this == other;
1728 * Return whenever this object is an instance of a user defined subclass
1729 * of System.Type or an instance of TypeDelegator.
1730 * A user defined type is not simply the opposite of a system type.
1731 * It's any class that's neither a SRE or runtime baked type.
1733 internal virtual bool IsUserType {
1740 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1742 throw new NotImplementedException ();
1745 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1747 throw new NotImplementedException ();
1750 void _Type.GetTypeInfoCount (out uint pcTInfo)
1752 throw new NotImplementedException ();
1755 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1757 throw new NotImplementedException ();