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;
36 using System.Reflection.Emit;
37 using System.Collections;
38 using System.Collections.Generic;
39 using System.Runtime.InteropServices;
40 using System.Runtime.CompilerServices;
41 using System.Globalization;
46 [ClassInterface (ClassInterfaceType.None)]
48 [ComDefaultInterface (typeof (_Type))]
49 [StructLayout (LayoutKind.Sequential)]
50 public abstract class Type : MemberInfo, IReflect, _Type {
52 internal RuntimeTypeHandle _impl;
54 public static readonly char Delimiter = '.';
55 public static readonly Type[] EmptyTypes = {};
56 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
57 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
58 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
59 public static readonly object Missing = System.Reflection.Missing.Value;
61 internal const BindingFlags DefaultBindingFlags =
62 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
64 /* implementation of the delegates for MemberFilter */
65 static bool FilterName_impl (MemberInfo m, object filterCriteria)
67 string name = (string) filterCriteria;
68 if (name == null || name.Length == 0 )
69 return false; // because m.Name cannot be null or empty
71 if (name [name.Length-1] == '*')
72 return string.CompareOrdinal (name, 0, m.Name, 0, name.Length-1) == 0;
74 return name.Equals (m.Name);
77 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
79 string name = (string) filterCriteria;
80 if (name == null || name.Length == 0 )
81 return false; // because m.Name cannot be null or empty
83 if (name [name.Length-1] == '*')
84 return string.Compare (name, 0, m.Name, 0, name.Length-1, StringComparison.OrdinalIgnoreCase) == 0;
86 return string.Equals (name, m.Name, StringComparison.OrdinalIgnoreCase);
89 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
91 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
93 return ((int)((MethodInfo)m).Attributes & flags) != 0;
95 return ((int)((FieldInfo)m).Attributes & flags) != 0;
96 if (m is PropertyInfo)
97 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
99 return ((int)((EventInfo)m).Attributes & flags) != 0;
108 /// The assembly where the type is defined.
110 public abstract Assembly Assembly {
115 /// Gets the fully qualified name for the type including the
116 /// assembly name where the type is defined.
118 public abstract string AssemblyQualifiedName {
123 /// Returns the Attributes associated with the type.
125 public TypeAttributes Attributes {
127 return GetAttributeFlagsImpl ();
132 /// Returns the basetype for this type
134 public abstract Type BaseType {
139 /// Returns the class that declares the member.
141 public override Type DeclaringType {
150 public static Binder DefaultBinder {
152 return Binder.DefaultBinder;
157 /// The full name of the type including its namespace
159 public abstract string FullName {
163 public abstract Guid GUID {
167 public bool HasElementType {
169 return HasElementTypeImpl ();
173 public bool IsAbstract {
175 return (Attributes & TypeAttributes.Abstract) != 0;
179 public bool IsAnsiClass {
181 return (Attributes & TypeAttributes.StringFormatMask)
182 == TypeAttributes.AnsiClass;
186 public bool IsArray {
188 return IsArrayImpl ();
192 public bool IsAutoClass {
194 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
198 public bool IsAutoLayout {
200 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
204 public bool IsByRef {
206 return IsByRefImpl ();
210 public bool IsClass {
219 public bool IsCOMObject {
221 return IsCOMObjectImpl ();
226 public virtual bool IsConstructedGenericType {
228 throw new NotImplementedException ();
233 public bool IsContextful {
235 return IsContextfulImpl ();
245 return IsSubclassOf (typeof (Enum));
249 public bool IsExplicitLayout {
251 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
255 public bool IsImport {
257 return (Attributes & TypeAttributes.Import) != 0;
261 public bool IsInterface {
263 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
267 public bool IsLayoutSequential {
269 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
273 public bool IsMarshalByRef {
275 return IsMarshalByRefImpl ();
279 public bool IsNestedAssembly {
281 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
285 public bool IsNestedFamANDAssem {
287 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
291 public bool IsNestedFamily {
293 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
297 public bool IsNestedFamORAssem {
299 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
303 public bool IsNestedPrivate {
305 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
309 public bool IsNestedPublic {
311 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
315 public bool IsNotPublic {
317 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
321 public bool IsPointer {
323 return IsPointerImpl ();
327 public bool IsPrimitive {
329 return IsPrimitiveImpl ();
333 public bool IsPublic {
335 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
339 public bool IsSealed {
341 return (Attributes & TypeAttributes.Sealed) != 0;
349 bool IsSerializable {
351 if ((Attributes & TypeAttributes.Serializable) != 0)
354 // Enums and delegates are always serializable
356 Type type = UnderlyingSystemType;
360 // Fast check for system types
361 if (type.IsSystemType)
362 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
364 // User defined types depend on this behavior
366 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
369 type = type.BaseType;
370 } while (type != null);
376 public bool IsSpecialName {
378 return (Attributes & TypeAttributes.SpecialName) != 0;
382 public bool IsUnicodeClass {
384 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
388 public bool IsValueType {
390 return IsValueTypeImpl ();
394 public override MemberTypes MemberType {
396 return MemberTypes.TypeInfo;
400 public abstract override Module Module {
404 public abstract string Namespace {get;}
406 public override Type ReflectedType {
412 public virtual RuntimeTypeHandle TypeHandle {
413 get { throw new ArgumentException ("Derived class must provide implementation."); }
417 public ConstructorInfo TypeInitializer {
419 return GetConstructorImpl (
420 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
422 CallingConventions.Any,
429 * This has NOTHING to do with getting the base type of an enum. Use
430 * Enum.GetUnderlyingType () for that.
432 public abstract Type UnderlyingSystemType {get;}
434 public override bool Equals (object o)
437 return Equals (o as Type);
442 Type me = UnderlyingSystemType;
445 return me.EqualsInternal (o as Type);
450 public virtual bool Equals (Type o)
452 if ((object)o == (object)this)
454 if ((object)o == null)
456 Type me = UnderlyingSystemType;
457 if ((object)me == null)
460 o = o.UnderlyingSystemType;
461 if ((object)o == null)
463 if ((object)o == (object)this)
465 return me.EqualsInternal (o);
468 public bool Equals (Type o)
475 Type me = UnderlyingSystemType;
478 return me.EqualsInternal (o.UnderlyingSystemType);
482 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
483 public static bool operator == (Type left, Type right)
485 return Object.ReferenceEquals (left, right);
488 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
489 public static bool operator != (Type left, Type right)
491 return !Object.ReferenceEquals (left, right);
494 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
495 public virtual Type GetEnumUnderlyingType () {
497 throw new ArgumentException ("Type is not an enumeration", "enumType");
499 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
501 if (fields == null || fields.Length != 1)
502 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
504 return fields [0].FieldType;
507 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
508 public virtual string[] GetEnumNames () {
510 throw new ArgumentException ("Type is not an enumeration", "enumType");
512 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
514 string [] names = new string [fields.Length];
515 if (0 != names.Length) {
516 for (int i = 0; i < fields.Length; ++i)
517 names [i] = fields [i].Name;
519 var et = GetEnumUnderlyingType ();
520 var values = Array.CreateInstance (et, names.Length);
521 for (int i = 0; i < fields.Length; ++i)
522 values.SetValue (fields [i].GetValue (null), i);
523 MonoEnumInfo.SortEnums (et, values, names);
529 static NotImplementedException CreateNIE () {
530 return new NotImplementedException ();
533 public virtual Array GetEnumValues () {
535 throw new ArgumentException ("Type is not an enumeration", "enumType");
540 bool IsValidEnumType (Type type) {
541 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
544 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
545 public virtual string GetEnumName (object value) {
547 throw new ArgumentException ("Value is null", "value");
548 if (!IsValidEnumType (value.GetType ()))
549 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
551 throw new ArgumentException ("Type is not an enumeration", "enumType");
554 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
556 for (int i = 0; i < fields.Length; ++i) {
557 var fv = fields [i].GetValue (null);
560 //XXX we can't use 'this' as argument as it might be an UserType
561 obj = Enum.ToObject (fv.GetType (), value);
562 } catch (OverflowException) {
564 } catch (InvalidCastException) {
565 throw new ArgumentException ("Value is not valid", "value");
569 return fields [i].Name;
575 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
576 public virtual bool IsEnumDefined (object value) {
578 throw new ArgumentException ("Value is null", "value");
580 throw new ArgumentException ("Type is not an enumeration", "enumType");
582 Type vt = value.GetType ();
583 if (!IsValidEnumType (vt) && vt != typeof (string))
584 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
586 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
588 if (value is string) {
589 for (int i = 0; i < fields.Length; ++i) {
590 if (fields [i].Name.Equals (value))
594 if (vt != this && vt != GetEnumUnderlyingType ())
595 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
598 for (int i = 0; i < fields.Length; ++i) {
599 var fv = fields [i].GetValue (null);
602 //XXX we can't use 'this' as argument as it might be an UserType
603 obj = Enum.ToObject (fv.GetType (), value);
604 } catch (OverflowException) {
606 } catch (InvalidCastException) {
607 throw new ArgumentException ("Value is not valid", "value");
617 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
619 return GetType (typeName, assemblyResolver, typeResolver, false, false);
622 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
624 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
627 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
629 TypeSpec spec = TypeSpec.Parse (typeName);
630 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
633 public virtual bool IsSecurityTransparent
635 get { throw CreateNIE (); }
638 public virtual bool IsSecurityCritical
640 get { throw CreateNIE (); }
643 public virtual bool IsSecuritySafeCritical
645 get { throw CreateNIE (); }
649 [MethodImplAttribute(MethodImplOptions.InternalCall)]
650 internal extern bool EqualsInternal (Type type);
652 [MethodImplAttribute(MethodImplOptions.InternalCall)]
653 private static extern Type internal_from_handle (IntPtr handle);
655 [MethodImplAttribute(MethodImplOptions.InternalCall)]
656 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
658 public static Type GetType(string typeName)
660 if (typeName == null)
661 throw new ArgumentNullException ("TypeName");
663 return internal_from_name (typeName, false, false);
666 public static Type GetType(string typeName, bool throwOnError)
668 if (typeName == null)
669 throw new ArgumentNullException ("TypeName");
671 Type type = internal_from_name (typeName, throwOnError, false);
672 if (throwOnError && type == null)
673 throw new TypeLoadException ("Error loading '" + typeName + "'");
678 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
680 if (typeName == null)
681 throw new ArgumentNullException ("TypeName");
683 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
684 if (throwOnError && t == null)
685 throw new TypeLoadException ("Error loading '" + typeName + "'");
690 public static Type[] GetTypeArray (object[] args) {
692 throw new ArgumentNullException ("args");
695 ret = new Type [args.Length];
696 for (int i = 0; i < args.Length; ++i)
697 ret [i] = args[i].GetType ();
701 [MethodImplAttribute(MethodImplOptions.InternalCall)]
702 internal extern static TypeCode GetTypeCodeInternal (Type type);
707 TypeCode GetTypeCodeImpl () {
709 if (type is MonoType)
710 return GetTypeCodeInternal (type);
711 if (type is TypeBuilder)
712 return ((TypeBuilder)type).GetTypeCodeInternal ();
714 type = type.UnderlyingSystemType;
716 if (!type.IsSystemType)
717 return TypeCode.Object;
719 return GetTypeCodeInternal (type);
722 public static TypeCode GetTypeCode (Type type) {
724 /* MS.NET returns this */
725 return TypeCode.Empty;
726 return type.GetTypeCodeImpl ();
729 [MonoTODO("This operation is currently not supported by Mono")]
730 public static Type GetTypeFromCLSID (Guid clsid)
732 throw new NotImplementedException ();
735 [MonoTODO("This operation is currently not supported by Mono")]
736 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
738 throw new NotImplementedException ();
741 [MonoTODO("This operation is currently not supported by Mono")]
742 public static Type GetTypeFromCLSID (Guid clsid, string server)
744 throw new NotImplementedException ();
747 [MonoTODO("This operation is currently not supported by Mono")]
748 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
750 throw new NotImplementedException ();
753 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
755 if (handle.Value == IntPtr.Zero)
756 // This is not consistent with the other GetXXXFromHandle methods, but
757 // MS.NET seems to do this
760 return internal_from_handle (handle.Value);
763 [MonoTODO("Mono does not support COM")]
764 public static Type GetTypeFromProgID (string progID)
766 throw new NotImplementedException ();
769 [MonoTODO("Mono does not support COM")]
770 public static Type GetTypeFromProgID (string progID, bool throwOnError)
772 throw new NotImplementedException ();
775 [MonoTODO("Mono does not support COM")]
776 public static Type GetTypeFromProgID (string progID, string server)
778 throw new NotImplementedException ();
781 [MonoTODO("Mono does not support COM")]
782 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
784 throw new NotImplementedException ();
787 public static RuntimeTypeHandle GetTypeHandle (object o)
790 throw new ArgumentNullException ();
792 return o.GetType().TypeHandle;
795 [MethodImplAttribute(MethodImplOptions.InternalCall)]
796 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
798 [MethodImplAttribute(MethodImplOptions.InternalCall)]
799 internal static extern bool type_is_assignable_from (Type a, Type b);
801 public new Type GetType ()
803 return base.GetType ();
807 public virtual bool IsSubclassOf (Type c)
809 if (c == null || c == this)
812 // Fast check for system types
814 return c.IsSystemType && type_is_subtype_of (this, c, false);
816 // User defined types depend on this behavior
817 for (Type type = BaseType; type != null; type = type.BaseType)
824 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
827 throw new ArgumentNullException ("filter");
829 var ifaces = new List<Type> ();
830 foreach (Type iface in GetInterfaces ()) {
831 if (filter (iface, filterCriteria))
835 return ifaces.ToArray ();
838 public Type GetInterface (string name) {
839 return GetInterface (name, false);
842 public abstract Type GetInterface (string name, bool ignoreCase);
844 [MethodImplAttribute(MethodImplOptions.InternalCall)]
845 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
848 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
850 throw new NotSupportedException ("Derived classes must provide an implementation.");
851 if (interfaceType == null)
852 throw new ArgumentNullException ("interfaceType");
853 if (!interfaceType.IsSystemType)
854 throw new ArgumentException ("interfaceType", "Type is an user type");
855 InterfaceMapping res;
856 if (!interfaceType.IsInterface)
857 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
859 throw new ArgumentException ("'this' type cannot be an interface itself");
860 res.TargetType = this;
861 res.InterfaceType = interfaceType;
862 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
863 if (res.TargetMethods == null)
864 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
869 public abstract Type[] GetInterfaces ();
871 public virtual bool IsAssignableFrom (Type c)
879 if (c is TypeBuilder)
880 return ((TypeBuilder)c).IsAssignableTo (this);
882 /* Handle user defined type classes */
884 Type systemType = UnderlyingSystemType;
885 if (!systemType.IsSystemType)
888 Type other = c.UnderlyingSystemType;
889 if (!other.IsSystemType)
892 return systemType.IsAssignableFrom (other);
895 if (!c.IsSystemType) {
896 Type underlyingType = c.UnderlyingSystemType;
897 if (!underlyingType.IsSystemType)
899 return IsAssignableFrom (underlyingType);
902 return type_is_assignable_from (this, c);
905 [MethodImplAttribute(MethodImplOptions.InternalCall)]
906 extern static bool IsInstanceOfType (Type type, object o);
908 public virtual bool IsInstanceOfType (object o)
910 Type type = UnderlyingSystemType;
911 if (!type.IsSystemType)
913 return IsInstanceOfType (type, o);
916 public virtual int GetArrayRank ()
918 throw new NotSupportedException (); // according to MSDN
921 public abstract Type GetElementType ();
923 public EventInfo GetEvent (string name)
925 return GetEvent (name, DefaultBindingFlags);
928 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
930 public virtual EventInfo[] GetEvents ()
932 return GetEvents (DefaultBindingFlags);
935 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
937 public FieldInfo GetField( string name)
939 return GetField (name, DefaultBindingFlags);
942 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
944 public FieldInfo[] GetFields ()
946 return GetFields (DefaultBindingFlags);
949 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
951 public override int GetHashCode()
953 Type t = UnderlyingSystemType;
954 if (t != null && t != this)
955 return t.GetHashCode ();
956 return (int)_impl.Value;
959 public MemberInfo[] GetMember (string name)
961 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
964 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
966 return GetMember (name, MemberTypes.All, bindingAttr);
969 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
972 throw new ArgumentNullException ("name");
973 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
974 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
976 return FindMembers (type, bindingAttr, FilterName, name);
979 public MemberInfo[] GetMembers ()
981 return GetMembers (DefaultBindingFlags);
984 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
986 public MethodInfo GetMethod (string name)
989 throw new ArgumentNullException ("name");
990 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
993 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
996 throw new ArgumentNullException ("name");
998 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1001 public MethodInfo GetMethod (string name, Type[] types)
1003 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1006 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1008 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1011 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1012 Type[] types, ParameterModifier[] modifiers)
1014 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1017 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1018 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1021 throw new ArgumentNullException ("name");
1023 throw new ArgumentNullException ("types");
1025 for (int i = 0; i < types.Length; i++)
1026 if (types[i] == null)
1027 throw new ArgumentNullException ("types");
1029 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1032 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1033 CallingConventions callConvention, Type[] types,
1034 ParameterModifier[] modifiers);
1036 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1037 CallingConventions callConvention, Type[] types,
1038 ParameterModifier[] modifiers)
1040 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1043 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1045 throw new System.InvalidOperationException ("can only be called in generic type");
1048 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1050 throw new System.InvalidOperationException ("can only be called in generic type");
1053 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1055 throw new System.InvalidOperationException ("can only be called in generic type");
1059 public MethodInfo[] GetMethods ()
1061 return GetMethods (DefaultBindingFlags);
1064 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1066 public Type GetNestedType (string name)
1068 return GetNestedType (name, DefaultBindingFlags);
1071 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1073 public Type[] GetNestedTypes ()
1075 return GetNestedTypes (DefaultBindingFlags);
1078 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1081 public PropertyInfo[] GetProperties ()
1083 return GetProperties (DefaultBindingFlags);
1086 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1089 public PropertyInfo GetProperty (string name)
1092 throw new ArgumentNullException ("name");
1094 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1097 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1100 throw new ArgumentNullException ("name");
1101 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1104 public PropertyInfo GetProperty (string name, Type returnType)
1107 throw new ArgumentNullException ("name");
1108 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1111 public PropertyInfo GetProperty (string name, Type[] types)
1113 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1116 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1118 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1121 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1123 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1126 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1127 Type[] types, ParameterModifier[] modifiers)
1130 throw new ArgumentNullException ("name");
1132 throw new ArgumentNullException ("types");
1134 foreach (Type t in types) {
1136 throw new ArgumentNullException ("types");
1139 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1142 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1143 Type returnType, Type[] types, ParameterModifier[] modifiers);
1145 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1146 Type returnType, Type[] types, ParameterModifier[] modifiers)
1148 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1151 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1153 CallingConventions callConvention,
1155 ParameterModifier[] modifiers);
1157 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1158 protected abstract bool HasElementTypeImpl ();
1159 protected abstract bool IsArrayImpl ();
1160 protected abstract bool IsByRefImpl ();
1161 protected abstract bool IsCOMObjectImpl ();
1162 protected abstract bool IsPointerImpl ();
1163 protected abstract bool IsPrimitiveImpl ();
1165 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1166 internal static extern bool IsArrayImpl (Type type);
1168 protected virtual bool IsValueTypeImpl ()
1170 if (this == typeof (ValueType) || this == typeof (Enum))
1173 return IsSubclassOf (typeof (ValueType));
1176 protected virtual bool IsContextfulImpl ()
1178 return typeof (ContextBoundObject).IsAssignableFrom (this);
1181 protected virtual bool IsMarshalByRefImpl ()
1183 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1187 public ConstructorInfo GetConstructor (Type[] types)
1189 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1193 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1194 Type[] types, ParameterModifier[] modifiers)
1196 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1200 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1201 CallingConventions callConvention,
1202 Type[] types, ParameterModifier[] modifiers)
1205 throw new ArgumentNullException ("types");
1207 foreach (Type t in types) {
1209 throw new ArgumentNullException ("types");
1212 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1216 public ConstructorInfo[] GetConstructors ()
1218 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1222 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1224 public virtual MemberInfo[] GetDefaultMembers ()
1226 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1227 if (att.Length == 0)
1228 return new MemberInfo [0];
1230 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1231 return (member != null) ? member : new MemberInfo [0];
1234 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1235 MemberFilter filter, object filterCriteria)
1237 MemberInfo[] result;
1238 ArrayList l = new ArrayList ();
1240 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1241 // this.FullName, this.GetType().FullName, this.obj_address());
1242 if ((memberType & MemberTypes.Method) != 0) {
1243 MethodInfo[] c = GetMethods (bindingAttr);
1244 if (filter != null) {
1245 foreach (MemberInfo m in c) {
1246 if (filter (m, filterCriteria))
1253 if ((memberType & MemberTypes.Constructor) != 0) {
1254 ConstructorInfo[] c = GetConstructors (bindingAttr);
1255 if (filter != null) {
1256 foreach (MemberInfo m in c) {
1257 if (filter (m, filterCriteria))
1264 if ((memberType & MemberTypes.Property) != 0) {
1265 PropertyInfo[] c = GetProperties (bindingAttr);
1268 if (filter != null) {
1269 foreach (MemberInfo m in c) {
1270 if (filter (m, filterCriteria))
1278 if ((memberType & MemberTypes.Event) != 0) {
1279 EventInfo[] c = GetEvents (bindingAttr);
1280 if (filter != null) {
1281 foreach (MemberInfo m in c) {
1282 if (filter (m, filterCriteria))
1289 if ((memberType & MemberTypes.Field) != 0) {
1290 FieldInfo[] c = GetFields (bindingAttr);
1291 if (filter != null) {
1292 foreach (MemberInfo m in c) {
1293 if (filter (m, filterCriteria))
1300 if ((memberType & MemberTypes.NestedType) != 0) {
1301 Type[] c = GetNestedTypes (bindingAttr);
1302 if (filter != null) {
1303 foreach (MemberInfo m in c) {
1304 if (filter (m, filterCriteria)) {
1313 switch (memberType) {
1314 case MemberTypes.Constructor :
1315 result = new ConstructorInfo [l.Count];
1317 case MemberTypes.Event :
1318 result = new EventInfo [l.Count];
1320 case MemberTypes.Field :
1321 result = new FieldInfo [l.Count];
1323 case MemberTypes.Method :
1324 result = new MethodInfo [l.Count];
1326 case MemberTypes.NestedType :
1327 case MemberTypes.TypeInfo :
1328 result = new Type [l.Count];
1330 case MemberTypes.Property :
1331 result = new PropertyInfo [l.Count];
1334 result = new MemberInfo [l.Count];
1342 [DebuggerStepThrough]
1343 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1345 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1349 [DebuggerStepThrough]
1350 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1351 object target, object[] args, CultureInfo culture)
1353 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1356 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1357 Binder binder, object target, object[] args,
1358 ParameterModifier[] modifiers,
1359 CultureInfo culture, string[] namedParameters);
1361 public override string ToString()
1366 internal virtual Type InternalResolve ()
1368 return UnderlyingSystemType;
1371 internal bool IsSystemType {
1373 return _impl.Value != IntPtr.Zero;
1378 public virtual Type[] GenericTypeArguments {
1380 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1385 public virtual Type[] GetGenericArguments ()
1387 throw new NotSupportedException ();
1390 public virtual bool ContainsGenericParameters {
1391 get { return false; }
1394 public virtual extern bool IsGenericTypeDefinition {
1395 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1399 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1400 internal extern Type GetGenericTypeDefinition_impl ();
1402 public virtual Type GetGenericTypeDefinition ()
1404 throw new NotSupportedException ("Derived classes must provide an implementation.");
1407 public virtual extern bool IsGenericType {
1408 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1412 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1413 static extern Type MakeGenericType (Type gt, Type [] types);
1415 public virtual Type MakeGenericType (params Type[] typeArguments)
1418 throw new NotSupportedException ();
1419 if (!IsGenericTypeDefinition)
1420 throw new InvalidOperationException ("not a generic type definition");
1421 if (typeArguments == null)
1422 throw new ArgumentNullException ("typeArguments");
1423 if (GetGenericArguments().Length != typeArguments.Length)
1424 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");
1426 bool hasUserType = false;
1428 Type[] systemTypes = new Type[typeArguments.Length];
1429 for (int i = 0; i < typeArguments.Length; ++i) {
1430 Type t = typeArguments [i];
1432 throw new ArgumentNullException ("typeArguments");
1434 if (!(t is MonoType))
1436 systemTypes [i] = t;
1440 return new MonoGenericClass (this, typeArguments);
1443 Type res = MakeGenericType (this, systemTypes);
1445 throw new TypeLoadException ();
1449 public virtual bool IsGenericParameter {
1455 public bool IsNested {
1457 return DeclaringType != null;
1461 public bool IsVisible {
1464 return DeclaringType.IsVisible;
1470 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1471 extern int GetGenericParameterPosition ();
1473 public virtual int GenericParameterPosition {
1475 int res = GetGenericParameterPosition ();
1477 throw new InvalidOperationException ();
1482 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1483 extern GenericParameterAttributes GetGenericParameterAttributes ();
1485 public virtual GenericParameterAttributes GenericParameterAttributes {
1488 throw new NotSupportedException ("Derived classes must provide an implementation.");
1490 if (!IsGenericParameter)
1491 throw new InvalidOperationException ();
1493 return GetGenericParameterAttributes ();
1497 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1498 extern Type[] GetGenericParameterConstraints_impl ();
1500 public virtual Type[] GetGenericParameterConstraints ()
1503 throw new InvalidOperationException ();
1505 if (!IsGenericParameter)
1506 throw new InvalidOperationException ();
1508 return GetGenericParameterConstraints_impl ();
1511 public virtual MethodBase DeclaringMethod {
1517 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1518 extern Type make_array_type (int rank);
1520 public virtual Type MakeArrayType ()
1523 throw new NotSupportedException ("Derived classes must provide an implementation.");
1524 return make_array_type (0);
1527 public virtual Type MakeArrayType (int rank)
1530 throw new NotSupportedException ("Derived classes must provide an implementation.");
1531 if (rank < 1 || rank > 255)
1532 throw new IndexOutOfRangeException ();
1533 return make_array_type (rank);
1536 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1537 extern Type make_byref_type ();
1539 public virtual Type MakeByRefType ()
1542 throw new NotSupportedException ("Derived classes must provide an implementation.");
1544 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1545 return make_byref_type ();
1548 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1549 static extern Type MakePointerType (Type type);
1551 public virtual Type MakePointerType ()
1554 throw new NotSupportedException ("Derived classes must provide an implementation.");
1555 return MakePointerType (this);
1558 public static Type ReflectionOnlyGetType (string typeName,
1559 bool throwIfNotFound,
1562 if (typeName == null)
1563 throw new ArgumentNullException ("typeName");
1564 int idx = typeName.IndexOf (',');
1565 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1566 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1567 string an = typeName.Substring (idx + 1);
1570 a = Assembly.ReflectionOnlyLoad (an);
1572 if (throwIfNotFound)
1576 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1579 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1580 extern void GetPacking (out int packing, out int size);
1582 public virtual StructLayoutAttribute StructLayoutAttribute {
1585 throw new NotSupportedException ();
1587 return GetStructLayoutAttribute ();
1592 internal StructLayoutAttribute GetStructLayoutAttribute ()
1596 if (IsLayoutSequential)
1597 kind = LayoutKind.Sequential;
1598 else if (IsExplicitLayout)
1599 kind = LayoutKind.Explicit;
1601 kind = LayoutKind.Auto;
1603 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1606 attr.CharSet = CharSet.Unicode;
1607 else if (IsAnsiClass)
1608 attr.CharSet = CharSet.Ansi;
1610 attr.CharSet = CharSet.Auto;
1612 if (kind != LayoutKind.Auto) {
1614 GetPacking (out packing, out attr.Size);
1615 // 0 means no data provided, we end up with default value
1617 attr.Pack = packing;
1623 internal object[] GetPseudoCustomAttributes ()
1627 /* IsSerializable returns true for delegates/enums as well */
1628 if ((Attributes & TypeAttributes.Serializable) != 0)
1630 if ((Attributes & TypeAttributes.Import) != 0)
1635 object[] attrs = new object [count];
1638 if ((Attributes & TypeAttributes.Serializable) != 0)
1639 attrs [count ++] = new SerializableAttribute ();
1640 if ((Attributes & TypeAttributes.Import) != 0)
1641 attrs [count ++] = new ComImportAttribute ();
1648 public virtual bool IsEquivalentTo (Type other)
1650 return this == other;
1655 * Return whenever this object is an instance of a user defined subclass
1656 * of System.Type or an instance of TypeDelegator.
1657 * A user defined type is not simply the opposite of a system type.
1658 * It's any class that's neither a SRE or runtime baked type.
1660 internal virtual bool IsUserType {
1666 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1668 throw new NotImplementedException ();
1671 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1673 throw new NotImplementedException ();
1676 void _Type.GetTypeInfoCount (out uint pcTInfo)
1678 throw new NotImplementedException ();
1681 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1683 throw new NotImplementedException ();