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 [] result = new string [fields.Length];
515 for (int i = 0; i < fields.Length; ++i)
516 result [i] = fields [i].Name;
521 static NotImplementedException CreateNIE () {
522 return new NotImplementedException ();
525 public virtual Array GetEnumValues () {
527 throw new ArgumentException ("Type is not an enumeration", "enumType");
532 bool IsValidEnumType (Type type) {
533 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
536 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
537 public virtual string GetEnumName (object value) {
539 throw new ArgumentException ("Value is null", "value");
540 if (!IsValidEnumType (value.GetType ()))
541 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
543 throw new ArgumentException ("Type is not an enumeration", "enumType");
546 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
548 for (int i = 0; i < fields.Length; ++i) {
549 var fv = fields [i].GetValue (null);
552 //XXX we can't use 'this' as argument as it might be an UserType
553 obj = Enum.ToObject (fv.GetType (), value);
554 } catch (OverflowException) {
556 } catch (InvalidCastException) {
557 throw new ArgumentException ("Value is not valid", "value");
561 return fields [i].Name;
567 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
568 public virtual bool IsEnumDefined (object value) {
570 throw new ArgumentException ("Value is null", "value");
572 throw new ArgumentException ("Type is not an enumeration", "enumType");
574 Type vt = value.GetType ();
575 if (!IsValidEnumType (vt) && vt != typeof (string))
576 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
578 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
580 if (value is string) {
581 for (int i = 0; i < fields.Length; ++i) {
582 if (fields [i].Name.Equals (value))
586 if (vt != this && vt != GetEnumUnderlyingType ())
587 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
590 for (int i = 0; i < fields.Length; ++i) {
591 var fv = fields [i].GetValue (null);
594 //XXX we can't use 'this' as argument as it might be an UserType
595 obj = Enum.ToObject (fv.GetType (), value);
596 } catch (OverflowException) {
598 } catch (InvalidCastException) {
599 throw new ArgumentException ("Value is not valid", "value");
609 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
611 return GetType (typeName, assemblyResolver, typeResolver, false, false);
614 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
616 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
619 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
621 TypeSpec spec = TypeSpec.Parse (typeName);
622 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
625 public virtual bool IsSecurityTransparent
627 get { throw CreateNIE (); }
630 public virtual bool IsSecurityCritical
632 get { throw CreateNIE (); }
635 public virtual bool IsSecuritySafeCritical
637 get { throw CreateNIE (); }
641 [MethodImplAttribute(MethodImplOptions.InternalCall)]
642 internal extern bool EqualsInternal (Type type);
644 [MethodImplAttribute(MethodImplOptions.InternalCall)]
645 private static extern Type internal_from_handle (IntPtr handle);
647 [MethodImplAttribute(MethodImplOptions.InternalCall)]
648 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
650 public static Type GetType(string typeName)
652 if (typeName == null)
653 throw new ArgumentNullException ("TypeName");
655 return internal_from_name (typeName, false, false);
658 public static Type GetType(string typeName, bool throwOnError)
660 if (typeName == null)
661 throw new ArgumentNullException ("TypeName");
663 Type type = internal_from_name (typeName, throwOnError, false);
664 if (throwOnError && type == null)
665 throw new TypeLoadException ("Error loading '" + typeName + "'");
670 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
672 if (typeName == null)
673 throw new ArgumentNullException ("TypeName");
675 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
676 if (throwOnError && t == null)
677 throw new TypeLoadException ("Error loading '" + typeName + "'");
682 public static Type[] GetTypeArray (object[] args) {
684 throw new ArgumentNullException ("args");
687 ret = new Type [args.Length];
688 for (int i = 0; i < args.Length; ++i)
689 ret [i] = args[i].GetType ();
693 [MethodImplAttribute(MethodImplOptions.InternalCall)]
694 internal extern static TypeCode GetTypeCodeInternal (Type type);
699 TypeCode GetTypeCodeImpl () {
701 if (type is MonoType)
702 return GetTypeCodeInternal (type);
703 if (type is TypeBuilder)
704 return ((TypeBuilder)type).GetTypeCodeInternal ();
706 type = type.UnderlyingSystemType;
708 if (!type.IsSystemType)
709 return TypeCode.Object;
711 return GetTypeCodeInternal (type);
714 public static TypeCode GetTypeCode (Type type) {
716 /* MS.NET returns this */
717 return TypeCode.Empty;
718 return type.GetTypeCodeImpl ();
721 [MonoTODO("This operation is currently not supported by Mono")]
722 public static Type GetTypeFromCLSID (Guid clsid)
724 throw new NotImplementedException ();
727 [MonoTODO("This operation is currently not supported by Mono")]
728 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
730 throw new NotImplementedException ();
733 [MonoTODO("This operation is currently not supported by Mono")]
734 public static Type GetTypeFromCLSID (Guid clsid, string server)
736 throw new NotImplementedException ();
739 [MonoTODO("This operation is currently not supported by Mono")]
740 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
742 throw new NotImplementedException ();
745 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
747 if (handle.Value == IntPtr.Zero)
748 // This is not consistent with the other GetXXXFromHandle methods, but
749 // MS.NET seems to do this
752 return internal_from_handle (handle.Value);
755 [MonoTODO("Mono does not support COM")]
756 public static Type GetTypeFromProgID (string progID)
758 throw new NotImplementedException ();
761 [MonoTODO("Mono does not support COM")]
762 public static Type GetTypeFromProgID (string progID, bool throwOnError)
764 throw new NotImplementedException ();
767 [MonoTODO("Mono does not support COM")]
768 public static Type GetTypeFromProgID (string progID, string server)
770 throw new NotImplementedException ();
773 [MonoTODO("Mono does not support COM")]
774 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
776 throw new NotImplementedException ();
779 public static RuntimeTypeHandle GetTypeHandle (object o)
782 throw new ArgumentNullException ();
784 return o.GetType().TypeHandle;
787 [MethodImplAttribute(MethodImplOptions.InternalCall)]
788 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
790 [MethodImplAttribute(MethodImplOptions.InternalCall)]
791 internal static extern bool type_is_assignable_from (Type a, Type b);
793 public new Type GetType ()
795 return base.GetType ();
799 public virtual bool IsSubclassOf (Type c)
801 if (c == null || c == this)
804 // Fast check for system types
806 return c.IsSystemType && type_is_subtype_of (this, c, false);
808 // User defined types depend on this behavior
809 for (Type type = BaseType; type != null; type = type.BaseType)
816 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
819 throw new ArgumentNullException ("filter");
821 var ifaces = new List<Type> ();
822 foreach (Type iface in GetInterfaces ()) {
823 if (filter (iface, filterCriteria))
827 return ifaces.ToArray ();
830 public Type GetInterface (string name) {
831 return GetInterface (name, false);
834 public abstract Type GetInterface (string name, bool ignoreCase);
836 [MethodImplAttribute(MethodImplOptions.InternalCall)]
837 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
840 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
842 throw new NotSupportedException ("Derived classes must provide an implementation.");
843 if (interfaceType == null)
844 throw new ArgumentNullException ("interfaceType");
845 if (!interfaceType.IsSystemType)
846 throw new ArgumentException ("interfaceType", "Type is an user type");
847 InterfaceMapping res;
848 if (!interfaceType.IsInterface)
849 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
851 throw new ArgumentException ("'this' type cannot be an interface itself");
852 res.TargetType = this;
853 res.InterfaceType = interfaceType;
854 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
855 if (res.TargetMethods == null)
856 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
861 public abstract Type[] GetInterfaces ();
863 public virtual bool IsAssignableFrom (Type c)
871 if (c is TypeBuilder)
872 return ((TypeBuilder)c).IsAssignableTo (this);
874 /* Handle user defined type classes */
876 Type systemType = UnderlyingSystemType;
877 if (!systemType.IsSystemType)
880 Type other = c.UnderlyingSystemType;
881 if (!other.IsSystemType)
884 return systemType.IsAssignableFrom (other);
887 if (!c.IsSystemType) {
888 Type underlyingType = c.UnderlyingSystemType;
889 if (!underlyingType.IsSystemType)
891 return IsAssignableFrom (underlyingType);
894 return type_is_assignable_from (this, c);
897 [MethodImplAttribute(MethodImplOptions.InternalCall)]
898 extern static bool IsInstanceOfType (Type type, object o);
900 public virtual bool IsInstanceOfType (object o)
902 Type type = UnderlyingSystemType;
903 if (!type.IsSystemType)
905 return IsInstanceOfType (type, o);
908 public virtual int GetArrayRank ()
910 throw new NotSupportedException (); // according to MSDN
913 public abstract Type GetElementType ();
915 public EventInfo GetEvent (string name)
917 return GetEvent (name, DefaultBindingFlags);
920 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
922 public virtual EventInfo[] GetEvents ()
924 return GetEvents (DefaultBindingFlags);
927 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
929 public FieldInfo GetField( string name)
931 return GetField (name, DefaultBindingFlags);
934 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
936 public FieldInfo[] GetFields ()
938 return GetFields (DefaultBindingFlags);
941 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
943 public override int GetHashCode()
945 Type t = UnderlyingSystemType;
946 if (t != null && t != this)
947 return t.GetHashCode ();
948 return (int)_impl.Value;
951 public MemberInfo[] GetMember (string name)
953 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
956 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
958 return GetMember (name, MemberTypes.All, bindingAttr);
961 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
964 throw new ArgumentNullException ("name");
965 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
966 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
968 return FindMembers (type, bindingAttr, FilterName, name);
971 public MemberInfo[] GetMembers ()
973 return GetMembers (DefaultBindingFlags);
976 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
978 public MethodInfo GetMethod (string name)
981 throw new ArgumentNullException ("name");
982 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
985 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
988 throw new ArgumentNullException ("name");
990 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
993 public MethodInfo GetMethod (string name, Type[] types)
995 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
998 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1000 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1003 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1004 Type[] types, ParameterModifier[] modifiers)
1006 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1009 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1010 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1013 throw new ArgumentNullException ("name");
1015 throw new ArgumentNullException ("types");
1017 for (int i = 0; i < types.Length; i++)
1018 if (types[i] == null)
1019 throw new ArgumentNullException ("types");
1021 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1024 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1025 CallingConventions callConvention, Type[] types,
1026 ParameterModifier[] modifiers);
1028 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1029 CallingConventions callConvention, Type[] types,
1030 ParameterModifier[] modifiers)
1032 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1035 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1037 throw new System.InvalidOperationException ("can only be called in generic type");
1040 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1042 throw new System.InvalidOperationException ("can only be called in generic type");
1045 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1047 throw new System.InvalidOperationException ("can only be called in generic type");
1051 public MethodInfo[] GetMethods ()
1053 return GetMethods (DefaultBindingFlags);
1056 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1058 public Type GetNestedType (string name)
1060 return GetNestedType (name, DefaultBindingFlags);
1063 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1065 public Type[] GetNestedTypes ()
1067 return GetNestedTypes (DefaultBindingFlags);
1070 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1073 public PropertyInfo[] GetProperties ()
1075 return GetProperties (DefaultBindingFlags);
1078 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1081 public PropertyInfo GetProperty (string name)
1084 throw new ArgumentNullException ("name");
1086 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1089 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1092 throw new ArgumentNullException ("name");
1093 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1096 public PropertyInfo GetProperty (string name, Type returnType)
1099 throw new ArgumentNullException ("name");
1100 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1103 public PropertyInfo GetProperty (string name, Type[] types)
1105 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1108 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1110 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1113 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1115 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1118 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1119 Type[] types, ParameterModifier[] modifiers)
1122 throw new ArgumentNullException ("name");
1124 throw new ArgumentNullException ("types");
1126 foreach (Type t in types) {
1128 throw new ArgumentNullException ("types");
1131 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1134 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1135 Type returnType, Type[] types, ParameterModifier[] modifiers);
1137 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1138 Type returnType, Type[] types, ParameterModifier[] modifiers)
1140 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1143 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1145 CallingConventions callConvention,
1147 ParameterModifier[] modifiers);
1149 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1150 protected abstract bool HasElementTypeImpl ();
1151 protected abstract bool IsArrayImpl ();
1152 protected abstract bool IsByRefImpl ();
1153 protected abstract bool IsCOMObjectImpl ();
1154 protected abstract bool IsPointerImpl ();
1155 protected abstract bool IsPrimitiveImpl ();
1157 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1158 internal static extern bool IsArrayImpl (Type type);
1160 protected virtual bool IsValueTypeImpl ()
1162 if (this == typeof (ValueType) || this == typeof (Enum))
1165 return IsSubclassOf (typeof (ValueType));
1168 protected virtual bool IsContextfulImpl ()
1170 return typeof (ContextBoundObject).IsAssignableFrom (this);
1173 protected virtual bool IsMarshalByRefImpl ()
1175 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1179 public ConstructorInfo GetConstructor (Type[] types)
1181 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1185 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1186 Type[] types, ParameterModifier[] modifiers)
1188 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1192 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1193 CallingConventions callConvention,
1194 Type[] types, ParameterModifier[] modifiers)
1197 throw new ArgumentNullException ("types");
1199 foreach (Type t in types) {
1201 throw new ArgumentNullException ("types");
1204 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1208 public ConstructorInfo[] GetConstructors ()
1210 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1214 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1216 public virtual MemberInfo[] GetDefaultMembers ()
1218 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1219 if (att.Length == 0)
1220 return new MemberInfo [0];
1222 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1223 return (member != null) ? member : new MemberInfo [0];
1226 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1227 MemberFilter filter, object filterCriteria)
1229 MemberInfo[] result;
1230 ArrayList l = new ArrayList ();
1232 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1233 // this.FullName, this.GetType().FullName, this.obj_address());
1234 if ((memberType & MemberTypes.Method) != 0) {
1235 MethodInfo[] c = GetMethods (bindingAttr);
1236 if (filter != null) {
1237 foreach (MemberInfo m in c) {
1238 if (filter (m, filterCriteria))
1245 if ((memberType & MemberTypes.Constructor) != 0) {
1246 ConstructorInfo[] c = GetConstructors (bindingAttr);
1247 if (filter != null) {
1248 foreach (MemberInfo m in c) {
1249 if (filter (m, filterCriteria))
1256 if ((memberType & MemberTypes.Property) != 0) {
1257 PropertyInfo[] c = GetProperties (bindingAttr);
1260 if (filter != null) {
1261 foreach (MemberInfo m in c) {
1262 if (filter (m, filterCriteria))
1270 if ((memberType & MemberTypes.Event) != 0) {
1271 EventInfo[] c = GetEvents (bindingAttr);
1272 if (filter != null) {
1273 foreach (MemberInfo m in c) {
1274 if (filter (m, filterCriteria))
1281 if ((memberType & MemberTypes.Field) != 0) {
1282 FieldInfo[] c = GetFields (bindingAttr);
1283 if (filter != null) {
1284 foreach (MemberInfo m in c) {
1285 if (filter (m, filterCriteria))
1292 if ((memberType & MemberTypes.NestedType) != 0) {
1293 Type[] c = GetNestedTypes (bindingAttr);
1294 if (filter != null) {
1295 foreach (MemberInfo m in c) {
1296 if (filter (m, filterCriteria)) {
1305 switch (memberType) {
1306 case MemberTypes.Constructor :
1307 result = new ConstructorInfo [l.Count];
1309 case MemberTypes.Event :
1310 result = new EventInfo [l.Count];
1312 case MemberTypes.Field :
1313 result = new FieldInfo [l.Count];
1315 case MemberTypes.Method :
1316 result = new MethodInfo [l.Count];
1318 case MemberTypes.NestedType :
1319 case MemberTypes.TypeInfo :
1320 result = new Type [l.Count];
1322 case MemberTypes.Property :
1323 result = new PropertyInfo [l.Count];
1326 result = new MemberInfo [l.Count];
1334 [DebuggerStepThrough]
1335 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1337 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1341 [DebuggerStepThrough]
1342 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1343 object target, object[] args, CultureInfo culture)
1345 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1348 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1349 Binder binder, object target, object[] args,
1350 ParameterModifier[] modifiers,
1351 CultureInfo culture, string[] namedParameters);
1353 public override string ToString()
1358 internal virtual Type InternalResolve ()
1360 return UnderlyingSystemType;
1363 internal bool IsSystemType {
1365 return _impl.Value != IntPtr.Zero;
1370 public virtual Type[] GenericTypeArguments {
1372 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1377 public virtual Type[] GetGenericArguments ()
1379 throw new NotSupportedException ();
1382 public virtual bool ContainsGenericParameters {
1383 get { return false; }
1386 public virtual extern bool IsGenericTypeDefinition {
1387 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1391 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1392 internal extern Type GetGenericTypeDefinition_impl ();
1394 public virtual Type GetGenericTypeDefinition ()
1396 throw new NotSupportedException ("Derived classes must provide an implementation.");
1399 public virtual extern bool IsGenericType {
1400 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1404 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1405 static extern Type MakeGenericType (Type gt, Type [] types);
1407 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1409 if (type.Assembly is AssemblyBuilder)
1410 return (AssemblyBuilder)type.Assembly;
1412 if (type.HasElementType)
1413 return PeelAssemblyBuilder (type.GetElementType ());
1415 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1418 foreach (Type arg in type.GetGenericArguments ()) {
1419 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1426 public virtual Type MakeGenericType (params Type[] typeArguments)
1429 throw new NotSupportedException ();
1430 if (!IsGenericTypeDefinition)
1431 throw new InvalidOperationException ("not a generic type definition");
1432 if (typeArguments == null)
1433 throw new ArgumentNullException ("typeArguments");
1434 if (GetGenericArguments().Length != typeArguments.Length)
1435 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");
1437 bool hasUserType = false;
1439 Type[] systemTypes = new Type[typeArguments.Length];
1440 for (int i = 0; i < typeArguments.Length; ++i) {
1441 Type t = typeArguments [i];
1443 throw new ArgumentNullException ("typeArguments");
1445 if (!(t is MonoType))
1447 systemTypes [i] = t;
1451 return new MonoGenericClass (this, typeArguments);
1454 Type res = MakeGenericType (this, systemTypes);
1456 throw new TypeLoadException ();
1460 public virtual bool IsGenericParameter {
1466 public bool IsNested {
1468 return DeclaringType != null;
1472 public bool IsVisible {
1475 return DeclaringType.IsVisible;
1481 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1482 extern int GetGenericParameterPosition ();
1484 public virtual int GenericParameterPosition {
1486 int res = GetGenericParameterPosition ();
1488 throw new InvalidOperationException ();
1493 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1494 extern GenericParameterAttributes GetGenericParameterAttributes ();
1496 public virtual GenericParameterAttributes GenericParameterAttributes {
1499 throw new NotSupportedException ("Derived classes must provide an implementation.");
1501 if (!IsGenericParameter)
1502 throw new InvalidOperationException ();
1504 return GetGenericParameterAttributes ();
1508 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1509 extern Type[] GetGenericParameterConstraints_impl ();
1511 public virtual Type[] GetGenericParameterConstraints ()
1514 throw new InvalidOperationException ();
1516 if (!IsGenericParameter)
1517 throw new InvalidOperationException ();
1519 return GetGenericParameterConstraints_impl ();
1522 public virtual MethodBase DeclaringMethod {
1528 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1529 extern Type make_array_type (int rank);
1531 public virtual Type MakeArrayType ()
1534 throw new NotSupportedException ("Derived classes must provide an implementation.");
1535 return make_array_type (0);
1538 public virtual Type MakeArrayType (int rank)
1541 throw new NotSupportedException ("Derived classes must provide an implementation.");
1542 if (rank < 1 || rank > 255)
1543 throw new IndexOutOfRangeException ();
1544 return make_array_type (rank);
1547 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1548 extern Type make_byref_type ();
1550 public virtual Type MakeByRefType ()
1553 throw new NotSupportedException ("Derived classes must provide an implementation.");
1555 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1556 return make_byref_type ();
1559 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1560 static extern Type MakePointerType (Type type);
1562 public virtual Type MakePointerType ()
1565 throw new NotSupportedException ("Derived classes must provide an implementation.");
1566 return MakePointerType (this);
1569 public static Type ReflectionOnlyGetType (string typeName,
1570 bool throwIfNotFound,
1573 if (typeName == null)
1574 throw new ArgumentNullException ("typeName");
1575 int idx = typeName.IndexOf (',');
1576 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1577 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1578 string an = typeName.Substring (idx + 1);
1581 a = Assembly.ReflectionOnlyLoad (an);
1583 if (throwIfNotFound)
1587 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1590 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1591 extern void GetPacking (out int packing, out int size);
1593 public virtual StructLayoutAttribute StructLayoutAttribute {
1596 throw new NotSupportedException ();
1598 return GetStructLayoutAttribute ();
1603 internal StructLayoutAttribute GetStructLayoutAttribute ()
1607 if (IsLayoutSequential)
1608 kind = LayoutKind.Sequential;
1609 else if (IsExplicitLayout)
1610 kind = LayoutKind.Explicit;
1612 kind = LayoutKind.Auto;
1614 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1617 attr.CharSet = CharSet.Unicode;
1618 else if (IsAnsiClass)
1619 attr.CharSet = CharSet.Ansi;
1621 attr.CharSet = CharSet.Auto;
1623 if (kind != LayoutKind.Auto) {
1625 GetPacking (out packing, out attr.Size);
1626 // 0 means no data provided, we end up with default value
1628 attr.Pack = packing;
1634 internal object[] GetPseudoCustomAttributes ()
1638 /* IsSerializable returns true for delegates/enums as well */
1639 if ((Attributes & TypeAttributes.Serializable) != 0)
1641 if ((Attributes & TypeAttributes.Import) != 0)
1646 object[] attrs = new object [count];
1649 if ((Attributes & TypeAttributes.Serializable) != 0)
1650 attrs [count ++] = new SerializableAttribute ();
1651 if ((Attributes & TypeAttributes.Import) != 0)
1652 attrs [count ++] = new ComImportAttribute ();
1659 public virtual bool IsEquivalentTo (Type other)
1661 return this == other;
1666 * Return whenever this object is an instance of a user defined subclass
1667 * of System.Type or an instance of TypeDelegator.
1669 internal bool IsUserType {
1672 * subclasses cannot modify _impl so if it is zero, it means the
1673 * type is not created by the runtime.
1675 return _impl.Value == IntPtr.Zero &&
1676 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1680 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1682 throw new NotImplementedException ();
1685 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1687 throw new NotImplementedException ();
1690 void _Type.GetTypeInfoCount (out uint pcTInfo)
1692 throw new NotImplementedException ();
1695 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1697 throw new NotImplementedException ();