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 ();
230 return IsSubclassOf (typeof (Enum));
234 public bool IsExplicitLayout {
236 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
240 public bool IsImport {
242 return (Attributes & TypeAttributes.Import) != 0;
246 public bool IsInterface {
248 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
252 public bool IsLayoutSequential {
254 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
258 public bool IsMarshalByRef {
260 return IsMarshalByRefImpl ();
264 public bool IsNestedAssembly {
266 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
270 public bool IsNestedFamANDAssem {
272 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
276 public bool IsNestedFamily {
278 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
282 public bool IsNestedFamORAssem {
284 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
288 public bool IsNestedPrivate {
290 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
294 public bool IsNestedPublic {
296 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
300 public bool IsNotPublic {
302 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
306 public bool IsPointer {
308 return IsPointerImpl ();
312 public bool IsPrimitive {
314 return IsPrimitiveImpl ();
318 public bool IsPublic {
320 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
324 public bool IsSealed {
326 return (Attributes & TypeAttributes.Sealed) != 0;
330 public bool IsSerializable {
332 if ((Attributes & TypeAttributes.Serializable) != 0)
335 // Enums and delegates are always serializable
337 Type type = UnderlyingSystemType;
341 // Fast check for system types
342 if (type.IsSystemType)
343 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
345 // User defined types depend on this behavior
347 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
350 type = type.BaseType;
351 } while (type != null);
357 public bool IsSpecialName {
359 return (Attributes & TypeAttributes.SpecialName) != 0;
363 public bool IsUnicodeClass {
365 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
369 public bool IsValueType {
371 return IsValueTypeImpl ();
375 public override MemberTypes MemberType {
376 get {return MemberTypes.TypeInfo;}
380 public abstract Module Module {get;}
382 public abstract string Namespace {get;}
384 public override Type ReflectedType {
390 public virtual RuntimeTypeHandle TypeHandle {
391 get { throw new ArgumentException ("Derived class must provide implementation."); }
395 public ConstructorInfo TypeInitializer {
397 return GetConstructorImpl (
398 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
400 CallingConventions.Any,
407 * This has NOTHING to do with getting the base type of an enum. Use
408 * Enum.GetUnderlyingType () for that.
410 public abstract Type UnderlyingSystemType {get;}
412 public override bool Equals (object o)
415 return Equals (o as Type);
420 Type me = UnderlyingSystemType;
423 return me.EqualsInternal (o as Type);
428 public virtual bool Equals (Type o)
431 public bool Equals (Type o)
439 Type me = UnderlyingSystemType;
442 return me.EqualsInternal (o.UnderlyingSystemType);
446 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
447 public static bool operator == (Type left, Type right)
449 return Object.ReferenceEquals (left, right);
452 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
453 public static bool operator != (Type left, Type right)
455 return !Object.ReferenceEquals (left, right);
458 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
459 public virtual Type GetEnumUnderlyingType () {
461 throw new ArgumentException ("Type is not an enumeration", "enumType");
463 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
465 if (fields == null || fields.Length != 1)
466 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
468 return fields [0].FieldType;
471 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
472 public virtual string[] GetEnumNames () {
474 throw new ArgumentException ("Type is not an enumeration", "enumType");
476 var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
478 string [] result = new string [fields.Length];
479 for (int i = 0; i < fields.Length; ++i)
480 result [i] = fields [i].Name;
486 [MethodImplAttribute(MethodImplOptions.InternalCall)]
487 internal extern bool EqualsInternal (Type type);
489 [MethodImplAttribute(MethodImplOptions.InternalCall)]
490 private static extern Type internal_from_handle (IntPtr handle);
492 [MethodImplAttribute(MethodImplOptions.InternalCall)]
493 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
495 public static Type GetType(string typeName)
497 if (typeName == null)
498 throw new ArgumentNullException ("TypeName");
500 return internal_from_name (typeName, false, false);
503 public static Type GetType(string typeName, bool throwOnError)
505 if (typeName == null)
506 throw new ArgumentNullException ("TypeName");
508 Type type = internal_from_name (typeName, throwOnError, false);
509 if (throwOnError && type == null)
510 throw new TypeLoadException ("Error loading '" + typeName + "'");
515 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
517 if (typeName == null)
518 throw new ArgumentNullException ("TypeName");
520 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
521 if (throwOnError && t == null)
522 throw new TypeLoadException ("Error loading '" + typeName + "'");
527 public static Type[] GetTypeArray (object[] args) {
529 throw new ArgumentNullException ("args");
532 ret = new Type [args.Length];
533 for (int i = 0; i < args.Length; ++i)
534 ret [i] = args[i].GetType ();
538 [MethodImplAttribute(MethodImplOptions.InternalCall)]
539 internal extern static TypeCode GetTypeCodeInternal (Type type);
544 TypeCode GetTypeCodeImpl () {
546 if (type is MonoType)
547 return GetTypeCodeInternal (type);
549 type = type.UnderlyingSystemType;
551 if (!type.IsSystemType)
552 return TypeCode.Object;
554 return GetTypeCodeInternal (type);
557 public static TypeCode GetTypeCode (Type type) {
559 /* MS.NET returns this */
560 return TypeCode.Empty;
561 return type.GetTypeCodeImpl ();
564 [MonoTODO("This operation is currently not supported by Mono")]
565 public static Type GetTypeFromCLSID (Guid clsid)
567 throw new NotImplementedException ();
570 [MonoTODO("This operation is currently not supported by Mono")]
571 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
573 throw new NotImplementedException ();
576 [MonoTODO("This operation is currently not supported by Mono")]
577 public static Type GetTypeFromCLSID (Guid clsid, string server)
579 throw new NotImplementedException ();
582 [MonoTODO("This operation is currently not supported by Mono")]
583 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
585 throw new NotImplementedException ();
588 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
590 if (handle.Value == IntPtr.Zero)
591 // This is not consistent with the other GetXXXFromHandle methods, but
592 // MS.NET seems to do this
595 return internal_from_handle (handle.Value);
598 [MonoTODO("Mono does not support COM")]
599 public static Type GetTypeFromProgID (string progID)
601 throw new NotImplementedException ();
604 [MonoTODO("Mono does not support COM")]
605 public static Type GetTypeFromProgID (string progID, bool throwOnError)
607 throw new NotImplementedException ();
610 [MonoTODO("Mono does not support COM")]
611 public static Type GetTypeFromProgID (string progID, string server)
613 throw new NotImplementedException ();
616 [MonoTODO("Mono does not support COM")]
617 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
619 throw new NotImplementedException ();
622 public static RuntimeTypeHandle GetTypeHandle (object o)
625 throw new ArgumentNullException ();
627 return o.GetType().TypeHandle;
630 [MethodImplAttribute(MethodImplOptions.InternalCall)]
631 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
633 [MethodImplAttribute(MethodImplOptions.InternalCall)]
634 internal static extern bool type_is_assignable_from (Type a, Type b);
636 public new Type GetType ()
638 return base.GetType ();
642 public virtual bool IsSubclassOf (Type c)
644 if (c == null || c == this)
647 // Fast check for system types
649 return c.IsSystemType && type_is_subtype_of (this, c, false);
651 // User defined types depend on this behavior
652 for (Type type = BaseType; type != null; type = type.BaseType)
659 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
662 throw new ArgumentNullException ("filter");
664 ArrayList ifaces = new ArrayList ();
665 foreach (Type iface in GetInterfaces ()) {
666 if (filter (iface, filterCriteria))
670 return (Type []) ifaces.ToArray (typeof (Type));
673 public Type GetInterface (string name) {
674 return GetInterface (name, false);
677 public abstract Type GetInterface (string name, bool ignoreCase);
679 [MethodImplAttribute(MethodImplOptions.InternalCall)]
680 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
683 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
685 throw new NotSupportedException ("Derived classes must provide an implementation.");
686 if (!interfaceType.IsSystemType)
687 throw new ArgumentException ("interfaceType", "Type is an user type");
688 InterfaceMapping res;
689 if (interfaceType == null)
690 throw new ArgumentNullException ("interfaceType");
691 if (!interfaceType.IsInterface)
692 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
694 throw new ArgumentException ("'this' type cannot be an interface itself");
695 res.TargetType = this;
696 res.InterfaceType = interfaceType;
697 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
698 if (res.TargetMethods == null)
699 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
704 public abstract Type[] GetInterfaces ();
706 public virtual bool IsAssignableFrom (Type c)
714 if (c is TypeBuilder)
715 return ((TypeBuilder)c).IsAssignableTo (this);
717 /* Handle user defined type classes */
719 Type systemType = UnderlyingSystemType;
720 if (!systemType.IsSystemType)
723 Type other = c.UnderlyingSystemType;
724 if (!other.IsSystemType)
727 return systemType.IsAssignableFrom (other);
730 if (!c.IsSystemType) {
731 Type underlyingType = c.UnderlyingSystemType;
732 if (!underlyingType.IsSystemType)
734 return IsAssignableFrom (underlyingType);
737 return type_is_assignable_from (this, c);
740 [MethodImplAttribute(MethodImplOptions.InternalCall)]
741 extern static bool IsInstanceOfType (Type type, object o);
743 public virtual bool IsInstanceOfType (object o)
745 Type type = UnderlyingSystemType;
746 if (!type.IsSystemType)
748 return IsInstanceOfType (type, o);
751 public virtual int GetArrayRank ()
753 throw new NotSupportedException (); // according to MSDN
756 public abstract Type GetElementType ();
758 public EventInfo GetEvent (string name)
760 return GetEvent (name, DefaultBindingFlags);
763 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
765 public virtual EventInfo[] GetEvents ()
767 return GetEvents (DefaultBindingFlags);
770 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
772 public FieldInfo GetField( string name)
774 return GetField (name, DefaultBindingFlags);
777 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
779 public FieldInfo[] GetFields ()
781 return GetFields (DefaultBindingFlags);
784 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
786 public override int GetHashCode()
788 Type t = UnderlyingSystemType;
789 if (t != null && t != this)
790 return t.GetHashCode ();
791 return (int)_impl.Value;
794 public MemberInfo[] GetMember (string name)
796 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
799 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
801 return GetMember (name, MemberTypes.All, bindingAttr);
804 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
807 throw new ArgumentNullException ("name");
808 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
809 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
811 return FindMembers (type, bindingAttr, FilterName, name);
814 public MemberInfo[] GetMembers ()
816 return GetMembers (DefaultBindingFlags);
819 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
821 public MethodInfo GetMethod (string name)
824 throw new ArgumentNullException ("name");
825 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
828 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
831 throw new ArgumentNullException ("name");
833 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
836 public MethodInfo GetMethod (string name, Type[] types)
838 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
841 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
843 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
846 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
847 Type[] types, ParameterModifier[] modifiers)
849 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
852 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
853 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
856 throw new ArgumentNullException ("name");
858 throw new ArgumentNullException ("types");
860 for (int i = 0; i < types.Length; i++)
861 if (types[i] == null)
862 throw new ArgumentNullException ("types");
864 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
867 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
868 CallingConventions callConvention, Type[] types,
869 ParameterModifier[] modifiers);
871 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
872 CallingConventions callConvention, Type[] types,
873 ParameterModifier[] modifiers)
875 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
878 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
880 throw new System.InvalidOperationException ("can only be called in generic type");
883 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
885 throw new System.InvalidOperationException ("can only be called in generic type");
888 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
890 throw new System.InvalidOperationException ("can only be called in generic type");
894 public MethodInfo[] GetMethods ()
896 return GetMethods (DefaultBindingFlags);
899 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
901 public Type GetNestedType (string name)
903 return GetNestedType (name, DefaultBindingFlags);
906 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
908 public Type[] GetNestedTypes ()
910 return GetNestedTypes (DefaultBindingFlags);
913 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
916 public PropertyInfo[] GetProperties ()
918 return GetProperties (DefaultBindingFlags);
921 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
924 public PropertyInfo GetProperty (string name)
927 throw new ArgumentNullException ("name");
929 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
932 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
935 throw new ArgumentNullException ("name");
936 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
939 public PropertyInfo GetProperty (string name, Type returnType)
942 throw new ArgumentNullException ("name");
943 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
946 public PropertyInfo GetProperty (string name, Type[] types)
948 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
951 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
953 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
956 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
958 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
961 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
962 Type[] types, ParameterModifier[] modifiers)
965 throw new ArgumentNullException ("name");
967 throw new ArgumentNullException ("types");
969 foreach (Type t in types) {
971 throw new ArgumentNullException ("types");
974 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
977 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
978 Type returnType, Type[] types, ParameterModifier[] modifiers);
980 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
981 Type returnType, Type[] types, ParameterModifier[] modifiers)
983 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
986 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
988 CallingConventions callConvention,
990 ParameterModifier[] modifiers);
992 protected abstract TypeAttributes GetAttributeFlagsImpl ();
993 protected abstract bool HasElementTypeImpl ();
994 protected abstract bool IsArrayImpl ();
995 protected abstract bool IsByRefImpl ();
996 protected abstract bool IsCOMObjectImpl ();
997 protected abstract bool IsPointerImpl ();
998 protected abstract bool IsPrimitiveImpl ();
1000 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1001 internal static extern bool IsArrayImpl (Type type);
1003 protected virtual bool IsValueTypeImpl ()
1005 if (this == typeof (ValueType) || this == typeof (Enum))
1008 return IsSubclassOf (typeof (ValueType));
1011 protected virtual bool IsContextfulImpl ()
1013 return typeof (ContextBoundObject).IsAssignableFrom (this);
1016 protected virtual bool IsMarshalByRefImpl ()
1018 return typeof (MarshalByRefObject).IsAssignableFrom (this);
1022 public ConstructorInfo GetConstructor (Type[] types)
1024 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1028 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1029 Type[] types, ParameterModifier[] modifiers)
1031 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1035 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1036 CallingConventions callConvention,
1037 Type[] types, ParameterModifier[] modifiers)
1040 throw new ArgumentNullException ("types");
1042 foreach (Type t in types) {
1044 throw new ArgumentNullException ("types");
1047 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1051 public ConstructorInfo[] GetConstructors ()
1053 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1057 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1059 public virtual MemberInfo[] GetDefaultMembers ()
1061 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1062 if (att.Length == 0)
1063 return new MemberInfo [0];
1065 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1066 return (member != null) ? member : new MemberInfo [0];
1069 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1070 MemberFilter filter, object filterCriteria)
1072 MemberInfo[] result;
1073 ArrayList l = new ArrayList ();
1075 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1076 // this.FullName, this.GetType().FullName, this.obj_address());
1077 if ((memberType & MemberTypes.Method) != 0) {
1078 MethodInfo[] c = GetMethods (bindingAttr);
1079 if (filter != null) {
1080 foreach (MemberInfo m in c) {
1081 if (filter (m, filterCriteria))
1088 if ((memberType & MemberTypes.Constructor) != 0) {
1089 ConstructorInfo[] c = GetConstructors (bindingAttr);
1090 if (filter != null) {
1091 foreach (MemberInfo m in c) {
1092 if (filter (m, filterCriteria))
1099 if ((memberType & MemberTypes.Property) != 0) {
1101 int count = l.Count;
1103 if (filter != null) {
1105 while ((l.Count == count) && (ptype != null)) {
1106 c = ptype.GetProperties (bindingAttr);
1107 foreach (MemberInfo m in c) {
1108 if (filter (m, filterCriteria))
1111 ptype = ptype.BaseType;
1114 c = GetProperties (bindingAttr);
1118 if ((memberType & MemberTypes.Event) != 0) {
1119 EventInfo[] c = GetEvents (bindingAttr);
1120 if (filter != null) {
1121 foreach (MemberInfo m in c) {
1122 if (filter (m, filterCriteria))
1129 if ((memberType & MemberTypes.Field) != 0) {
1130 FieldInfo[] c = GetFields (bindingAttr);
1131 if (filter != null) {
1132 foreach (MemberInfo m in c) {
1133 if (filter (m, filterCriteria))
1140 if ((memberType & MemberTypes.NestedType) != 0) {
1141 Type[] c = GetNestedTypes (bindingAttr);
1142 if (filter != null) {
1143 foreach (MemberInfo m in c) {
1144 if (filter (m, filterCriteria)) {
1153 switch (memberType) {
1154 case MemberTypes.Constructor :
1155 result = new ConstructorInfo [l.Count];
1157 case MemberTypes.Event :
1158 result = new EventInfo [l.Count];
1160 case MemberTypes.Field :
1161 result = new FieldInfo [l.Count];
1163 case MemberTypes.Method :
1164 result = new MethodInfo [l.Count];
1166 case MemberTypes.NestedType :
1167 case MemberTypes.TypeInfo :
1168 result = new Type [l.Count];
1170 case MemberTypes.Property :
1171 result = new PropertyInfo [l.Count];
1174 result = new MemberInfo [l.Count];
1182 [DebuggerStepThrough]
1183 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1185 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1189 [DebuggerStepThrough]
1190 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1191 object target, object[] args, CultureInfo culture)
1193 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1196 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1197 Binder binder, object target, object[] args,
1198 ParameterModifier[] modifiers,
1199 CultureInfo culture, string[] namedParameters);
1201 public override string ToString()
1206 internal virtual bool IsCompilerContext {
1208 AssemblyBuilder builder = Assembly as AssemblyBuilder;
1209 return builder != null && builder.IsCompilerContext;
1213 internal bool IsSystemType {
1215 return _impl.Value != IntPtr.Zero;
1219 public virtual Type[] GetGenericArguments ()
1221 throw new NotSupportedException ();
1224 public virtual bool ContainsGenericParameters {
1225 get { return false; }
1228 public virtual extern bool IsGenericTypeDefinition {
1229 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1233 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1234 internal extern Type GetGenericTypeDefinition_impl ();
1236 public virtual Type GetGenericTypeDefinition ()
1238 throw new NotSupportedException ("Derived classes must provide an implementation.");
1241 public virtual extern bool IsGenericType {
1242 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1246 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1247 static extern Type MakeGenericType (Type gt, Type [] types);
1249 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1251 if (type.Assembly is AssemblyBuilder)
1252 return (AssemblyBuilder)type.Assembly;
1254 if (type.HasElementType)
1255 return PeelAssemblyBuilder (type.GetElementType ());
1257 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1260 foreach (Type arg in type.GetGenericArguments ()) {
1261 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1268 public virtual Type MakeGenericType (params Type[] typeArguments)
1271 throw new NotSupportedException ();
1272 if (!IsGenericTypeDefinition)
1273 throw new InvalidOperationException ("not a generic type definition");
1274 if (typeArguments == null)
1275 throw new ArgumentNullException ("typeArguments");
1276 if (GetGenericArguments().Length != typeArguments.Length)
1277 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");
1279 bool hasUserType = false;
1280 AssemblyBuilder compilerContext = null;
1282 Type[] systemTypes = new Type[typeArguments.Length];
1283 for (int i = 0; i < typeArguments.Length; ++i) {
1284 Type t = typeArguments [i];
1286 throw new ArgumentNullException ("typeArguments");
1288 if (!(t is MonoType))
1290 if (t.IsCompilerContext)
1291 compilerContext = PeelAssemblyBuilder (t);
1292 systemTypes [i] = t;
1296 if (compilerContext != null)
1297 return compilerContext.MakeGenericType (this, typeArguments);
1298 return new MonoGenericClass (this, typeArguments);
1301 Type res = MakeGenericType (this, systemTypes);
1303 throw new TypeLoadException ();
1307 public virtual bool IsGenericParameter {
1313 public bool IsNested {
1315 return DeclaringType != null;
1319 public bool IsVisible {
1322 return DeclaringType.IsVisible;
1328 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1329 extern int GetGenericParameterPosition ();
1331 public virtual int GenericParameterPosition {
1333 int res = GetGenericParameterPosition ();
1335 throw new InvalidOperationException ();
1340 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1341 extern GenericParameterAttributes GetGenericParameterAttributes ();
1343 public virtual GenericParameterAttributes GenericParameterAttributes {
1346 throw new NotSupportedException ("Derived classes must provide an implementation.");
1348 if (!IsGenericParameter)
1349 throw new InvalidOperationException ();
1351 return GetGenericParameterAttributes ();
1355 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1356 extern Type[] GetGenericParameterConstraints_impl ();
1358 public virtual Type[] GetGenericParameterConstraints ()
1361 throw new InvalidOperationException ();
1363 if (!IsGenericParameter)
1364 throw new InvalidOperationException ();
1366 return GetGenericParameterConstraints_impl ();
1369 public virtual MethodBase DeclaringMethod {
1375 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1376 extern Type make_array_type (int rank);
1378 public virtual Type MakeArrayType ()
1381 throw new NotSupportedException ("Derived classes must provide an implementation.");
1382 return make_array_type (0);
1385 public virtual Type MakeArrayType (int rank)
1388 throw new NotSupportedException ("Derived classes must provide an implementation.");
1389 if (rank < 1 || rank > 255)
1390 throw new IndexOutOfRangeException ();
1391 return make_array_type (rank);
1394 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1395 extern Type make_byref_type ();
1397 public virtual Type MakeByRefType ()
1400 throw new NotSupportedException ("Derived classes must provide an implementation.");
1402 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1403 return make_byref_type ();
1406 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1407 static extern Type MakePointerType (Type type);
1409 public virtual Type MakePointerType ()
1412 throw new NotSupportedException ("Derived classes must provide an implementation.");
1413 return MakePointerType (this);
1416 public static Type ReflectionOnlyGetType (string typeName,
1417 bool throwIfNotFound,
1420 if (typeName == null)
1421 throw new ArgumentNullException ("typeName");
1422 int idx = typeName.IndexOf (',');
1423 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1424 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1425 string an = typeName.Substring (idx + 1);
1428 a = Assembly.ReflectionOnlyLoad (an);
1430 if (throwIfNotFound)
1434 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1437 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1438 extern void GetPacking (out int packing, out int size);
1440 public virtual StructLayoutAttribute StructLayoutAttribute {
1444 if (IsLayoutSequential)
1445 kind = LayoutKind.Sequential;
1446 else if (IsExplicitLayout)
1447 kind = LayoutKind.Explicit;
1449 kind = LayoutKind.Auto;
1451 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1454 attr.CharSet = CharSet.Unicode;
1455 else if (IsAnsiClass)
1456 attr.CharSet = CharSet.Ansi;
1458 attr.CharSet = CharSet.Auto;
1460 if (kind != LayoutKind.Auto)
1461 GetPacking (out attr.Pack, out attr.Size);
1467 internal object[] GetPseudoCustomAttributes ()
1471 /* IsSerializable returns true for delegates/enums as well */
1472 if ((Attributes & TypeAttributes.Serializable) != 0)
1474 if ((Attributes & TypeAttributes.Import) != 0)
1479 object[] attrs = new object [count];
1482 if ((Attributes & TypeAttributes.Serializable) != 0)
1483 attrs [count ++] = new SerializableAttribute ();
1484 if ((Attributes & TypeAttributes.Import) != 0)
1485 attrs [count ++] = new ComImportAttribute ();
1491 #if NET_4_0 || BOOTSTRAP_NET_4_0
1492 public virtual bool IsEquivalentTo (Type other)
1494 return this == other;
1499 * Return whenever this object is an instance of a user defined subclass
1500 * of System.Type or an instance of TypeDelegator.
1502 internal bool IsUserType {
1505 * subclasses cannot modify _impl so if it is zero, it means the
1506 * type is not created by the runtime.
1508 return _impl.Value == IntPtr.Zero &&
1509 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1513 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1515 throw new NotImplementedException ();
1518 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1520 throw new NotImplementedException ();
1523 void _Type.GetTypeInfoCount (out uint pcTInfo)
1525 throw new NotImplementedException ();
1528 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1530 throw new NotImplementedException ();