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 public abstract class Type : MemberInfo, IReflect, _Type {
51 internal RuntimeTypeHandle _impl;
53 public static readonly char Delimiter = '.';
54 public static readonly Type[] EmptyTypes = {};
55 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
56 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
57 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
58 public static readonly object Missing = System.Reflection.Missing.Value;
60 internal const BindingFlags DefaultBindingFlags =
61 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
63 /* implementation of the delegates for MemberFilter */
64 static bool FilterName_impl (MemberInfo m, object filterCriteria)
66 string name = (string) filterCriteria;
67 if (name == null || name.Length == 0 )
68 return false; // because m.Name cannot be null or empty
70 if (name [name.Length-1] == '*')
71 return string.CompareOrdinal (name, 0, m.Name, 0, name.Length-1) == 0;
73 return name.Equals (m.Name);
76 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
78 string name = (string) filterCriteria;
79 if (name == null || name.Length == 0 )
80 return false; // because m.Name cannot be null or empty
82 if (name [name.Length-1] == '*')
83 return string.Compare (name, 0, m.Name, 0, name.Length-1, StringComparison.OrdinalIgnoreCase) == 0;
85 return string.Equals (name, m.Name, StringComparison.OrdinalIgnoreCase);
88 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
90 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
92 return ((int)((MethodInfo)m).Attributes & flags) != 0;
94 return ((int)((FieldInfo)m).Attributes & flags) != 0;
95 if (m is PropertyInfo)
96 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
98 return ((int)((EventInfo)m).Attributes & flags) != 0;
107 /// The assembly where the type is defined.
109 public abstract Assembly Assembly {
114 /// Gets the fully qualified name for the type including the
115 /// assembly name where the type is defined.
117 public abstract string AssemblyQualifiedName {
122 /// Returns the Attributes associated with the type.
124 public TypeAttributes Attributes {
126 return GetAttributeFlagsImpl ();
131 /// Returns the basetype for this type
133 public abstract Type BaseType {
138 /// Returns the class that declares the member.
140 public override Type DeclaringType {
149 public static Binder DefaultBinder {
151 return Binder.DefaultBinder;
156 /// The full name of the type including its namespace
158 public abstract string FullName {
162 public abstract Guid GUID {
166 public bool HasElementType {
168 return HasElementTypeImpl ();
172 public bool IsAbstract {
174 return (Attributes & TypeAttributes.Abstract) != 0;
178 public bool IsAnsiClass {
180 return (Attributes & TypeAttributes.StringFormatMask)
181 == TypeAttributes.AnsiClass;
185 public bool IsArray {
187 return IsArrayImpl ();
191 public bool IsAutoClass {
193 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
197 public bool IsAutoLayout {
199 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
203 public bool IsByRef {
205 return IsByRefImpl ();
209 public bool IsClass {
218 public bool IsCOMObject {
220 return IsCOMObjectImpl ();
224 public bool IsContextful {
226 return IsContextfulImpl ();
236 return IsSubclassOf (typeof (Enum));
240 public bool IsExplicitLayout {
242 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
246 public bool IsImport {
248 return (Attributes & TypeAttributes.Import) != 0;
252 public bool IsInterface {
254 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
258 public bool IsLayoutSequential {
260 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
264 public bool IsMarshalByRef {
266 return IsMarshalByRefImpl ();
270 public bool IsNestedAssembly {
272 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
276 public bool IsNestedFamANDAssem {
278 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
282 public bool IsNestedFamily {
284 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
288 public bool IsNestedFamORAssem {
290 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
294 public bool IsNestedPrivate {
296 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
300 public bool IsNestedPublic {
302 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
306 public bool IsNotPublic {
308 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
312 public bool IsPointer {
314 return IsPointerImpl ();
318 public bool IsPrimitive {
320 return IsPrimitiveImpl ();
324 public bool IsPublic {
326 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
330 public bool IsSealed {
332 return (Attributes & TypeAttributes.Sealed) != 0;
340 bool IsSerializable {
342 if ((Attributes & TypeAttributes.Serializable) != 0)
345 // Enums and delegates are always serializable
347 Type type = UnderlyingSystemType;
351 // Fast check for system types
352 if (type.IsSystemType)
353 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
355 // User defined types depend on this behavior
357 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
360 type = type.BaseType;
361 } while (type != null);
367 public bool IsSpecialName {
369 return (Attributes & TypeAttributes.SpecialName) != 0;
373 public bool IsUnicodeClass {
375 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
379 public bool IsValueType {
381 return IsValueTypeImpl ();
385 public override MemberTypes MemberType {
387 return MemberTypes.TypeInfo;
391 public abstract override Module Module {
395 public abstract string Namespace {get;}
397 public override Type ReflectedType {
403 public virtual RuntimeTypeHandle TypeHandle {
404 get { throw new ArgumentException ("Derived class must provide implementation."); }
408 public ConstructorInfo TypeInitializer {
410 return GetConstructorImpl (
411 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
413 CallingConventions.Any,
420 * This has NOTHING to do with getting the base type of an enum. Use
421 * Enum.GetUnderlyingType () for that.
423 public abstract Type UnderlyingSystemType {get;}
425 public override bool Equals (object o)
428 return Equals (o as Type);
433 Type me = UnderlyingSystemType;
436 return me.EqualsInternal (o as Type);
441 public virtual bool Equals (Type o)
443 if ((object)o == (object)this)
445 if ((object)o == null)
447 Type me = UnderlyingSystemType;
448 if ((object)me == null)
451 o = o.UnderlyingSystemType;
452 if ((object)o == null)
454 if ((object)o == (object)this)
456 return me.EqualsInternal (o);
459 public bool Equals (Type o)
466 Type me = UnderlyingSystemType;
469 return me.EqualsInternal (o.UnderlyingSystemType);
473 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
474 public static bool operator == (Type left, Type right)
476 return Object.ReferenceEquals (left, right);
479 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
480 public static bool operator != (Type left, Type right)
482 return !Object.ReferenceEquals (left, right);
485 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
486 public virtual Type GetEnumUnderlyingType () {
488 throw new ArgumentException ("Type is not an enumeration", "enumType");
490 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
492 if (fields == null || fields.Length != 1)
493 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
495 return fields [0].FieldType;
498 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
499 public virtual string[] GetEnumNames () {
501 throw new ArgumentException ("Type is not an enumeration", "enumType");
503 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
505 string [] result = new string [fields.Length];
506 for (int i = 0; i < fields.Length; ++i)
507 result [i] = fields [i].Name;
512 static NotImplementedException CreateNIE () {
513 return new NotImplementedException ();
516 public virtual Array GetEnumValues () {
518 throw new ArgumentException ("Type is not an enumeration", "enumType");
523 bool IsValidEnumType (Type type) {
524 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
527 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
528 public virtual string GetEnumName (object value) {
530 throw new ArgumentException ("Value is null", "value");
531 if (!IsValidEnumType (value.GetType ()))
532 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
534 throw new ArgumentException ("Type is not an enumeration", "enumType");
537 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
539 for (int i = 0; i < fields.Length; ++i) {
540 var fv = fields [i].GetValue (null);
543 //XXX we can't use 'this' as argument as it might be an UserType
544 obj = Enum.ToObject (fv.GetType (), value);
545 } catch (OverflowException) {
547 } catch (InvalidCastException) {
548 throw new ArgumentException ("Value is not valid", "value");
552 return fields [i].Name;
558 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
559 public virtual bool IsEnumDefined (object value) {
561 throw new ArgumentException ("Value is null", "value");
563 throw new ArgumentException ("Type is not an enumeration", "enumType");
565 Type vt = value.GetType ();
566 if (!IsValidEnumType (vt) && vt != typeof (string))
567 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
569 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
571 if (value is string) {
572 for (int i = 0; i < fields.Length; ++i) {
573 if (fields [i].Name.Equals (value))
577 if (vt != this && vt != GetEnumUnderlyingType ())
578 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
581 for (int i = 0; i < fields.Length; ++i) {
582 var fv = fields [i].GetValue (null);
585 //XXX we can't use 'this' as argument as it might be an UserType
586 obj = Enum.ToObject (fv.GetType (), value);
587 } catch (OverflowException) {
589 } catch (InvalidCastException) {
590 throw new ArgumentException ("Value is not valid", "value");
600 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
602 return GetType (typeName, assemblyResolver, typeResolver, false, false);
605 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
607 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
610 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
612 TypeSpec spec = TypeSpec.Parse (typeName);
613 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
616 public virtual bool IsSecurityTransparent
618 get { throw CreateNIE (); }
621 public virtual bool IsSecurityCritical
623 get { throw CreateNIE (); }
626 public virtual bool IsSecuritySafeCritical
628 get { throw CreateNIE (); }
632 [MethodImplAttribute(MethodImplOptions.InternalCall)]
633 internal extern bool EqualsInternal (Type type);
635 [MethodImplAttribute(MethodImplOptions.InternalCall)]
636 private static extern Type internal_from_handle (IntPtr handle);
638 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
641 public static Type GetType(string typeName)
643 if (typeName == null)
644 throw new ArgumentNullException ("TypeName");
646 return internal_from_name (typeName, false, false);
649 public static Type GetType(string typeName, bool throwOnError)
651 if (typeName == null)
652 throw new ArgumentNullException ("TypeName");
654 Type type = internal_from_name (typeName, throwOnError, false);
655 if (throwOnError && type == null)
656 throw new TypeLoadException ("Error loading '" + typeName + "'");
661 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
663 if (typeName == null)
664 throw new ArgumentNullException ("TypeName");
666 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
667 if (throwOnError && t == null)
668 throw new TypeLoadException ("Error loading '" + typeName + "'");
673 public static Type[] GetTypeArray (object[] args) {
675 throw new ArgumentNullException ("args");
678 ret = new Type [args.Length];
679 for (int i = 0; i < args.Length; ++i)
680 ret [i] = args[i].GetType ();
684 [MethodImplAttribute(MethodImplOptions.InternalCall)]
685 internal extern static TypeCode GetTypeCodeInternal (Type type);
690 TypeCode GetTypeCodeImpl () {
692 if (type is MonoType)
693 return GetTypeCodeInternal (type);
694 if (type is TypeBuilder)
695 return ((TypeBuilder)type).GetTypeCodeInternal ();
697 type = type.UnderlyingSystemType;
699 if (!type.IsSystemType)
700 return TypeCode.Object;
702 return GetTypeCodeInternal (type);
705 public static TypeCode GetTypeCode (Type type) {
707 /* MS.NET returns this */
708 return TypeCode.Empty;
709 return type.GetTypeCodeImpl ();
712 [MonoTODO("This operation is currently not supported by Mono")]
713 public static Type GetTypeFromCLSID (Guid clsid)
715 throw new NotImplementedException ();
718 [MonoTODO("This operation is currently not supported by Mono")]
719 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
721 throw new NotImplementedException ();
724 [MonoTODO("This operation is currently not supported by Mono")]
725 public static Type GetTypeFromCLSID (Guid clsid, string server)
727 throw new NotImplementedException ();
730 [MonoTODO("This operation is currently not supported by Mono")]
731 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
733 throw new NotImplementedException ();
736 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
738 if (handle.Value == IntPtr.Zero)
739 // This is not consistent with the other GetXXXFromHandle methods, but
740 // MS.NET seems to do this
743 return internal_from_handle (handle.Value);
746 [MonoTODO("Mono does not support COM")]
747 public static Type GetTypeFromProgID (string progID)
749 throw new NotImplementedException ();
752 [MonoTODO("Mono does not support COM")]
753 public static Type GetTypeFromProgID (string progID, bool throwOnError)
755 throw new NotImplementedException ();
758 [MonoTODO("Mono does not support COM")]
759 public static Type GetTypeFromProgID (string progID, string server)
761 throw new NotImplementedException ();
764 [MonoTODO("Mono does not support COM")]
765 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
767 throw new NotImplementedException ();
770 public static RuntimeTypeHandle GetTypeHandle (object o)
773 throw new ArgumentNullException ();
775 return o.GetType().TypeHandle;
778 [MethodImplAttribute(MethodImplOptions.InternalCall)]
779 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
781 [MethodImplAttribute(MethodImplOptions.InternalCall)]
782 internal static extern bool type_is_assignable_from (Type a, Type b);
784 public new Type GetType ()
786 return base.GetType ();
790 public virtual bool IsSubclassOf (Type c)
792 if (c == null || c == this)
795 // Fast check for system types
797 return c.IsSystemType && type_is_subtype_of (this, c, false);
799 // User defined types depend on this behavior
800 for (Type type = BaseType; type != null; type = type.BaseType)
807 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
810 throw new ArgumentNullException ("filter");
812 var ifaces = new List<Type> ();
813 foreach (Type iface in GetInterfaces ()) {
814 if (filter (iface, filterCriteria))
818 return ifaces.ToArray ();
821 public Type GetInterface (string name) {
822 return GetInterface (name, false);
825 public abstract Type GetInterface (string name, bool ignoreCase);
827 [MethodImplAttribute(MethodImplOptions.InternalCall)]
828 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
831 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
833 throw new NotSupportedException ("Derived classes must provide an implementation.");
834 if (interfaceType == null)
835 throw new ArgumentNullException ("interfaceType");
836 if (!interfaceType.IsSystemType)
837 throw new ArgumentException ("interfaceType", "Type is an user type");
838 InterfaceMapping res;
839 if (!interfaceType.IsInterface)
840 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
842 throw new ArgumentException ("'this' type cannot be an interface itself");
843 res.TargetType = this;
844 res.InterfaceType = interfaceType;
845 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
846 if (res.TargetMethods == null)
847 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
852 public abstract Type[] GetInterfaces ();
854 public virtual bool IsAssignableFrom (Type c)
862 if (c is TypeBuilder)
863 return ((TypeBuilder)c).IsAssignableTo (this);
865 /* Handle user defined type classes */
867 Type systemType = UnderlyingSystemType;
868 if (!systemType.IsSystemType)
871 Type other = c.UnderlyingSystemType;
872 if (!other.IsSystemType)
875 return systemType.IsAssignableFrom (other);
878 if (!c.IsSystemType) {
879 Type underlyingType = c.UnderlyingSystemType;
880 if (!underlyingType.IsSystemType)
882 return IsAssignableFrom (underlyingType);
885 return type_is_assignable_from (this, c);
888 [MethodImplAttribute(MethodImplOptions.InternalCall)]
889 extern static bool IsInstanceOfType (Type type, object o);
891 public virtual bool IsInstanceOfType (object o)
893 Type type = UnderlyingSystemType;
894 if (!type.IsSystemType)
896 return IsInstanceOfType (type, o);
899 public virtual int GetArrayRank ()
901 throw new NotSupportedException (); // according to MSDN
904 public abstract Type GetElementType ();
906 public EventInfo GetEvent (string name)
908 return GetEvent (name, DefaultBindingFlags);
911 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
913 public virtual EventInfo[] GetEvents ()
915 return GetEvents (DefaultBindingFlags);
918 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
920 public FieldInfo GetField( string name)
922 return GetField (name, DefaultBindingFlags);
925 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
927 public FieldInfo[] GetFields ()
929 return GetFields (DefaultBindingFlags);
932 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
934 public override int GetHashCode()
936 Type t = UnderlyingSystemType;
937 if (t != null && t != this)
938 return t.GetHashCode ();
939 return (int)_impl.Value;
942 public MemberInfo[] GetMember (string name)
944 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
947 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
949 return GetMember (name, MemberTypes.All, bindingAttr);
952 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
955 throw new ArgumentNullException ("name");
956 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
957 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
959 return FindMembers (type, bindingAttr, FilterName, name);
962 public MemberInfo[] GetMembers ()
964 return GetMembers (DefaultBindingFlags);
967 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
969 public MethodInfo GetMethod (string name)
972 throw new ArgumentNullException ("name");
973 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
976 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
979 throw new ArgumentNullException ("name");
981 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
984 public MethodInfo GetMethod (string name, Type[] types)
986 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
989 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
991 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
994 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
995 Type[] types, ParameterModifier[] modifiers)
997 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1000 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1001 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1004 throw new ArgumentNullException ("name");
1006 throw new ArgumentNullException ("types");
1008 for (int i = 0; i < types.Length; i++)
1009 if (types[i] == null)
1010 throw new ArgumentNullException ("types");
1012 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1015 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1016 CallingConventions callConvention, Type[] types,
1017 ParameterModifier[] modifiers);
1019 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1020 CallingConventions callConvention, Type[] types,
1021 ParameterModifier[] modifiers)
1023 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1026 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1028 throw new System.InvalidOperationException ("can only be called in generic type");
1031 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1033 throw new System.InvalidOperationException ("can only be called in generic type");
1036 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1038 throw new System.InvalidOperationException ("can only be called in generic type");
1042 public MethodInfo[] GetMethods ()
1044 return GetMethods (DefaultBindingFlags);
1047 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1049 public Type GetNestedType (string name)
1051 return GetNestedType (name, DefaultBindingFlags);
1054 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1056 public Type[] GetNestedTypes ()
1058 return GetNestedTypes (DefaultBindingFlags);
1061 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1064 public PropertyInfo[] GetProperties ()
1066 return GetProperties (DefaultBindingFlags);
1069 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1072 public PropertyInfo GetProperty (string name)
1075 throw new ArgumentNullException ("name");
1077 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1080 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1083 throw new ArgumentNullException ("name");
1084 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1087 public PropertyInfo GetProperty (string name, Type returnType)
1090 throw new ArgumentNullException ("name");
1091 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1094 public PropertyInfo GetProperty (string name, Type[] types)
1096 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1099 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1101 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1104 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1106 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1109 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1110 Type[] types, ParameterModifier[] modifiers)
1113 throw new ArgumentNullException ("name");
1115 throw new ArgumentNullException ("types");
1117 foreach (Type t in types) {
1119 throw new ArgumentNullException ("types");
1122 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1125 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1126 Type returnType, Type[] types, ParameterModifier[] modifiers);
1128 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1129 Type returnType, Type[] types, ParameterModifier[] modifiers)
1131 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1134 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1136 CallingConventions callConvention,
1138 ParameterModifier[] modifiers);
1140 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1141 protected abstract bool HasElementTypeImpl ();
1142 protected abstract bool IsArrayImpl ();
1143 protected abstract bool IsByRefImpl ();
1144 protected abstract bool IsCOMObjectImpl ();
1145 protected abstract bool IsPointerImpl ();
1146 protected abstract bool IsPrimitiveImpl ();
1148 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1149 internal static extern bool IsArrayImpl (Type type);
1151 protected virtual bool IsValueTypeImpl ()
1153 if (this == typeof (ValueType) || this == typeof (Enum))
1156 return IsSubclassOf (typeof (ValueType));
1159 protected virtual bool IsContextfulImpl ()
1161 return typeof (ContextBoundObject).IsAssignableFrom (this);
1164 protected virtual bool IsMarshalByRefImpl ()
1166 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1170 public ConstructorInfo GetConstructor (Type[] types)
1172 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1176 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1177 Type[] types, ParameterModifier[] modifiers)
1179 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1183 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1184 CallingConventions callConvention,
1185 Type[] types, ParameterModifier[] modifiers)
1188 throw new ArgumentNullException ("types");
1190 foreach (Type t in types) {
1192 throw new ArgumentNullException ("types");
1195 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1199 public ConstructorInfo[] GetConstructors ()
1201 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1205 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1207 public virtual MemberInfo[] GetDefaultMembers ()
1209 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1210 if (att.Length == 0)
1211 return new MemberInfo [0];
1213 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1214 return (member != null) ? member : new MemberInfo [0];
1217 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1218 MemberFilter filter, object filterCriteria)
1220 MemberInfo[] result;
1221 ArrayList l = new ArrayList ();
1223 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1224 // this.FullName, this.GetType().FullName, this.obj_address());
1225 if ((memberType & MemberTypes.Method) != 0) {
1226 MethodInfo[] c = GetMethods (bindingAttr);
1227 if (filter != null) {
1228 foreach (MemberInfo m in c) {
1229 if (filter (m, filterCriteria))
1236 if ((memberType & MemberTypes.Constructor) != 0) {
1237 ConstructorInfo[] c = GetConstructors (bindingAttr);
1238 if (filter != null) {
1239 foreach (MemberInfo m in c) {
1240 if (filter (m, filterCriteria))
1247 if ((memberType & MemberTypes.Property) != 0) {
1248 PropertyInfo[] c = GetProperties (bindingAttr);
1251 if (filter != null) {
1252 foreach (MemberInfo m in c) {
1253 if (filter (m, filterCriteria))
1261 if ((memberType & MemberTypes.Event) != 0) {
1262 EventInfo[] c = GetEvents (bindingAttr);
1263 if (filter != null) {
1264 foreach (MemberInfo m in c) {
1265 if (filter (m, filterCriteria))
1272 if ((memberType & MemberTypes.Field) != 0) {
1273 FieldInfo[] c = GetFields (bindingAttr);
1274 if (filter != null) {
1275 foreach (MemberInfo m in c) {
1276 if (filter (m, filterCriteria))
1283 if ((memberType & MemberTypes.NestedType) != 0) {
1284 Type[] c = GetNestedTypes (bindingAttr);
1285 if (filter != null) {
1286 foreach (MemberInfo m in c) {
1287 if (filter (m, filterCriteria)) {
1296 switch (memberType) {
1297 case MemberTypes.Constructor :
1298 result = new ConstructorInfo [l.Count];
1300 case MemberTypes.Event :
1301 result = new EventInfo [l.Count];
1303 case MemberTypes.Field :
1304 result = new FieldInfo [l.Count];
1306 case MemberTypes.Method :
1307 result = new MethodInfo [l.Count];
1309 case MemberTypes.NestedType :
1310 case MemberTypes.TypeInfo :
1311 result = new Type [l.Count];
1313 case MemberTypes.Property :
1314 result = new PropertyInfo [l.Count];
1317 result = new MemberInfo [l.Count];
1325 [DebuggerStepThrough]
1326 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1328 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1332 [DebuggerStepThrough]
1333 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1334 object target, object[] args, CultureInfo culture)
1336 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1339 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1340 Binder binder, object target, object[] args,
1341 ParameterModifier[] modifiers,
1342 CultureInfo culture, string[] namedParameters);
1344 public override string ToString()
1349 internal virtual Type InternalResolve ()
1351 return UnderlyingSystemType;
1354 internal bool IsSystemType {
1356 return _impl.Value != IntPtr.Zero;
1360 public virtual Type[] GetGenericArguments ()
1362 throw new NotSupportedException ();
1365 public virtual bool ContainsGenericParameters {
1366 get { return false; }
1369 public virtual extern bool IsGenericTypeDefinition {
1370 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1374 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1375 internal extern Type GetGenericTypeDefinition_impl ();
1377 public virtual Type GetGenericTypeDefinition ()
1379 throw new NotSupportedException ("Derived classes must provide an implementation.");
1382 public virtual extern bool IsGenericType {
1383 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1387 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1388 static extern Type MakeGenericType (Type gt, Type [] types);
1390 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1392 if (type.Assembly is AssemblyBuilder)
1393 return (AssemblyBuilder)type.Assembly;
1395 if (type.HasElementType)
1396 return PeelAssemblyBuilder (type.GetElementType ());
1398 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1401 foreach (Type arg in type.GetGenericArguments ()) {
1402 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1409 public virtual Type MakeGenericType (params Type[] typeArguments)
1412 throw new NotSupportedException ();
1413 if (!IsGenericTypeDefinition)
1414 throw new InvalidOperationException ("not a generic type definition");
1415 if (typeArguments == null)
1416 throw new ArgumentNullException ("typeArguments");
1417 if (GetGenericArguments().Length != typeArguments.Length)
1418 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");
1420 bool hasUserType = false;
1422 Type[] systemTypes = new Type[typeArguments.Length];
1423 for (int i = 0; i < typeArguments.Length; ++i) {
1424 Type t = typeArguments [i];
1426 throw new ArgumentNullException ("typeArguments");
1428 if (!(t is MonoType))
1430 systemTypes [i] = t;
1434 return new MonoGenericClass (this, typeArguments);
1437 Type res = MakeGenericType (this, systemTypes);
1439 throw new TypeLoadException ();
1443 public virtual bool IsGenericParameter {
1449 public bool IsNested {
1451 return DeclaringType != null;
1455 public bool IsVisible {
1458 return DeclaringType.IsVisible;
1464 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1465 extern int GetGenericParameterPosition ();
1467 public virtual int GenericParameterPosition {
1469 int res = GetGenericParameterPosition ();
1471 throw new InvalidOperationException ();
1476 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1477 extern GenericParameterAttributes GetGenericParameterAttributes ();
1479 public virtual GenericParameterAttributes GenericParameterAttributes {
1482 throw new NotSupportedException ("Derived classes must provide an implementation.");
1484 if (!IsGenericParameter)
1485 throw new InvalidOperationException ();
1487 return GetGenericParameterAttributes ();
1491 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1492 extern Type[] GetGenericParameterConstraints_impl ();
1494 public virtual Type[] GetGenericParameterConstraints ()
1497 throw new InvalidOperationException ();
1499 if (!IsGenericParameter)
1500 throw new InvalidOperationException ();
1502 return GetGenericParameterConstraints_impl ();
1505 public virtual MethodBase DeclaringMethod {
1511 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1512 extern Type make_array_type (int rank);
1514 public virtual Type MakeArrayType ()
1517 throw new NotSupportedException ("Derived classes must provide an implementation.");
1518 return make_array_type (0);
1521 public virtual Type MakeArrayType (int rank)
1524 throw new NotSupportedException ("Derived classes must provide an implementation.");
1525 if (rank < 1 || rank > 255)
1526 throw new IndexOutOfRangeException ();
1527 return make_array_type (rank);
1530 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1531 extern Type make_byref_type ();
1533 public virtual Type MakeByRefType ()
1536 throw new NotSupportedException ("Derived classes must provide an implementation.");
1538 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1539 return make_byref_type ();
1542 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1543 static extern Type MakePointerType (Type type);
1545 public virtual Type MakePointerType ()
1548 throw new NotSupportedException ("Derived classes must provide an implementation.");
1549 return MakePointerType (this);
1552 public static Type ReflectionOnlyGetType (string typeName,
1553 bool throwIfNotFound,
1556 if (typeName == null)
1557 throw new ArgumentNullException ("typeName");
1558 int idx = typeName.IndexOf (',');
1559 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1560 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1561 string an = typeName.Substring (idx + 1);
1564 a = Assembly.ReflectionOnlyLoad (an);
1566 if (throwIfNotFound)
1570 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1573 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1574 extern void GetPacking (out int packing, out int size);
1576 public virtual StructLayoutAttribute StructLayoutAttribute {
1579 throw new NotSupportedException ();
1581 return GetStructLayoutAttribute ();
1586 internal StructLayoutAttribute GetStructLayoutAttribute ()
1590 if (IsLayoutSequential)
1591 kind = LayoutKind.Sequential;
1592 else if (IsExplicitLayout)
1593 kind = LayoutKind.Explicit;
1595 kind = LayoutKind.Auto;
1597 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1600 attr.CharSet = CharSet.Unicode;
1601 else if (IsAnsiClass)
1602 attr.CharSet = CharSet.Ansi;
1604 attr.CharSet = CharSet.Auto;
1606 if (kind != LayoutKind.Auto) {
1608 GetPacking (out packing, out attr.Size);
1609 // 0 means no data provided, we end up with default value
1611 attr.Pack = packing;
1617 internal object[] GetPseudoCustomAttributes ()
1621 /* IsSerializable returns true for delegates/enums as well */
1622 if ((Attributes & TypeAttributes.Serializable) != 0)
1624 if ((Attributes & TypeAttributes.Import) != 0)
1629 object[] attrs = new object [count];
1632 if ((Attributes & TypeAttributes.Serializable) != 0)
1633 attrs [count ++] = new SerializableAttribute ();
1634 if ((Attributes & TypeAttributes.Import) != 0)
1635 attrs [count ++] = new ComImportAttribute ();
1642 public virtual bool IsEquivalentTo (Type other)
1644 return this == other;
1649 * Return whenever this object is an instance of a user defined subclass
1650 * of System.Type or an instance of TypeDelegator.
1652 internal bool IsUserType {
1655 * subclasses cannot modify _impl so if it is zero, it means the
1656 * type is not created by the runtime.
1658 return _impl.Value == IntPtr.Zero &&
1659 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1663 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1665 throw new NotImplementedException ();
1668 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1670 throw new NotImplementedException ();
1673 void _Type.GetTypeInfoCount (out uint pcTInfo)
1675 throw new NotImplementedException ();
1678 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1680 throw new NotImplementedException ();