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)
438 if ((object)o == this)
440 if ((object)o == null)
442 Type me = UnderlyingSystemType;
443 if ((object)me == null)
446 o = o.UnderlyingSystemType;
447 if ((object)o == null)
449 if ((object)o == this)
451 return me.EqualsInternal (o);
454 public bool Equals (Type o)
461 Type me = UnderlyingSystemType;
464 return me.EqualsInternal (o.UnderlyingSystemType);
468 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
469 public static bool operator == (Type left, Type right)
471 return Object.ReferenceEquals (left, right);
474 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
475 public static bool operator != (Type left, Type right)
477 return !Object.ReferenceEquals (left, right);
480 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
481 public virtual Type GetEnumUnderlyingType () {
483 throw new ArgumentException ("Type is not an enumeration", "enumType");
485 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
487 if (fields == null || fields.Length != 1)
488 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
490 return fields [0].FieldType;
493 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
494 public virtual string[] GetEnumNames () {
496 throw new ArgumentException ("Type is not an enumeration", "enumType");
498 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
500 string [] result = new string [fields.Length];
501 for (int i = 0; i < fields.Length; ++i)
502 result [i] = fields [i].Name;
507 NotImplementedException CreateNIE () {
508 return new NotImplementedException ();
511 public virtual Array GetEnumValues () {
513 throw new ArgumentException ("Type is not an enumeration", "enumType");
518 bool IsValidEnumType (Type type) {
519 return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
522 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
523 public virtual string GetEnumName (object value) {
525 throw new ArgumentException ("Value is null", "value");
526 if (!IsValidEnumType (value.GetType ()))
527 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
529 throw new ArgumentException ("Type is not an enumeration", "enumType");
532 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
534 for (int i = 0; i < fields.Length; ++i) {
535 var fv = fields [i].GetValue (null);
538 //XXX we can't use 'this' as argument as it might be an UserType
539 obj = Enum.ToObject (fv.GetType (), value);
540 } catch (OverflowException) {
542 } catch (InvalidCastException) {
543 throw new ArgumentException ("Value is not valid", "value");
547 return fields [i].Name;
553 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
554 public virtual bool IsEnumDefined (object value) {
556 throw new ArgumentException ("Value is null", "value");
558 throw new ArgumentException ("Type is not an enumeration", "enumType");
560 Type vt = value.GetType ();
561 if (!IsValidEnumType (vt) && vt != typeof (string))
562 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
564 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
566 if (value is string) {
567 for (int i = 0; i < fields.Length; ++i) {
568 if (fields [i].Name.Equals (value))
572 if (vt != this && vt != GetEnumUnderlyingType ())
573 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
576 for (int i = 0; i < fields.Length; ++i) {
577 var fv = fields [i].GetValue (null);
580 //XXX we can't use 'this' as argument as it might be an UserType
581 obj = Enum.ToObject (fv.GetType (), value);
582 } catch (OverflowException) {
584 } catch (InvalidCastException) {
585 throw new ArgumentException ("Value is not valid", "value");
595 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
597 return GetType (typeName, assemblyResolver, typeResolver, false, false);
600 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
602 return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
605 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
607 TypeSpec spec = TypeSpec.Parse (typeName);
608 return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
611 public virtual bool IsSecurityTransparent
613 get { throw CreateNIE (); }
616 public virtual bool IsSecurityCritical
618 get { throw CreateNIE (); }
621 public virtual bool IsSecuritySafeCritical
623 get { throw CreateNIE (); }
627 [MethodImplAttribute(MethodImplOptions.InternalCall)]
628 internal extern bool EqualsInternal (Type type);
630 [MethodImplAttribute(MethodImplOptions.InternalCall)]
631 private static extern Type internal_from_handle (IntPtr handle);
633 [MethodImplAttribute(MethodImplOptions.InternalCall)]
634 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
636 public static Type GetType(string typeName)
638 if (typeName == null)
639 throw new ArgumentNullException ("TypeName");
641 return internal_from_name (typeName, false, false);
644 public static Type GetType(string typeName, bool throwOnError)
646 if (typeName == null)
647 throw new ArgumentNullException ("TypeName");
649 Type type = internal_from_name (typeName, throwOnError, false);
650 if (throwOnError && type == null)
651 throw new TypeLoadException ("Error loading '" + typeName + "'");
656 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
658 if (typeName == null)
659 throw new ArgumentNullException ("TypeName");
661 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
662 if (throwOnError && t == null)
663 throw new TypeLoadException ("Error loading '" + typeName + "'");
668 public static Type[] GetTypeArray (object[] args) {
670 throw new ArgumentNullException ("args");
673 ret = new Type [args.Length];
674 for (int i = 0; i < args.Length; ++i)
675 ret [i] = args[i].GetType ();
679 [MethodImplAttribute(MethodImplOptions.InternalCall)]
680 internal extern static TypeCode GetTypeCodeInternal (Type type);
685 TypeCode GetTypeCodeImpl () {
687 if (type is MonoType)
688 return GetTypeCodeInternal (type);
689 if (type is TypeBuilder)
690 return ((TypeBuilder)type).GetTypeCodeInternal ();
692 type = type.UnderlyingSystemType;
694 if (!type.IsSystemType)
695 return TypeCode.Object;
697 return GetTypeCodeInternal (type);
700 public static TypeCode GetTypeCode (Type type) {
702 /* MS.NET returns this */
703 return TypeCode.Empty;
704 return type.GetTypeCodeImpl ();
707 [MonoTODO("This operation is currently not supported by Mono")]
708 public static Type GetTypeFromCLSID (Guid clsid)
710 throw new NotImplementedException ();
713 [MonoTODO("This operation is currently not supported by Mono")]
714 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
716 throw new NotImplementedException ();
719 [MonoTODO("This operation is currently not supported by Mono")]
720 public static Type GetTypeFromCLSID (Guid clsid, string server)
722 throw new NotImplementedException ();
725 [MonoTODO("This operation is currently not supported by Mono")]
726 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
728 throw new NotImplementedException ();
731 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
733 if (handle.Value == IntPtr.Zero)
734 // This is not consistent with the other GetXXXFromHandle methods, but
735 // MS.NET seems to do this
738 return internal_from_handle (handle.Value);
741 [MonoTODO("Mono does not support COM")]
742 public static Type GetTypeFromProgID (string progID)
744 throw new NotImplementedException ();
747 [MonoTODO("Mono does not support COM")]
748 public static Type GetTypeFromProgID (string progID, bool throwOnError)
750 throw new NotImplementedException ();
753 [MonoTODO("Mono does not support COM")]
754 public static Type GetTypeFromProgID (string progID, string server)
756 throw new NotImplementedException ();
759 [MonoTODO("Mono does not support COM")]
760 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
762 throw new NotImplementedException ();
765 public static RuntimeTypeHandle GetTypeHandle (object o)
768 throw new ArgumentNullException ();
770 return o.GetType().TypeHandle;
773 [MethodImplAttribute(MethodImplOptions.InternalCall)]
774 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
776 [MethodImplAttribute(MethodImplOptions.InternalCall)]
777 internal static extern bool type_is_assignable_from (Type a, Type b);
779 public new Type GetType ()
781 return base.GetType ();
785 public virtual bool IsSubclassOf (Type c)
787 if (c == null || c == this)
790 // Fast check for system types
792 return c.IsSystemType && type_is_subtype_of (this, c, false);
794 // User defined types depend on this behavior
795 for (Type type = BaseType; type != null; type = type.BaseType)
802 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
805 throw new ArgumentNullException ("filter");
807 ArrayList ifaces = new ArrayList ();
808 foreach (Type iface in GetInterfaces ()) {
809 if (filter (iface, filterCriteria))
813 return (Type []) ifaces.ToArray (typeof (Type));
816 public Type GetInterface (string name) {
817 return GetInterface (name, false);
820 public abstract Type GetInterface (string name, bool ignoreCase);
822 [MethodImplAttribute(MethodImplOptions.InternalCall)]
823 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
826 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
828 throw new NotSupportedException ("Derived classes must provide an implementation.");
829 if (!interfaceType.IsSystemType)
830 throw new ArgumentException ("interfaceType", "Type is an user type");
831 InterfaceMapping res;
832 if (interfaceType == null)
833 throw new ArgumentNullException ("interfaceType");
834 if (!interfaceType.IsInterface)
835 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
837 throw new ArgumentException ("'this' type cannot be an interface itself");
838 res.TargetType = this;
839 res.InterfaceType = interfaceType;
840 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
841 if (res.TargetMethods == null)
842 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
847 public abstract Type[] GetInterfaces ();
849 public virtual bool IsAssignableFrom (Type c)
857 if (c is TypeBuilder)
858 return ((TypeBuilder)c).IsAssignableTo (this);
860 /* Handle user defined type classes */
862 Type systemType = UnderlyingSystemType;
863 if (!systemType.IsSystemType)
866 Type other = c.UnderlyingSystemType;
867 if (!other.IsSystemType)
870 return systemType.IsAssignableFrom (other);
873 if (!c.IsSystemType) {
874 Type underlyingType = c.UnderlyingSystemType;
875 if (!underlyingType.IsSystemType)
877 return IsAssignableFrom (underlyingType);
880 return type_is_assignable_from (this, c);
883 [MethodImplAttribute(MethodImplOptions.InternalCall)]
884 extern static bool IsInstanceOfType (Type type, object o);
886 public virtual bool IsInstanceOfType (object o)
888 Type type = UnderlyingSystemType;
889 if (!type.IsSystemType)
891 return IsInstanceOfType (type, o);
894 public virtual int GetArrayRank ()
896 throw new NotSupportedException (); // according to MSDN
899 public abstract Type GetElementType ();
901 public EventInfo GetEvent (string name)
903 return GetEvent (name, DefaultBindingFlags);
906 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
908 public virtual EventInfo[] GetEvents ()
910 return GetEvents (DefaultBindingFlags);
913 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
915 public FieldInfo GetField( string name)
917 return GetField (name, DefaultBindingFlags);
920 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
922 public FieldInfo[] GetFields ()
924 return GetFields (DefaultBindingFlags);
927 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
929 public override int GetHashCode()
931 Type t = UnderlyingSystemType;
932 if (t != null && t != this)
933 return t.GetHashCode ();
934 return (int)_impl.Value;
937 public MemberInfo[] GetMember (string name)
939 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
942 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
944 return GetMember (name, MemberTypes.All, bindingAttr);
947 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
950 throw new ArgumentNullException ("name");
951 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
952 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
954 return FindMembers (type, bindingAttr, FilterName, name);
957 public MemberInfo[] GetMembers ()
959 return GetMembers (DefaultBindingFlags);
962 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
964 public MethodInfo GetMethod (string name)
967 throw new ArgumentNullException ("name");
968 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
971 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
974 throw new ArgumentNullException ("name");
976 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
979 public MethodInfo GetMethod (string name, Type[] types)
981 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
984 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
986 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
989 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
990 Type[] types, ParameterModifier[] modifiers)
992 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
995 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
996 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
999 throw new ArgumentNullException ("name");
1001 throw new ArgumentNullException ("types");
1003 for (int i = 0; i < types.Length; i++)
1004 if (types[i] == null)
1005 throw new ArgumentNullException ("types");
1007 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1010 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1011 CallingConventions callConvention, Type[] types,
1012 ParameterModifier[] modifiers);
1014 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1015 CallingConventions callConvention, Type[] types,
1016 ParameterModifier[] modifiers)
1018 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1021 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1023 throw new System.InvalidOperationException ("can only be called in generic type");
1026 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1028 throw new System.InvalidOperationException ("can only be called in generic type");
1031 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1033 throw new System.InvalidOperationException ("can only be called in generic type");
1037 public MethodInfo[] GetMethods ()
1039 return GetMethods (DefaultBindingFlags);
1042 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1044 public Type GetNestedType (string name)
1046 return GetNestedType (name, DefaultBindingFlags);
1049 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1051 public Type[] GetNestedTypes ()
1053 return GetNestedTypes (DefaultBindingFlags);
1056 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1059 public PropertyInfo[] GetProperties ()
1061 return GetProperties (DefaultBindingFlags);
1064 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1067 public PropertyInfo GetProperty (string name)
1070 throw new ArgumentNullException ("name");
1072 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1075 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1078 throw new ArgumentNullException ("name");
1079 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1082 public PropertyInfo GetProperty (string name, Type returnType)
1085 throw new ArgumentNullException ("name");
1086 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1089 public PropertyInfo GetProperty (string name, Type[] types)
1091 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1094 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1096 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1099 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1101 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1104 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1105 Type[] types, ParameterModifier[] modifiers)
1108 throw new ArgumentNullException ("name");
1110 throw new ArgumentNullException ("types");
1112 foreach (Type t in types) {
1114 throw new ArgumentNullException ("types");
1117 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1120 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1121 Type returnType, Type[] types, ParameterModifier[] modifiers);
1123 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1124 Type returnType, Type[] types, ParameterModifier[] modifiers)
1126 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1129 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1131 CallingConventions callConvention,
1133 ParameterModifier[] modifiers);
1135 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1136 protected abstract bool HasElementTypeImpl ();
1137 protected abstract bool IsArrayImpl ();
1138 protected abstract bool IsByRefImpl ();
1139 protected abstract bool IsCOMObjectImpl ();
1140 protected abstract bool IsPointerImpl ();
1141 protected abstract bool IsPrimitiveImpl ();
1143 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1144 internal static extern bool IsArrayImpl (Type type);
1146 protected virtual bool IsValueTypeImpl ()
1148 if (this == typeof (ValueType) || this == typeof (Enum))
1151 return IsSubclassOf (typeof (ValueType));
1154 protected virtual bool IsContextfulImpl ()
1156 return typeof (ContextBoundObject).IsAssignableFrom (this);
1159 protected virtual bool IsMarshalByRefImpl ()
1161 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1165 public ConstructorInfo GetConstructor (Type[] types)
1167 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1171 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1172 Type[] types, ParameterModifier[] modifiers)
1174 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1178 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1179 CallingConventions callConvention,
1180 Type[] types, ParameterModifier[] modifiers)
1183 throw new ArgumentNullException ("types");
1185 foreach (Type t in types) {
1187 throw new ArgumentNullException ("types");
1190 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1194 public ConstructorInfo[] GetConstructors ()
1196 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1200 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1202 public virtual MemberInfo[] GetDefaultMembers ()
1204 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1205 if (att.Length == 0)
1206 return new MemberInfo [0];
1208 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1209 return (member != null) ? member : new MemberInfo [0];
1212 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1213 MemberFilter filter, object filterCriteria)
1215 MemberInfo[] result;
1216 ArrayList l = new ArrayList ();
1218 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1219 // this.FullName, this.GetType().FullName, this.obj_address());
1220 if ((memberType & MemberTypes.Method) != 0) {
1221 MethodInfo[] c = GetMethods (bindingAttr);
1222 if (filter != null) {
1223 foreach (MemberInfo m in c) {
1224 if (filter (m, filterCriteria))
1231 if ((memberType & MemberTypes.Constructor) != 0) {
1232 ConstructorInfo[] c = GetConstructors (bindingAttr);
1233 if (filter != null) {
1234 foreach (MemberInfo m in c) {
1235 if (filter (m, filterCriteria))
1242 if ((memberType & MemberTypes.Property) != 0) {
1243 PropertyInfo[] c = GetProperties (bindingAttr);
1246 if (filter != null) {
1247 foreach (MemberInfo m in c) {
1248 if (filter (m, filterCriteria))
1256 if ((memberType & MemberTypes.Event) != 0) {
1257 EventInfo[] c = GetEvents (bindingAttr);
1258 if (filter != null) {
1259 foreach (MemberInfo m in c) {
1260 if (filter (m, filterCriteria))
1267 if ((memberType & MemberTypes.Field) != 0) {
1268 FieldInfo[] c = GetFields (bindingAttr);
1269 if (filter != null) {
1270 foreach (MemberInfo m in c) {
1271 if (filter (m, filterCriteria))
1278 if ((memberType & MemberTypes.NestedType) != 0) {
1279 Type[] c = GetNestedTypes (bindingAttr);
1280 if (filter != null) {
1281 foreach (MemberInfo m in c) {
1282 if (filter (m, filterCriteria)) {
1291 switch (memberType) {
1292 case MemberTypes.Constructor :
1293 result = new ConstructorInfo [l.Count];
1295 case MemberTypes.Event :
1296 result = new EventInfo [l.Count];
1298 case MemberTypes.Field :
1299 result = new FieldInfo [l.Count];
1301 case MemberTypes.Method :
1302 result = new MethodInfo [l.Count];
1304 case MemberTypes.NestedType :
1305 case MemberTypes.TypeInfo :
1306 result = new Type [l.Count];
1308 case MemberTypes.Property :
1309 result = new PropertyInfo [l.Count];
1312 result = new MemberInfo [l.Count];
1320 [DebuggerStepThrough]
1321 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1323 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1327 [DebuggerStepThrough]
1328 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1329 object target, object[] args, CultureInfo culture)
1331 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1334 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1335 Binder binder, object target, object[] args,
1336 ParameterModifier[] modifiers,
1337 CultureInfo culture, string[] namedParameters);
1339 public override string ToString()
1344 internal virtual bool IsCompilerContext {
1346 AssemblyBuilder builder = Assembly as AssemblyBuilder;
1347 return builder != null && builder.IsCompilerContext;
1351 internal virtual Type InternalResolve ()
1353 return UnderlyingSystemType;
1356 internal bool IsSystemType {
1358 return _impl.Value != IntPtr.Zero;
1362 public virtual Type[] GetGenericArguments ()
1364 throw new NotSupportedException ();
1367 public virtual bool ContainsGenericParameters {
1368 get { return false; }
1371 public virtual extern bool IsGenericTypeDefinition {
1372 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1376 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1377 internal extern Type GetGenericTypeDefinition_impl ();
1379 public virtual Type GetGenericTypeDefinition ()
1381 throw new NotSupportedException ("Derived classes must provide an implementation.");
1384 public virtual extern bool IsGenericType {
1385 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1389 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1390 static extern Type MakeGenericType (Type gt, Type [] types);
1392 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1394 if (type.Assembly is AssemblyBuilder)
1395 return (AssemblyBuilder)type.Assembly;
1397 if (type.HasElementType)
1398 return PeelAssemblyBuilder (type.GetElementType ());
1400 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1403 foreach (Type arg in type.GetGenericArguments ()) {
1404 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1411 public virtual Type MakeGenericType (params Type[] typeArguments)
1414 throw new NotSupportedException ();
1415 if (!IsGenericTypeDefinition)
1416 throw new InvalidOperationException ("not a generic type definition");
1417 if (typeArguments == null)
1418 throw new ArgumentNullException ("typeArguments");
1419 if (GetGenericArguments().Length != typeArguments.Length)
1420 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");
1422 bool hasUserType = false;
1423 AssemblyBuilder compilerContext = null;
1425 Type[] systemTypes = new Type[typeArguments.Length];
1426 for (int i = 0; i < typeArguments.Length; ++i) {
1427 Type t = typeArguments [i];
1429 throw new ArgumentNullException ("typeArguments");
1431 if (!(t is MonoType))
1433 if (t.IsCompilerContext)
1434 compilerContext = PeelAssemblyBuilder (t);
1435 systemTypes [i] = t;
1439 if (compilerContext != null)
1440 return compilerContext.MakeGenericType (this, typeArguments);
1441 return new MonoGenericClass (this, typeArguments);
1444 Type res = MakeGenericType (this, systemTypes);
1446 throw new TypeLoadException ();
1450 public virtual bool IsGenericParameter {
1456 public bool IsNested {
1458 return DeclaringType != null;
1462 public bool IsVisible {
1465 return DeclaringType.IsVisible;
1471 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1472 extern int GetGenericParameterPosition ();
1474 public virtual int GenericParameterPosition {
1476 int res = GetGenericParameterPosition ();
1478 throw new InvalidOperationException ();
1483 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1484 extern GenericParameterAttributes GetGenericParameterAttributes ();
1486 public virtual GenericParameterAttributes GenericParameterAttributes {
1489 throw new NotSupportedException ("Derived classes must provide an implementation.");
1491 if (!IsGenericParameter)
1492 throw new InvalidOperationException ();
1494 return GetGenericParameterAttributes ();
1498 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1499 extern Type[] GetGenericParameterConstraints_impl ();
1501 public virtual Type[] GetGenericParameterConstraints ()
1504 throw new InvalidOperationException ();
1506 if (!IsGenericParameter)
1507 throw new InvalidOperationException ();
1509 return GetGenericParameterConstraints_impl ();
1512 public virtual MethodBase DeclaringMethod {
1518 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1519 extern Type make_array_type (int rank);
1521 public virtual Type MakeArrayType ()
1524 throw new NotSupportedException ("Derived classes must provide an implementation.");
1525 return make_array_type (0);
1528 public virtual Type MakeArrayType (int rank)
1531 throw new NotSupportedException ("Derived classes must provide an implementation.");
1532 if (rank < 1 || rank > 255)
1533 throw new IndexOutOfRangeException ();
1534 return make_array_type (rank);
1537 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1538 extern Type make_byref_type ();
1540 public virtual Type MakeByRefType ()
1543 throw new NotSupportedException ("Derived classes must provide an implementation.");
1545 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1546 return make_byref_type ();
1549 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1550 static extern Type MakePointerType (Type type);
1552 public virtual Type MakePointerType ()
1555 throw new NotSupportedException ("Derived classes must provide an implementation.");
1556 return MakePointerType (this);
1559 public static Type ReflectionOnlyGetType (string typeName,
1560 bool throwIfNotFound,
1563 if (typeName == null)
1564 throw new ArgumentNullException ("typeName");
1565 int idx = typeName.IndexOf (',');
1566 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1567 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1568 string an = typeName.Substring (idx + 1);
1571 a = Assembly.ReflectionOnlyLoad (an);
1573 if (throwIfNotFound)
1577 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1580 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1581 extern void GetPacking (out int packing, out int size);
1583 public virtual StructLayoutAttribute StructLayoutAttribute {
1588 return GetStructLayoutAttribute ();
1593 internal StructLayoutAttribute GetStructLayoutAttribute ()
1597 if (IsLayoutSequential)
1598 kind = LayoutKind.Sequential;
1599 else if (IsExplicitLayout)
1600 kind = LayoutKind.Explicit;
1602 kind = LayoutKind.Auto;
1604 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1607 attr.CharSet = CharSet.Unicode;
1608 else if (IsAnsiClass)
1609 attr.CharSet = CharSet.Ansi;
1611 attr.CharSet = CharSet.Auto;
1613 if (kind != LayoutKind.Auto)
1614 GetPacking (out attr.Pack, out attr.Size);
1619 internal object[] GetPseudoCustomAttributes ()
1623 /* IsSerializable returns true for delegates/enums as well */
1624 if ((Attributes & TypeAttributes.Serializable) != 0)
1626 if ((Attributes & TypeAttributes.Import) != 0)
1631 object[] attrs = new object [count];
1634 if ((Attributes & TypeAttributes.Serializable) != 0)
1635 attrs [count ++] = new SerializableAttribute ();
1636 if ((Attributes & TypeAttributes.Import) != 0)
1637 attrs [count ++] = new ComImportAttribute ();
1643 #if NET_4_0 || BOOTSTRAP_NET_4_0
1644 public virtual bool IsEquivalentTo (Type other)
1646 return this == other;
1651 * Return whenever this object is an instance of a user defined subclass
1652 * of System.Type or an instance of TypeDelegator.
1654 internal bool IsUserType {
1657 * subclasses cannot modify _impl so if it is zero, it means the
1658 * type is not created by the runtime.
1660 return _impl.Value == IntPtr.Zero &&
1661 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1665 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1667 throw new NotImplementedException ();
1670 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1672 throw new NotImplementedException ();
1675 void _Type.GetTypeInfoCount (out uint pcTInfo)
1677 throw new NotImplementedException ();
1680 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1682 throw new NotImplementedException ();