5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Diagnostics;
34 using System.Reflection;
35 using System.Reflection.Emit;
36 using System.Collections;
37 using System.Runtime.InteropServices;
38 using System.Runtime.CompilerServices;
39 using System.Globalization;
44 [ClassInterface (ClassInterfaceType.None)]
46 [ComDefaultInterface (typeof (_Type))]
47 public abstract class Type : MemberInfo, IReflect, _Type {
49 internal RuntimeTypeHandle _impl;
51 public static readonly char Delimiter = '.';
52 public static readonly Type[] EmptyTypes = {};
53 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
54 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
55 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
56 public static readonly object Missing = System.Reflection.Missing.Value;
58 internal const BindingFlags DefaultBindingFlags =
59 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
61 /* implementation of the delegates for MemberFilter */
62 static bool FilterName_impl (MemberInfo m, object filterCriteria)
64 string name = (string) filterCriteria;
65 if (name == null || name.Length == 0 )
66 return false; // because m.Name cannot be null or empty
68 if (name [name.Length-1] == '*')
69 return string.CompareOrdinal (name, 0, m.Name, 0, name.Length-1) == 0;
71 return name.Equals (m.Name);
74 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
76 string name = (string) filterCriteria;
77 if (name == null || name.Length == 0 )
78 return false; // because m.Name cannot be null or empty
80 if (name [name.Length-1] == '*')
81 return string.Compare (name, 0, m.Name, 0, name.Length-1, StringComparison.OrdinalIgnoreCase) == 0;
83 return string.Equals (name, m.Name, StringComparison.OrdinalIgnoreCase);
86 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
88 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
90 return ((int)((MethodInfo)m).Attributes & flags) != 0;
92 return ((int)((FieldInfo)m).Attributes & flags) != 0;
93 if (m is PropertyInfo)
94 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
96 return ((int)((EventInfo)m).Attributes & flags) != 0;
105 /// The assembly where the type is defined.
107 public abstract Assembly Assembly {
112 /// Gets the fully qualified name for the type including the
113 /// assembly name where the type is defined.
115 public abstract string AssemblyQualifiedName {
120 /// Returns the Attributes associated with the type.
122 public TypeAttributes Attributes {
124 return GetAttributeFlagsImpl ();
129 /// Returns the basetype for this type
131 public abstract Type BaseType {
136 /// Returns the class that declares the member.
138 public override Type DeclaringType {
147 public static Binder DefaultBinder {
149 return Binder.DefaultBinder;
154 /// The full name of the type including its namespace
156 public abstract string FullName {
160 public abstract Guid GUID {
164 public bool HasElementType {
166 return HasElementTypeImpl ();
170 public bool IsAbstract {
172 return (Attributes & TypeAttributes.Abstract) != 0;
176 public bool IsAnsiClass {
178 return (Attributes & TypeAttributes.StringFormatMask)
179 == TypeAttributes.AnsiClass;
183 public bool IsArray {
185 return IsArrayImpl ();
189 public bool IsAutoClass {
191 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
195 public bool IsAutoLayout {
197 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
201 public bool IsByRef {
203 return IsByRefImpl ();
207 public bool IsClass {
216 public bool IsCOMObject {
218 return IsCOMObjectImpl ();
222 public bool IsContextful {
224 return IsContextfulImpl ();
234 return IsSubclassOf (typeof (Enum));
238 public bool IsExplicitLayout {
240 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
244 public bool IsImport {
246 return (Attributes & TypeAttributes.Import) != 0;
250 public bool IsInterface {
252 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
256 public bool IsLayoutSequential {
258 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
262 public bool IsMarshalByRef {
264 return IsMarshalByRefImpl ();
268 public bool IsNestedAssembly {
270 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
274 public bool IsNestedFamANDAssem {
276 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
280 public bool IsNestedFamily {
282 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
286 public bool IsNestedFamORAssem {
288 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
292 public bool IsNestedPrivate {
294 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
298 public bool IsNestedPublic {
300 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
304 public bool IsNotPublic {
306 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
310 public bool IsPointer {
312 return IsPointerImpl ();
316 public bool IsPrimitive {
318 return IsPrimitiveImpl ();
322 public bool IsPublic {
324 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
328 public bool IsSealed {
330 return (Attributes & TypeAttributes.Sealed) != 0;
338 bool IsSerializable {
340 if ((Attributes & TypeAttributes.Serializable) != 0)
343 // Enums and delegates are always serializable
345 Type type = UnderlyingSystemType;
349 // Fast check for system types
350 if (type.IsSystemType)
351 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
353 // User defined types depend on this behavior
355 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
358 type = type.BaseType;
359 } while (type != null);
365 public bool IsSpecialName {
367 return (Attributes & TypeAttributes.SpecialName) != 0;
371 public bool IsUnicodeClass {
373 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
377 public bool IsValueType {
379 return IsValueTypeImpl ();
383 public override MemberTypes MemberType {
384 get {return MemberTypes.TypeInfo;}
388 public abstract Module Module {get;}
390 public abstract string Namespace {get;}
392 public override Type ReflectedType {
398 public virtual RuntimeTypeHandle TypeHandle {
399 get { throw new ArgumentException ("Derived class must provide implementation."); }
403 public ConstructorInfo TypeInitializer {
405 return GetConstructorImpl (
406 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
408 CallingConventions.Any,
415 * This has NOTHING to do with getting the base type of an enum. Use
416 * Enum.GetUnderlyingType () for that.
418 public abstract Type UnderlyingSystemType {get;}
420 public override bool Equals (object o)
423 return Equals (o as Type);
428 Type me = UnderlyingSystemType;
431 return me.EqualsInternal (o as Type);
436 public virtual bool Equals (Type o)
439 public bool Equals (Type o)
447 Type me = UnderlyingSystemType;
450 return me.EqualsInternal (o.UnderlyingSystemType);
454 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
455 public static bool operator == (Type left, Type right)
457 return Object.ReferenceEquals (left, right);
460 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
461 public static bool operator != (Type left, Type right)
463 return !Object.ReferenceEquals (left, right);
466 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
467 public virtual Type GetEnumUnderlyingType () {
469 throw new ArgumentException ("Type is not an enumeration", "enumType");
471 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
473 if (fields == null || fields.Length != 1)
474 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
476 return fields [0].FieldType;
479 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
480 public virtual string[] GetEnumNames () {
482 throw new ArgumentException ("Type is not an enumeration", "enumType");
484 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
486 string [] result = new string [fields.Length];
487 for (int i = 0; i < fields.Length; ++i)
488 result [i] = fields [i].Name;
493 NotImplementedException CreateNIE () {
494 return new NotImplementedException ();
497 public virtual Array GetEnumValues () {
499 throw new ArgumentException ("Type is not an enumeration", "enumType");
504 bool IsValidEnumType (Type type) {
505 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
508 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
509 public virtual string GetEnumName (object value) {
511 throw new ArgumentException ("Value is null", "value");
512 if (!IsValidEnumType (value.GetType ()))
513 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
515 throw new ArgumentException ("Type is not an enumeration", "enumType");
518 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
520 for (int i = 0; i < fields.Length; ++i) {
521 var fv = fields [i].GetValue (null);
524 //XXX we can't use 'this' as argument as it might be an UserType
525 obj = Enum.ToObject (fv.GetType (), value);
526 } catch (OverflowException) {
528 } catch (InvalidCastException) {
529 throw new ArgumentException ("Value is not valid", "value");
533 return fields [i].Name;
539 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
540 public virtual bool IsEnumDefined (object value) {
542 throw new ArgumentException ("Value is null", "value");
544 throw new ArgumentException ("Type is not an enumeration", "enumType");
546 Type vt = value.GetType ();
547 if (!IsValidEnumType (vt) && vt != typeof (string))
548 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
550 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
552 if (value is string) {
553 for (int i = 0; i < fields.Length; ++i) {
554 if (fields [i].Name.Equals (value))
558 if (vt != this && vt != GetEnumUnderlyingType ())
559 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
562 for (int i = 0; i < fields.Length; ++i) {
563 var fv = fields [i].GetValue (null);
566 //XXX we can't use 'this' as argument as it might be an UserType
567 obj = Enum.ToObject (fv.GetType (), value);
568 } catch (OverflowException) {
570 } catch (InvalidCastException) {
571 throw new ArgumentException ("Value is not valid", "value");
581 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
583 return GetType (typeName, assemblyResolver, typeResolver, false, false);
586 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
588 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
591 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
593 TypeSpec spec = TypeSpec.Parse (typeName);
594 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
599 [MethodImplAttribute(MethodImplOptions.InternalCall)]
600 internal extern bool EqualsInternal (Type type);
602 [MethodImplAttribute(MethodImplOptions.InternalCall)]
603 private static extern Type internal_from_handle (IntPtr handle);
605 [MethodImplAttribute(MethodImplOptions.InternalCall)]
606 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
608 public static Type GetType(string typeName)
610 if (typeName == null)
611 throw new ArgumentNullException ("TypeName");
613 return internal_from_name (typeName, false, false);
616 public static Type GetType(string typeName, bool throwOnError)
618 if (typeName == null)
619 throw new ArgumentNullException ("TypeName");
621 Type type = internal_from_name (typeName, throwOnError, false);
622 if (throwOnError && type == null)
623 throw new TypeLoadException ("Error loading '" + typeName + "'");
628 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
630 if (typeName == null)
631 throw new ArgumentNullException ("TypeName");
633 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
634 if (throwOnError && t == null)
635 throw new TypeLoadException ("Error loading '" + typeName + "'");
640 public static Type[] GetTypeArray (object[] args) {
642 throw new ArgumentNullException ("args");
645 ret = new Type [args.Length];
646 for (int i = 0; i < args.Length; ++i)
647 ret [i] = args[i].GetType ();
651 [MethodImplAttribute(MethodImplOptions.InternalCall)]
652 internal extern static TypeCode GetTypeCodeInternal (Type type);
657 TypeCode GetTypeCodeImpl () {
659 if (type is MonoType)
660 return GetTypeCodeInternal (type);
662 type = type.UnderlyingSystemType;
664 if (!type.IsSystemType)
665 return TypeCode.Object;
667 return GetTypeCodeInternal (type);
670 public static TypeCode GetTypeCode (Type type) {
672 /* MS.NET returns this */
673 return TypeCode.Empty;
674 return type.GetTypeCodeImpl ();
677 [MonoTODO("This operation is currently not supported by Mono")]
678 public static Type GetTypeFromCLSID (Guid clsid)
680 throw new NotImplementedException ();
683 [MonoTODO("This operation is currently not supported by Mono")]
684 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
686 throw new NotImplementedException ();
689 [MonoTODO("This operation is currently not supported by Mono")]
690 public static Type GetTypeFromCLSID (Guid clsid, string server)
692 throw new NotImplementedException ();
695 [MonoTODO("This operation is currently not supported by Mono")]
696 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
698 throw new NotImplementedException ();
701 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
703 if (handle.Value == IntPtr.Zero)
704 // This is not consistent with the other GetXXXFromHandle methods, but
705 // MS.NET seems to do this
708 return internal_from_handle (handle.Value);
711 [MonoTODO("Mono does not support COM")]
712 public static Type GetTypeFromProgID (string progID)
714 throw new NotImplementedException ();
717 [MonoTODO("Mono does not support COM")]
718 public static Type GetTypeFromProgID (string progID, bool throwOnError)
720 throw new NotImplementedException ();
723 [MonoTODO("Mono does not support COM")]
724 public static Type GetTypeFromProgID (string progID, string server)
726 throw new NotImplementedException ();
729 [MonoTODO("Mono does not support COM")]
730 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
732 throw new NotImplementedException ();
735 public static RuntimeTypeHandle GetTypeHandle (object o)
738 throw new ArgumentNullException ();
740 return o.GetType().TypeHandle;
743 [MethodImplAttribute(MethodImplOptions.InternalCall)]
744 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
746 [MethodImplAttribute(MethodImplOptions.InternalCall)]
747 internal static extern bool type_is_assignable_from (Type a, Type b);
749 public new Type GetType ()
751 return base.GetType ();
755 public virtual bool IsSubclassOf (Type c)
757 if (c == null || c == this)
760 // Fast check for system types
762 return c.IsSystemType && type_is_subtype_of (this, c, false);
764 // User defined types depend on this behavior
765 for (Type type = BaseType; type != null; type = type.BaseType)
772 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
775 throw new ArgumentNullException ("filter");
777 ArrayList ifaces = new ArrayList ();
778 foreach (Type iface in GetInterfaces ()) {
779 if (filter (iface, filterCriteria))
783 return (Type []) ifaces.ToArray (typeof (Type));
786 public Type GetInterface (string name) {
787 return GetInterface (name, false);
790 public abstract Type GetInterface (string name, bool ignoreCase);
792 [MethodImplAttribute(MethodImplOptions.InternalCall)]
793 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
796 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
798 throw new NotSupportedException ("Derived classes must provide an implementation.");
799 if (!interfaceType.IsSystemType)
800 throw new ArgumentException ("interfaceType", "Type is an user type");
801 InterfaceMapping res;
802 if (interfaceType == null)
803 throw new ArgumentNullException ("interfaceType");
804 if (!interfaceType.IsInterface)
805 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
807 throw new ArgumentException ("'this' type cannot be an interface itself");
808 res.TargetType = this;
809 res.InterfaceType = interfaceType;
810 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
811 if (res.TargetMethods == null)
812 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
817 public abstract Type[] GetInterfaces ();
819 public virtual bool IsAssignableFrom (Type c)
827 if (c is TypeBuilder)
828 return ((TypeBuilder)c).IsAssignableTo (this);
830 /* Handle user defined type classes */
832 Type systemType = UnderlyingSystemType;
833 if (!systemType.IsSystemType)
836 Type other = c.UnderlyingSystemType;
837 if (!other.IsSystemType)
840 return systemType.IsAssignableFrom (other);
843 if (!c.IsSystemType) {
844 Type underlyingType = c.UnderlyingSystemType;
845 if (!underlyingType.IsSystemType)
847 return IsAssignableFrom (underlyingType);
850 return type_is_assignable_from (this, c);
853 [MethodImplAttribute(MethodImplOptions.InternalCall)]
854 extern static bool IsInstanceOfType (Type type, object o);
856 public virtual bool IsInstanceOfType (object o)
858 Type type = UnderlyingSystemType;
859 if (!type.IsSystemType)
861 return IsInstanceOfType (type, o);
864 public virtual int GetArrayRank ()
866 throw new NotSupportedException (); // according to MSDN
869 public abstract Type GetElementType ();
871 public EventInfo GetEvent (string name)
873 return GetEvent (name, DefaultBindingFlags);
876 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
878 public virtual EventInfo[] GetEvents ()
880 return GetEvents (DefaultBindingFlags);
883 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
885 public FieldInfo GetField( string name)
887 return GetField (name, DefaultBindingFlags);
890 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
892 public FieldInfo[] GetFields ()
894 return GetFields (DefaultBindingFlags);
897 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
899 public override int GetHashCode()
901 Type t = UnderlyingSystemType;
902 if (t != null && t != this)
903 return t.GetHashCode ();
904 return (int)_impl.Value;
907 public MemberInfo[] GetMember (string name)
909 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
912 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
914 return GetMember (name, MemberTypes.All, bindingAttr);
917 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
920 throw new ArgumentNullException ("name");
921 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
922 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
924 return FindMembers (type, bindingAttr, FilterName, name);
927 public MemberInfo[] GetMembers ()
929 return GetMembers (DefaultBindingFlags);
932 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
934 public MethodInfo GetMethod (string name)
937 throw new ArgumentNullException ("name");
938 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
941 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
944 throw new ArgumentNullException ("name");
946 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
949 public MethodInfo GetMethod (string name, Type[] types)
951 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
954 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
956 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
959 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
960 Type[] types, ParameterModifier[] modifiers)
962 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
965 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
966 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
969 throw new ArgumentNullException ("name");
971 throw new ArgumentNullException ("types");
973 for (int i = 0; i < types.Length; i++)
974 if (types[i] == null)
975 throw new ArgumentNullException ("types");
977 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
980 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
981 CallingConventions callConvention, Type[] types,
982 ParameterModifier[] modifiers);
984 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
985 CallingConventions callConvention, Type[] types,
986 ParameterModifier[] modifiers)
988 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
991 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
993 throw new System.InvalidOperationException ("can only be called in generic type");
996 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
998 throw new System.InvalidOperationException ("can only be called in generic type");
1001 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1003 throw new System.InvalidOperationException ("can only be called in generic type");
1007 public MethodInfo[] GetMethods ()
1009 return GetMethods (DefaultBindingFlags);
1012 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1014 public Type GetNestedType (string name)
1016 return GetNestedType (name, DefaultBindingFlags);
1019 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1021 public Type[] GetNestedTypes ()
1023 return GetNestedTypes (DefaultBindingFlags);
1026 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1029 public PropertyInfo[] GetProperties ()
1031 return GetProperties (DefaultBindingFlags);
1034 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1037 public PropertyInfo GetProperty (string name)
1040 throw new ArgumentNullException ("name");
1042 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1045 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1048 throw new ArgumentNullException ("name");
1049 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1052 public PropertyInfo GetProperty (string name, Type returnType)
1055 throw new ArgumentNullException ("name");
1056 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1059 public PropertyInfo GetProperty (string name, Type[] types)
1061 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1064 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1066 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1069 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1071 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1074 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1075 Type[] types, ParameterModifier[] modifiers)
1078 throw new ArgumentNullException ("name");
1080 throw new ArgumentNullException ("types");
1082 foreach (Type t in types) {
1084 throw new ArgumentNullException ("types");
1087 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1090 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1091 Type returnType, Type[] types, ParameterModifier[] modifiers);
1093 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1094 Type returnType, Type[] types, ParameterModifier[] modifiers)
1096 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1099 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1101 CallingConventions callConvention,
1103 ParameterModifier[] modifiers);
1105 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1106 protected abstract bool HasElementTypeImpl ();
1107 protected abstract bool IsArrayImpl ();
1108 protected abstract bool IsByRefImpl ();
1109 protected abstract bool IsCOMObjectImpl ();
1110 protected abstract bool IsPointerImpl ();
1111 protected abstract bool IsPrimitiveImpl ();
1113 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1114 internal static extern bool IsArrayImpl (Type type);
1116 protected virtual bool IsValueTypeImpl ()
1118 if (this == typeof (ValueType) || this == typeof (Enum))
1121 return IsSubclassOf (typeof (ValueType));
1124 protected virtual bool IsContextfulImpl ()
1126 return typeof (ContextBoundObject).IsAssignableFrom (this);
1129 protected virtual bool IsMarshalByRefImpl ()
1131 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1135 public ConstructorInfo GetConstructor (Type[] types)
1137 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1141 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1142 Type[] types, ParameterModifier[] modifiers)
1144 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1148 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1149 CallingConventions callConvention,
1150 Type[] types, ParameterModifier[] modifiers)
1153 throw new ArgumentNullException ("types");
1155 foreach (Type t in types) {
1157 throw new ArgumentNullException ("types");
1160 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1164 public ConstructorInfo[] GetConstructors ()
1166 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1170 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1172 public virtual MemberInfo[] GetDefaultMembers ()
1174 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1175 if (att.Length == 0)
1176 return new MemberInfo [0];
1178 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1179 return (member != null) ? member : new MemberInfo [0];
1182 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1183 MemberFilter filter, object filterCriteria)
1185 MemberInfo[] result;
1186 ArrayList l = new ArrayList ();
1188 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1189 // this.FullName, this.GetType().FullName, this.obj_address());
1190 if ((memberType & MemberTypes.Method) != 0) {
1191 MethodInfo[] c = GetMethods (bindingAttr);
1192 if (filter != null) {
1193 foreach (MemberInfo m in c) {
1194 if (filter (m, filterCriteria))
1201 if ((memberType & MemberTypes.Constructor) != 0) {
1202 ConstructorInfo[] c = GetConstructors (bindingAttr);
1203 if (filter != null) {
1204 foreach (MemberInfo m in c) {
1205 if (filter (m, filterCriteria))
1212 if ((memberType & MemberTypes.Property) != 0) {
1214 int count = l.Count;
1216 if (filter != null) {
1218 while ((l.Count == count) && (ptype != null)) {
1219 c = ptype.GetProperties (bindingAttr);
1220 foreach (MemberInfo m in c) {
1221 if (filter (m, filterCriteria))
1224 ptype = ptype.BaseType;
1227 c = GetProperties (bindingAttr);
1231 if ((memberType & MemberTypes.Event) != 0) {
1232 EventInfo[] c = GetEvents (bindingAttr);
1233 if (filter != null) {
1234 foreach (MemberInfo m in c) {
1235 if (filter (m, filterCriteria))
1242 if ((memberType & MemberTypes.Field) != 0) {
1243 FieldInfo[] c = GetFields (bindingAttr);
1244 if (filter != null) {
1245 foreach (MemberInfo m in c) {
1246 if (filter (m, filterCriteria))
1253 if ((memberType & MemberTypes.NestedType) != 0) {
1254 Type[] c = GetNestedTypes (bindingAttr);
1255 if (filter != null) {
1256 foreach (MemberInfo m in c) {
1257 if (filter (m, filterCriteria)) {
1266 switch (memberType) {
1267 case MemberTypes.Constructor :
1268 result = new ConstructorInfo [l.Count];
1270 case MemberTypes.Event :
1271 result = new EventInfo [l.Count];
1273 case MemberTypes.Field :
1274 result = new FieldInfo [l.Count];
1276 case MemberTypes.Method :
1277 result = new MethodInfo [l.Count];
1279 case MemberTypes.NestedType :
1280 case MemberTypes.TypeInfo :
1281 result = new Type [l.Count];
1283 case MemberTypes.Property :
1284 result = new PropertyInfo [l.Count];
1287 result = new MemberInfo [l.Count];
1295 [DebuggerStepThrough]
1296 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1298 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1302 [DebuggerStepThrough]
1303 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1304 object target, object[] args, CultureInfo culture)
1306 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1309 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1310 Binder binder, object target, object[] args,
1311 ParameterModifier[] modifiers,
1312 CultureInfo culture, string[] namedParameters);
1314 public override string ToString()
1319 internal virtual bool IsCompilerContext {
1321 AssemblyBuilder builder = Assembly as AssemblyBuilder;
1322 return builder != null && builder.IsCompilerContext;
1326 internal bool IsSystemType {
1328 return _impl.Value != IntPtr.Zero;
1332 public virtual Type[] GetGenericArguments ()
1334 throw new NotSupportedException ();
1337 public virtual bool ContainsGenericParameters {
1338 get { return false; }
1341 public virtual extern bool IsGenericTypeDefinition {
1342 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1346 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1347 internal extern Type GetGenericTypeDefinition_impl ();
1349 public virtual Type GetGenericTypeDefinition ()
1351 throw new NotSupportedException ("Derived classes must provide an implementation.");
1354 public virtual extern bool IsGenericType {
1355 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1359 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1360 static extern Type MakeGenericType (Type gt, Type [] types);
1362 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1364 if (type.Assembly is AssemblyBuilder)
1365 return (AssemblyBuilder)type.Assembly;
1367 if (type.HasElementType)
1368 return PeelAssemblyBuilder (type.GetElementType ());
1370 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1373 foreach (Type arg in type.GetGenericArguments ()) {
1374 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1381 public virtual Type MakeGenericType (params Type[] typeArguments)
1384 throw new NotSupportedException ();
1385 if (!IsGenericTypeDefinition)
1386 throw new InvalidOperationException ("not a generic type definition");
1387 if (typeArguments == null)
1388 throw new ArgumentNullException ("typeArguments");
1389 if (GetGenericArguments().Length != typeArguments.Length)
1390 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");
1392 bool hasUserType = false;
1393 AssemblyBuilder compilerContext = null;
1395 Type[] systemTypes = new Type[typeArguments.Length];
1396 for (int i = 0; i < typeArguments.Length; ++i) {
1397 Type t = typeArguments [i];
1399 throw new ArgumentNullException ("typeArguments");
1401 if (!(t is MonoType))
1403 if (t.IsCompilerContext)
1404 compilerContext = PeelAssemblyBuilder (t);
1405 systemTypes [i] = t;
1409 if (compilerContext != null)
1410 return compilerContext.MakeGenericType (this, typeArguments);
1411 return new MonoGenericClass (this, typeArguments);
1414 Type res = MakeGenericType (this, systemTypes);
1416 throw new TypeLoadException ();
1420 public virtual bool IsGenericParameter {
1426 public bool IsNested {
1428 return DeclaringType != null;
1432 public bool IsVisible {
1435 return DeclaringType.IsVisible;
1441 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1442 extern int GetGenericParameterPosition ();
1444 public virtual int GenericParameterPosition {
1446 int res = GetGenericParameterPosition ();
1448 throw new InvalidOperationException ();
1453 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1454 extern GenericParameterAttributes GetGenericParameterAttributes ();
1456 public virtual GenericParameterAttributes GenericParameterAttributes {
1459 throw new NotSupportedException ("Derived classes must provide an implementation.");
1461 if (!IsGenericParameter)
1462 throw new InvalidOperationException ();
1464 return GetGenericParameterAttributes ();
1468 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1469 extern Type[] GetGenericParameterConstraints_impl ();
1471 public virtual Type[] GetGenericParameterConstraints ()
1474 throw new InvalidOperationException ();
1476 if (!IsGenericParameter)
1477 throw new InvalidOperationException ();
1479 return GetGenericParameterConstraints_impl ();
1482 public virtual MethodBase DeclaringMethod {
1488 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1489 extern Type make_array_type (int rank);
1491 public virtual Type MakeArrayType ()
1494 throw new NotSupportedException ("Derived classes must provide an implementation.");
1495 return make_array_type (0);
1498 public virtual Type MakeArrayType (int rank)
1501 throw new NotSupportedException ("Derived classes must provide an implementation.");
1502 if (rank < 1 || rank > 255)
1503 throw new IndexOutOfRangeException ();
1504 return make_array_type (rank);
1507 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1508 extern Type make_byref_type ();
1510 public virtual Type MakeByRefType ()
1513 throw new NotSupportedException ("Derived classes must provide an implementation.");
1515 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1516 return make_byref_type ();
1519 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1520 static extern Type MakePointerType (Type type);
1522 public virtual Type MakePointerType ()
1525 throw new NotSupportedException ("Derived classes must provide an implementation.");
1526 return MakePointerType (this);
1529 public static Type ReflectionOnlyGetType (string typeName,
1530 bool throwIfNotFound,
1533 if (typeName == null)
1534 throw new ArgumentNullException ("typeName");
1535 int idx = typeName.IndexOf (',');
1536 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1537 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1538 string an = typeName.Substring (idx + 1);
1541 a = Assembly.ReflectionOnlyLoad (an);
1543 if (throwIfNotFound)
1547 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1550 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1551 extern void GetPacking (out int packing, out int size);
1553 public virtual StructLayoutAttribute StructLayoutAttribute {
1557 if (IsLayoutSequential)
1558 kind = LayoutKind.Sequential;
1559 else if (IsExplicitLayout)
1560 kind = LayoutKind.Explicit;
1562 kind = LayoutKind.Auto;
1564 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1567 attr.CharSet = CharSet.Unicode;
1568 else if (IsAnsiClass)
1569 attr.CharSet = CharSet.Ansi;
1571 attr.CharSet = CharSet.Auto;
1573 if (kind != LayoutKind.Auto)
1574 GetPacking (out attr.Pack, out attr.Size);
1580 internal object[] GetPseudoCustomAttributes ()
1584 /* IsSerializable returns true for delegates/enums as well */
1585 if ((Attributes & TypeAttributes.Serializable) != 0)
1587 if ((Attributes & TypeAttributes.Import) != 0)
1592 object[] attrs = new object [count];
1595 if ((Attributes & TypeAttributes.Serializable) != 0)
1596 attrs [count ++] = new SerializableAttribute ();
1597 if ((Attributes & TypeAttributes.Import) != 0)
1598 attrs [count ++] = new ComImportAttribute ();
1604 #if NET_4_0 || BOOTSTRAP_NET_4_0
1605 public virtual bool IsEquivalentTo (Type other)
1607 return this == other;
1612 * Return whenever this object is an instance of a user defined subclass
1613 * of System.Type or an instance of TypeDelegator.
1615 internal bool IsUserType {
1618 * subclasses cannot modify _impl so if it is zero, it means the
1619 * type is not created by the runtime.
1621 return _impl.Value == IntPtr.Zero &&
1622 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1626 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1628 throw new NotImplementedException ();
1631 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1633 throw new NotImplementedException ();
1636 void _Type.GetTypeInfoCount (out uint pcTInfo)
1638 throw new NotImplementedException ();
1641 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1643 throw new NotImplementedException ();