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.Runtime.InteropServices;
39 using System.Runtime.CompilerServices;
40 using System.Globalization;
45 [ClassInterface (ClassInterfaceType.None)]
47 [ComDefaultInterface (typeof (_Type))]
48 public abstract class Type : MemberInfo, IReflect, _Type {
50 internal RuntimeTypeHandle _impl;
52 public static readonly char Delimiter = '.';
53 public static readonly Type[] EmptyTypes = {};
54 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
55 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
56 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
57 public static readonly object Missing = System.Reflection.Missing.Value;
59 internal const BindingFlags DefaultBindingFlags =
60 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
62 /* implementation of the delegates for MemberFilter */
63 static bool FilterName_impl (MemberInfo m, object filterCriteria)
65 string name = (string) filterCriteria;
66 if (name == null || name.Length == 0 )
67 return false; // because m.Name cannot be null or empty
69 if (name [name.Length-1] == '*')
70 return string.CompareOrdinal (name, 0, m.Name, 0, name.Length-1) == 0;
72 return name.Equals (m.Name);
75 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
77 string name = (string) filterCriteria;
78 if (name == null || name.Length == 0 )
79 return false; // because m.Name cannot be null or empty
81 if (name [name.Length-1] == '*')
82 return string.Compare (name, 0, m.Name, 0, name.Length-1, StringComparison.OrdinalIgnoreCase) == 0;
84 return string.Equals (name, m.Name, StringComparison.OrdinalIgnoreCase);
87 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
89 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
91 return ((int)((MethodInfo)m).Attributes & flags) != 0;
93 return ((int)((FieldInfo)m).Attributes & flags) != 0;
94 if (m is PropertyInfo)
95 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
97 return ((int)((EventInfo)m).Attributes & flags) != 0;
106 /// The assembly where the type is defined.
108 public abstract Assembly Assembly {
113 /// Gets the fully qualified name for the type including the
114 /// assembly name where the type is defined.
116 public abstract string AssemblyQualifiedName {
121 /// Returns the Attributes associated with the type.
123 public TypeAttributes Attributes {
125 return GetAttributeFlagsImpl ();
130 /// Returns the basetype for this type
132 public abstract Type BaseType {
137 /// Returns the class that declares the member.
139 public override Type DeclaringType {
148 public static Binder DefaultBinder {
150 return Binder.DefaultBinder;
155 /// The full name of the type including its namespace
157 public abstract string FullName {
161 public abstract Guid GUID {
165 public bool HasElementType {
167 return HasElementTypeImpl ();
171 public bool IsAbstract {
173 return (Attributes & TypeAttributes.Abstract) != 0;
177 public bool IsAnsiClass {
179 return (Attributes & TypeAttributes.StringFormatMask)
180 == TypeAttributes.AnsiClass;
184 public bool IsArray {
186 return IsArrayImpl ();
190 public bool IsAutoClass {
192 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
196 public bool IsAutoLayout {
198 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
202 public bool IsByRef {
204 return IsByRefImpl ();
208 public bool IsClass {
217 public bool IsCOMObject {
219 return IsCOMObjectImpl ();
223 public bool IsContextful {
225 return IsContextfulImpl ();
235 return IsSubclassOf (typeof (Enum));
239 public bool IsExplicitLayout {
241 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
245 public bool IsImport {
247 return (Attributes & TypeAttributes.Import) != 0;
251 public bool IsInterface {
253 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
257 public bool IsLayoutSequential {
259 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
263 public bool IsMarshalByRef {
265 return IsMarshalByRefImpl ();
269 public bool IsNestedAssembly {
271 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
275 public bool IsNestedFamANDAssem {
277 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
281 public bool IsNestedFamily {
283 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
287 public bool IsNestedFamORAssem {
289 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
293 public bool IsNestedPrivate {
295 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
299 public bool IsNestedPublic {
301 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
305 public bool IsNotPublic {
307 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
311 public bool IsPointer {
313 return IsPointerImpl ();
317 public bool IsPrimitive {
319 return IsPrimitiveImpl ();
323 public bool IsPublic {
325 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
329 public bool IsSealed {
331 return (Attributes & TypeAttributes.Sealed) != 0;
339 bool IsSerializable {
341 if ((Attributes & TypeAttributes.Serializable) != 0)
344 // Enums and delegates are always serializable
346 Type type = UnderlyingSystemType;
350 // Fast check for system types
351 if (type.IsSystemType)
352 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
354 // User defined types depend on this behavior
356 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
359 type = type.BaseType;
360 } while (type != null);
366 public bool IsSpecialName {
368 return (Attributes & TypeAttributes.SpecialName) != 0;
372 public bool IsUnicodeClass {
374 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
378 public bool IsValueType {
380 return IsValueTypeImpl ();
384 public override MemberTypes MemberType {
386 return MemberTypes.TypeInfo;
390 public abstract override Module Module {
394 public abstract string Namespace {get;}
396 public override Type ReflectedType {
402 public virtual RuntimeTypeHandle TypeHandle {
403 get { throw new ArgumentException ("Derived class must provide implementation."); }
407 public ConstructorInfo TypeInitializer {
409 return GetConstructorImpl (
410 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
412 CallingConventions.Any,
419 * This has NOTHING to do with getting the base type of an enum. Use
420 * Enum.GetUnderlyingType () for that.
422 public abstract Type UnderlyingSystemType {get;}
424 public override bool Equals (object o)
427 return Equals (o as Type);
432 Type me = UnderlyingSystemType;
435 return me.EqualsInternal (o as Type);
440 public virtual bool Equals (Type o)
442 if ((object)o == (object)this)
444 if ((object)o == null)
446 Type me = UnderlyingSystemType;
447 if ((object)me == null)
450 o = o.UnderlyingSystemType;
451 if ((object)o == null)
453 if ((object)o == (object)this)
455 return me.EqualsInternal (o);
458 public bool Equals (Type o)
465 Type me = UnderlyingSystemType;
468 return me.EqualsInternal (o.UnderlyingSystemType);
472 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
473 public static bool operator == (Type left, Type right)
475 return Object.ReferenceEquals (left, right);
478 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
479 public static bool operator != (Type left, Type right)
481 return !Object.ReferenceEquals (left, right);
484 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
485 public virtual Type GetEnumUnderlyingType () {
487 throw new ArgumentException ("Type is not an enumeration", "enumType");
489 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
491 if (fields == null || fields.Length != 1)
492 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
494 return fields [0].FieldType;
497 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
498 public virtual string[] GetEnumNames () {
500 throw new ArgumentException ("Type is not an enumeration", "enumType");
502 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
504 string [] result = new string [fields.Length];
505 for (int i = 0; i < fields.Length; ++i)
506 result [i] = fields [i].Name;
511 static NotImplementedException CreateNIE () {
512 return new NotImplementedException ();
515 public virtual Array GetEnumValues () {
517 throw new ArgumentException ("Type is not an enumeration", "enumType");
522 bool IsValidEnumType (Type type) {
523 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
526 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
527 public virtual string GetEnumName (object value) {
529 throw new ArgumentException ("Value is null", "value");
530 if (!IsValidEnumType (value.GetType ()))
531 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
533 throw new ArgumentException ("Type is not an enumeration", "enumType");
536 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
538 for (int i = 0; i < fields.Length; ++i) {
539 var fv = fields [i].GetValue (null);
542 //XXX we can't use 'this' as argument as it might be an UserType
543 obj = Enum.ToObject (fv.GetType (), value);
544 } catch (OverflowException) {
546 } catch (InvalidCastException) {
547 throw new ArgumentException ("Value is not valid", "value");
551 return fields [i].Name;
557 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
558 public virtual bool IsEnumDefined (object value) {
560 throw new ArgumentException ("Value is null", "value");
562 throw new ArgumentException ("Type is not an enumeration", "enumType");
564 Type vt = value.GetType ();
565 if (!IsValidEnumType (vt) && vt != typeof (string))
566 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
568 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
570 if (value is string) {
571 for (int i = 0; i < fields.Length; ++i) {
572 if (fields [i].Name.Equals (value))
576 if (vt != this && vt != GetEnumUnderlyingType ())
577 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
580 for (int i = 0; i < fields.Length; ++i) {
581 var fv = fields [i].GetValue (null);
584 //XXX we can't use 'this' as argument as it might be an UserType
585 obj = Enum.ToObject (fv.GetType (), value);
586 } catch (OverflowException) {
588 } catch (InvalidCastException) {
589 throw new ArgumentException ("Value is not valid", "value");
599 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
601 return GetType (typeName, assemblyResolver, typeResolver, false, false);
604 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
606 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
609 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
611 TypeSpec spec = TypeSpec.Parse (typeName);
612 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
615 public virtual bool IsSecurityTransparent
617 get { throw CreateNIE (); }
620 public virtual bool IsSecurityCritical
622 get { throw CreateNIE (); }
625 public virtual bool IsSecuritySafeCritical
627 get { throw CreateNIE (); }
631 [MethodImplAttribute(MethodImplOptions.InternalCall)]
632 internal extern bool EqualsInternal (Type type);
634 [MethodImplAttribute(MethodImplOptions.InternalCall)]
635 private static extern Type internal_from_handle (IntPtr handle);
637 [MethodImplAttribute(MethodImplOptions.InternalCall)]
638 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
640 public static Type GetType(string typeName)
642 if (typeName == null)
643 throw new ArgumentNullException ("TypeName");
645 return internal_from_name (typeName, false, false);
648 public static Type GetType(string typeName, bool throwOnError)
650 if (typeName == null)
651 throw new ArgumentNullException ("TypeName");
653 Type type = internal_from_name (typeName, throwOnError, false);
654 if (throwOnError && type == null)
655 throw new TypeLoadException ("Error loading '" + typeName + "'");
660 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
662 if (typeName == null)
663 throw new ArgumentNullException ("TypeName");
665 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
666 if (throwOnError && t == null)
667 throw new TypeLoadException ("Error loading '" + typeName + "'");
672 public static Type[] GetTypeArray (object[] args) {
674 throw new ArgumentNullException ("args");
677 ret = new Type [args.Length];
678 for (int i = 0; i < args.Length; ++i)
679 ret [i] = args[i].GetType ();
683 [MethodImplAttribute(MethodImplOptions.InternalCall)]
684 internal extern static TypeCode GetTypeCodeInternal (Type type);
689 TypeCode GetTypeCodeImpl () {
691 if (type is MonoType)
692 return GetTypeCodeInternal (type);
693 if (type is TypeBuilder)
694 return ((TypeBuilder)type).GetTypeCodeInternal ();
696 type = type.UnderlyingSystemType;
698 if (!type.IsSystemType)
699 return TypeCode.Object;
701 return GetTypeCodeInternal (type);
704 public static TypeCode GetTypeCode (Type type) {
706 /* MS.NET returns this */
707 return TypeCode.Empty;
708 return type.GetTypeCodeImpl ();
711 [MonoTODO("This operation is currently not supported by Mono")]
712 public static Type GetTypeFromCLSID (Guid clsid)
714 throw new NotImplementedException ();
717 [MonoTODO("This operation is currently not supported by Mono")]
718 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
720 throw new NotImplementedException ();
723 [MonoTODO("This operation is currently not supported by Mono")]
724 public static Type GetTypeFromCLSID (Guid clsid, string server)
726 throw new NotImplementedException ();
729 [MonoTODO("This operation is currently not supported by Mono")]
730 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
732 throw new NotImplementedException ();
735 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
737 if (handle.Value == IntPtr.Zero)
738 // This is not consistent with the other GetXXXFromHandle methods, but
739 // MS.NET seems to do this
742 return internal_from_handle (handle.Value);
745 [MonoTODO("Mono does not support COM")]
746 public static Type GetTypeFromProgID (string progID)
748 throw new NotImplementedException ();
751 [MonoTODO("Mono does not support COM")]
752 public static Type GetTypeFromProgID (string progID, bool throwOnError)
754 throw new NotImplementedException ();
757 [MonoTODO("Mono does not support COM")]
758 public static Type GetTypeFromProgID (string progID, string server)
760 throw new NotImplementedException ();
763 [MonoTODO("Mono does not support COM")]
764 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
766 throw new NotImplementedException ();
769 public static RuntimeTypeHandle GetTypeHandle (object o)
772 throw new ArgumentNullException ();
774 return o.GetType().TypeHandle;
777 [MethodImplAttribute(MethodImplOptions.InternalCall)]
778 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
780 [MethodImplAttribute(MethodImplOptions.InternalCall)]
781 internal static extern bool type_is_assignable_from (Type a, Type b);
783 public new Type GetType ()
785 return base.GetType ();
789 public virtual bool IsSubclassOf (Type c)
791 if (c == null || c == this)
794 // Fast check for system types
796 return c.IsSystemType && type_is_subtype_of (this, c, false);
798 // User defined types depend on this behavior
799 for (Type type = BaseType; type != null; type = type.BaseType)
806 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
809 throw new ArgumentNullException ("filter");
811 ArrayList ifaces = new ArrayList ();
812 foreach (Type iface in GetInterfaces ()) {
813 if (filter (iface, filterCriteria))
817 return (Type []) ifaces.ToArray (typeof (Type));
820 public Type GetInterface (string name) {
821 return GetInterface (name, false);
824 public abstract Type GetInterface (string name, bool ignoreCase);
826 [MethodImplAttribute(MethodImplOptions.InternalCall)]
827 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
830 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
832 throw new NotSupportedException ("Derived classes must provide an implementation.");
833 if (!interfaceType.IsSystemType)
834 throw new ArgumentException ("interfaceType", "Type is an user type");
835 InterfaceMapping res;
836 if (interfaceType == null)
837 throw new ArgumentNullException ("interfaceType");
838 if (!interfaceType.IsInterface)
839 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
841 throw new ArgumentException ("'this' type cannot be an interface itself");
842 res.TargetType = this;
843 res.InterfaceType = interfaceType;
844 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
845 if (res.TargetMethods == null)
846 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
851 public abstract Type[] GetInterfaces ();
853 public virtual bool IsAssignableFrom (Type c)
861 if (c is TypeBuilder)
862 return ((TypeBuilder)c).IsAssignableTo (this);
864 /* Handle user defined type classes */
866 Type systemType = UnderlyingSystemType;
867 if (!systemType.IsSystemType)
870 Type other = c.UnderlyingSystemType;
871 if (!other.IsSystemType)
874 return systemType.IsAssignableFrom (other);
877 if (!c.IsSystemType) {
878 Type underlyingType = c.UnderlyingSystemType;
879 if (!underlyingType.IsSystemType)
881 return IsAssignableFrom (underlyingType);
884 return type_is_assignable_from (this, c);
887 [MethodImplAttribute(MethodImplOptions.InternalCall)]
888 extern static bool IsInstanceOfType (Type type, object o);
890 public virtual bool IsInstanceOfType (object o)
892 Type type = UnderlyingSystemType;
893 if (!type.IsSystemType)
895 return IsInstanceOfType (type, o);
898 public virtual int GetArrayRank ()
900 throw new NotSupportedException (); // according to MSDN
903 public abstract Type GetElementType ();
905 public EventInfo GetEvent (string name)
907 return GetEvent (name, DefaultBindingFlags);
910 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
912 public virtual EventInfo[] GetEvents ()
914 return GetEvents (DefaultBindingFlags);
917 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
919 public FieldInfo GetField( string name)
921 return GetField (name, DefaultBindingFlags);
924 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
926 public FieldInfo[] GetFields ()
928 return GetFields (DefaultBindingFlags);
931 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
933 public override int GetHashCode()
935 Type t = UnderlyingSystemType;
936 if (t != null && t != this)
937 return t.GetHashCode ();
938 return (int)_impl.Value;
941 public MemberInfo[] GetMember (string name)
943 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
946 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
948 return GetMember (name, MemberTypes.All, bindingAttr);
951 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
954 throw new ArgumentNullException ("name");
955 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
956 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
958 return FindMembers (type, bindingAttr, FilterName, name);
961 public MemberInfo[] GetMembers ()
963 return GetMembers (DefaultBindingFlags);
966 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
968 public MethodInfo GetMethod (string name)
971 throw new ArgumentNullException ("name");
972 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
975 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
978 throw new ArgumentNullException ("name");
980 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
983 public MethodInfo GetMethod (string name, Type[] types)
985 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
988 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
990 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
993 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
994 Type[] types, ParameterModifier[] modifiers)
996 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
999 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1000 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1003 throw new ArgumentNullException ("name");
1005 throw new ArgumentNullException ("types");
1007 for (int i = 0; i < types.Length; i++)
1008 if (types[i] == null)
1009 throw new ArgumentNullException ("types");
1011 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1014 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1015 CallingConventions callConvention, Type[] types,
1016 ParameterModifier[] modifiers);
1018 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1019 CallingConventions callConvention, Type[] types,
1020 ParameterModifier[] modifiers)
1022 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1025 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1027 throw new System.InvalidOperationException ("can only be called in generic type");
1030 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1032 throw new System.InvalidOperationException ("can only be called in generic type");
1035 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1037 throw new System.InvalidOperationException ("can only be called in generic type");
1041 public MethodInfo[] GetMethods ()
1043 return GetMethods (DefaultBindingFlags);
1046 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1048 public Type GetNestedType (string name)
1050 return GetNestedType (name, DefaultBindingFlags);
1053 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1055 public Type[] GetNestedTypes ()
1057 return GetNestedTypes (DefaultBindingFlags);
1060 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1063 public PropertyInfo[] GetProperties ()
1065 return GetProperties (DefaultBindingFlags);
1068 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1071 public PropertyInfo GetProperty (string name)
1074 throw new ArgumentNullException ("name");
1076 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1079 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1082 throw new ArgumentNullException ("name");
1083 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1086 public PropertyInfo GetProperty (string name, Type returnType)
1089 throw new ArgumentNullException ("name");
1090 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1093 public PropertyInfo GetProperty (string name, Type[] types)
1095 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1098 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1100 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1103 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1105 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1108 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1109 Type[] types, ParameterModifier[] modifiers)
1112 throw new ArgumentNullException ("name");
1114 throw new ArgumentNullException ("types");
1116 foreach (Type t in types) {
1118 throw new ArgumentNullException ("types");
1121 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1124 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1125 Type returnType, Type[] types, ParameterModifier[] modifiers);
1127 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1128 Type returnType, Type[] types, ParameterModifier[] modifiers)
1130 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1133 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1135 CallingConventions callConvention,
1137 ParameterModifier[] modifiers);
1139 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1140 protected abstract bool HasElementTypeImpl ();
1141 protected abstract bool IsArrayImpl ();
1142 protected abstract bool IsByRefImpl ();
1143 protected abstract bool IsCOMObjectImpl ();
1144 protected abstract bool IsPointerImpl ();
1145 protected abstract bool IsPrimitiveImpl ();
1147 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1148 internal static extern bool IsArrayImpl (Type type);
1150 protected virtual bool IsValueTypeImpl ()
1152 if (this == typeof (ValueType) || this == typeof (Enum))
1155 return IsSubclassOf (typeof (ValueType));
1158 protected virtual bool IsContextfulImpl ()
1160 return typeof (ContextBoundObject).IsAssignableFrom (this);
1163 protected virtual bool IsMarshalByRefImpl ()
1165 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1169 public ConstructorInfo GetConstructor (Type[] types)
1171 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1175 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1176 Type[] types, ParameterModifier[] modifiers)
1178 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1182 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1183 CallingConventions callConvention,
1184 Type[] types, ParameterModifier[] modifiers)
1187 throw new ArgumentNullException ("types");
1189 foreach (Type t in types) {
1191 throw new ArgumentNullException ("types");
1194 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1198 public ConstructorInfo[] GetConstructors ()
1200 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1204 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1206 public virtual MemberInfo[] GetDefaultMembers ()
1208 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1209 if (att.Length == 0)
1210 return new MemberInfo [0];
1212 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1213 return (member != null) ? member : new MemberInfo [0];
1216 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1217 MemberFilter filter, object filterCriteria)
1219 MemberInfo[] result;
1220 ArrayList l = new ArrayList ();
1222 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1223 // this.FullName, this.GetType().FullName, this.obj_address());
1224 if ((memberType & MemberTypes.Method) != 0) {
1225 MethodInfo[] c = GetMethods (bindingAttr);
1226 if (filter != null) {
1227 foreach (MemberInfo m in c) {
1228 if (filter (m, filterCriteria))
1235 if ((memberType & MemberTypes.Constructor) != 0) {
1236 ConstructorInfo[] c = GetConstructors (bindingAttr);
1237 if (filter != null) {
1238 foreach (MemberInfo m in c) {
1239 if (filter (m, filterCriteria))
1246 if ((memberType & MemberTypes.Property) != 0) {
1247 PropertyInfo[] c = GetProperties (bindingAttr);
1250 if (filter != null) {
1251 foreach (MemberInfo m in c) {
1252 if (filter (m, filterCriteria))
1260 if ((memberType & MemberTypes.Event) != 0) {
1261 EventInfo[] c = GetEvents (bindingAttr);
1262 if (filter != null) {
1263 foreach (MemberInfo m in c) {
1264 if (filter (m, filterCriteria))
1271 if ((memberType & MemberTypes.Field) != 0) {
1272 FieldInfo[] c = GetFields (bindingAttr);
1273 if (filter != null) {
1274 foreach (MemberInfo m in c) {
1275 if (filter (m, filterCriteria))
1282 if ((memberType & MemberTypes.NestedType) != 0) {
1283 Type[] c = GetNestedTypes (bindingAttr);
1284 if (filter != null) {
1285 foreach (MemberInfo m in c) {
1286 if (filter (m, filterCriteria)) {
1295 switch (memberType) {
1296 case MemberTypes.Constructor :
1297 result = new ConstructorInfo [l.Count];
1299 case MemberTypes.Event :
1300 result = new EventInfo [l.Count];
1302 case MemberTypes.Field :
1303 result = new FieldInfo [l.Count];
1305 case MemberTypes.Method :
1306 result = new MethodInfo [l.Count];
1308 case MemberTypes.NestedType :
1309 case MemberTypes.TypeInfo :
1310 result = new Type [l.Count];
1312 case MemberTypes.Property :
1313 result = new PropertyInfo [l.Count];
1316 result = new MemberInfo [l.Count];
1324 [DebuggerStepThrough]
1325 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1327 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1331 [DebuggerStepThrough]
1332 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1333 object target, object[] args, CultureInfo culture)
1335 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1338 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1339 Binder binder, object target, object[] args,
1340 ParameterModifier[] modifiers,
1341 CultureInfo culture, string[] namedParameters);
1343 public override string ToString()
1348 internal virtual bool IsCompilerContext {
1350 AssemblyBuilder builder = Assembly as AssemblyBuilder;
1351 return builder != null && builder.IsCompilerContext;
1355 internal virtual Type InternalResolve ()
1357 return UnderlyingSystemType;
1360 internal bool IsSystemType {
1362 return _impl.Value != IntPtr.Zero;
1366 public virtual Type[] GetGenericArguments ()
1368 throw new NotSupportedException ();
1371 public virtual bool ContainsGenericParameters {
1372 get { return false; }
1375 public virtual extern bool IsGenericTypeDefinition {
1376 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1380 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1381 internal extern Type GetGenericTypeDefinition_impl ();
1383 public virtual Type GetGenericTypeDefinition ()
1385 throw new NotSupportedException ("Derived classes must provide an implementation.");
1388 public virtual extern bool IsGenericType {
1389 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1393 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1394 static extern Type MakeGenericType (Type gt, Type [] types);
1396 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1398 if (type.Assembly is AssemblyBuilder)
1399 return (AssemblyBuilder)type.Assembly;
1401 if (type.HasElementType)
1402 return PeelAssemblyBuilder (type.GetElementType ());
1404 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1407 foreach (Type arg in type.GetGenericArguments ()) {
1408 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
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;
1427 AssemblyBuilder compilerContext = null;
1429 Type[] systemTypes = new Type[typeArguments.Length];
1430 for (int i = 0; i < typeArguments.Length; ++i) {
1431 Type t = typeArguments [i];
1433 throw new ArgumentNullException ("typeArguments");
1435 if (!(t is MonoType))
1437 if (t.IsCompilerContext)
1438 compilerContext = PeelAssemblyBuilder (t);
1439 systemTypes [i] = t;
1443 if (compilerContext != null)
1444 return compilerContext.MakeGenericType (this, typeArguments);
1445 return new MonoGenericClass (this, typeArguments);
1448 Type res = MakeGenericType (this, systemTypes);
1450 throw new TypeLoadException ();
1454 public virtual bool IsGenericParameter {
1460 public bool IsNested {
1462 return DeclaringType != null;
1466 public bool IsVisible {
1469 return DeclaringType.IsVisible;
1475 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1476 extern int GetGenericParameterPosition ();
1478 public virtual int GenericParameterPosition {
1480 int res = GetGenericParameterPosition ();
1482 throw new InvalidOperationException ();
1487 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1488 extern GenericParameterAttributes GetGenericParameterAttributes ();
1490 public virtual GenericParameterAttributes GenericParameterAttributes {
1493 throw new NotSupportedException ("Derived classes must provide an implementation.");
1495 if (!IsGenericParameter)
1496 throw new InvalidOperationException ();
1498 return GetGenericParameterAttributes ();
1502 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1503 extern Type[] GetGenericParameterConstraints_impl ();
1505 public virtual Type[] GetGenericParameterConstraints ()
1508 throw new InvalidOperationException ();
1510 if (!IsGenericParameter)
1511 throw new InvalidOperationException ();
1513 return GetGenericParameterConstraints_impl ();
1516 public virtual MethodBase DeclaringMethod {
1522 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1523 extern Type make_array_type (int rank);
1525 public virtual Type MakeArrayType ()
1528 throw new NotSupportedException ("Derived classes must provide an implementation.");
1529 return make_array_type (0);
1532 public virtual Type MakeArrayType (int rank)
1535 throw new NotSupportedException ("Derived classes must provide an implementation.");
1536 if (rank < 1 || rank > 255)
1537 throw new IndexOutOfRangeException ();
1538 return make_array_type (rank);
1541 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1542 extern Type make_byref_type ();
1544 public virtual Type MakeByRefType ()
1547 throw new NotSupportedException ("Derived classes must provide an implementation.");
1549 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1550 return make_byref_type ();
1553 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1554 static extern Type MakePointerType (Type type);
1556 public virtual Type MakePointerType ()
1559 throw new NotSupportedException ("Derived classes must provide an implementation.");
1560 return MakePointerType (this);
1563 public static Type ReflectionOnlyGetType (string typeName,
1564 bool throwIfNotFound,
1567 if (typeName == null)
1568 throw new ArgumentNullException ("typeName");
1569 int idx = typeName.IndexOf (',');
1570 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1571 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1572 string an = typeName.Substring (idx + 1);
1575 a = Assembly.ReflectionOnlyLoad (an);
1577 if (throwIfNotFound)
1581 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1584 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1585 extern void GetPacking (out int packing, out int size);
1587 public virtual StructLayoutAttribute StructLayoutAttribute {
1590 throw new NotSupportedException ();
1592 return GetStructLayoutAttribute ();
1597 internal StructLayoutAttribute GetStructLayoutAttribute ()
1601 if (IsLayoutSequential)
1602 kind = LayoutKind.Sequential;
1603 else if (IsExplicitLayout)
1604 kind = LayoutKind.Explicit;
1606 kind = LayoutKind.Auto;
1608 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1611 attr.CharSet = CharSet.Unicode;
1612 else if (IsAnsiClass)
1613 attr.CharSet = CharSet.Ansi;
1615 attr.CharSet = CharSet.Auto;
1617 if (kind != LayoutKind.Auto) {
1619 GetPacking (out packing, out attr.Size);
1620 // 0 means no data provided, we end up with default value
1622 attr.Pack = packing;
1628 internal object[] GetPseudoCustomAttributes ()
1632 /* IsSerializable returns true for delegates/enums as well */
1633 if ((Attributes & TypeAttributes.Serializable) != 0)
1635 if ((Attributes & TypeAttributes.Import) != 0)
1640 object[] attrs = new object [count];
1643 if ((Attributes & TypeAttributes.Serializable) != 0)
1644 attrs [count ++] = new SerializableAttribute ();
1645 if ((Attributes & TypeAttributes.Import) != 0)
1646 attrs [count ++] = new ComImportAttribute ();
1653 public virtual bool IsEquivalentTo (Type other)
1655 return this == other;
1660 * Return whenever this object is an instance of a user defined subclass
1661 * of System.Type or an instance of TypeDelegator.
1663 internal bool IsUserType {
1666 * subclasses cannot modify _impl so if it is zero, it means the
1667 * type is not created by the runtime.
1669 return _impl.Value == IntPtr.Zero &&
1670 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1674 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1676 throw new NotImplementedException ();
1679 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1681 throw new NotImplementedException ();
1684 void _Type.GetTypeInfoCount (out uint pcTInfo)
1686 throw new NotImplementedException ();
1689 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1691 throw new NotImplementedException ();