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)]
47 [ComDefaultInterface (typeof (_Type))]
49 public abstract class Type : MemberInfo, IReflect, _Type {
51 internal RuntimeTypeHandle _impl;
53 public static readonly char Delimiter = '.';
54 public static readonly Type[] EmptyTypes = {};
55 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
56 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
57 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
58 public static readonly object Missing;
60 internal const BindingFlags DefaultBindingFlags =
61 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
63 /* implementation of the delegates for MemberFilter */
64 static bool FilterName_impl (MemberInfo m, object filterCriteria)
66 string name = (string) filterCriteria;
67 if (name == null || name.Length == 0 )
68 return false; // because m.Name cannot be null or empty
70 if (name [name.Length-1] == '*')
71 return string.Compare (name, 0, m.Name, 0, name.Length-1, false, CultureInfo.InvariantCulture) == 0;
73 return name.Equals (m.Name);
76 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
78 string name = (string) filterCriteria;
79 if (name == null || name.Length == 0 )
80 return false; // because m.Name cannot be null or empty
82 if (name [name.Length-1] == '*')
83 return string.Compare (name, 0, m.Name, 0, name.Length-1, true, CultureInfo.InvariantCulture) == 0;
85 return String.Compare (name, m.Name, true, CultureInfo.InvariantCulture) == 0;
88 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
90 int flags = ((IConvertible)filterCriteria).ToInt32 (null);
92 return ((int)((MethodInfo)m).Attributes & flags) != 0;
94 return ((int)((FieldInfo)m).Attributes & flags) != 0;
95 if (m is PropertyInfo)
96 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
98 return ((int)((EventInfo)m).Attributes & flags) != 0;
107 /// The assembly where the type is defined.
109 public abstract Assembly Assembly {
114 /// Gets the fully qualified name for the type including the
115 /// assembly name where the type is defined.
117 public abstract string AssemblyQualifiedName {
122 /// Returns the Attributes associated with the type.
124 public TypeAttributes Attributes {
126 return GetAttributeFlagsImpl ();
131 /// Returns the basetype for this type
133 public abstract Type BaseType {
138 /// Returns the class that declares the member.
140 public override Type DeclaringType {
149 public static Binder DefaultBinder {
151 return Binder.DefaultBinder;
156 /// The full name of the type including its namespace
158 public abstract string FullName {
162 public abstract Guid GUID {
166 public bool HasElementType {
168 return HasElementTypeImpl ();
172 public bool IsAbstract {
174 return (Attributes & TypeAttributes.Abstract) != 0;
178 public bool IsAnsiClass {
180 return (Attributes & TypeAttributes.StringFormatMask)
181 == TypeAttributes.AnsiClass;
185 public bool IsArray {
187 return IsArrayImpl ();
191 public bool IsAutoClass {
193 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
197 public bool IsAutoLayout {
199 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
203 public bool IsByRef {
205 return IsByRefImpl ();
209 public bool IsClass {
214 return !IsSubclassOf (typeof (ValueType));
218 public bool IsCOMObject {
220 return IsCOMObjectImpl ();
224 public bool IsContextful {
226 return IsContextfulImpl ();
232 return IsSubclassOf (typeof (Enum));
236 public bool IsExplicitLayout {
238 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
242 public bool IsImport {
244 return (Attributes & TypeAttributes.Import) != 0;
248 public bool IsInterface {
250 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
254 public bool IsLayoutSequential {
256 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
260 public bool IsMarshalByRef {
262 return IsMarshalByRefImpl ();
266 public bool IsNestedAssembly {
268 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
272 public bool IsNestedFamANDAssem {
274 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
278 public bool IsNestedFamily {
280 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
284 public bool IsNestedFamORAssem {
286 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
290 public bool IsNestedPrivate {
292 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
296 public bool IsNestedPublic {
298 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
302 public bool IsNotPublic {
304 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
308 public bool IsPointer {
310 return IsPointerImpl ();
314 public bool IsPrimitive {
316 return IsPrimitiveImpl ();
320 public bool IsPublic {
322 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
326 public bool IsSealed {
328 return (Attributes & TypeAttributes.Sealed) != 0;
332 public bool IsSerializable {
334 if ((Attributes & TypeAttributes.Serializable) != 0)
337 // Enums and delegates are always serializable
339 Type type = UnderlyingSystemType;
343 // Fast check for system types
344 if (type.IsSystemType)
345 return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
347 // User defined types depend on this behavior
349 if ((type == typeof (Enum)) || (type == typeof (Delegate)))
352 type = type.BaseType;
353 } while (type != null);
359 public bool IsSpecialName {
361 return (Attributes & TypeAttributes.SpecialName) != 0;
365 public bool IsUnicodeClass {
367 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
371 public bool IsValueType {
373 return IsValueTypeImpl ();
377 public override MemberTypes MemberType {
378 get {return MemberTypes.TypeInfo;}
381 #if NET_2_0 || BOOTSTRAP_NET_2_0
384 public abstract Module Module {get;}
386 public abstract string Namespace {get;}
388 public override Type ReflectedType {
395 public virtual RuntimeTypeHandle TypeHandle {
396 get { return default (RuntimeTypeHandle); }
399 public abstract RuntimeTypeHandle TypeHandle {get;}
405 public ConstructorInfo TypeInitializer {
407 return GetConstructorImpl (
408 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
410 CallingConventions.Any,
417 * This has NOTHING to do with getting the base type of an enum. Use
418 * Enum.GetUnderlyingType () for that.
420 public abstract Type UnderlyingSystemType {get;}
422 public override bool Equals (object o)
427 Type cmp = o as Type;
433 public bool Equals (Type o) {
436 return UnderlyingSystemType.EqualsInternal (o.UnderlyingSystemType);
439 [MethodImplAttribute(MethodImplOptions.InternalCall)]
440 internal extern bool EqualsInternal (Type type);
442 [MethodImplAttribute(MethodImplOptions.InternalCall)]
443 private static extern Type internal_from_handle (IntPtr handle);
445 [MethodImplAttribute(MethodImplOptions.InternalCall)]
446 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
448 public static Type GetType(string typeName)
450 if (typeName == null)
452 throw new ArgumentNullException ("TypeName");
454 throw new ArgumentNullException ("className");
457 return internal_from_name (typeName, false, false);
460 public static Type GetType(string typeName, bool throwOnError)
462 if (typeName == null)
464 throw new ArgumentNullException ("TypeName");
466 throw new ArgumentNullException ("className");
469 Type type = internal_from_name (typeName, throwOnError, false);
470 if (throwOnError && type == null)
471 throw new TypeLoadException ("Error loading '" + typeName + "'");
476 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
478 if (typeName == null)
480 throw new ArgumentNullException ("TypeName");
482 throw new ArgumentNullException ("className");
485 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
486 if (throwOnError && t == null)
487 throw new TypeLoadException ("Error loading '" + typeName + "'");
492 public static Type[] GetTypeArray (object[] args) {
494 throw new ArgumentNullException ("args");
497 ret = new Type [args.Length];
498 for (int i = 0; i < args.Length; ++i)
499 ret [i] = args[i].GetType ();
503 [MethodImplAttribute(MethodImplOptions.InternalCall)]
504 internal extern static TypeCode GetTypeCodeInternal (Type type);
506 public static TypeCode GetTypeCode (Type type) {
507 if (type is MonoType)
508 return GetTypeCodeInternal (type);
510 /* MS.NET returns this */
511 return TypeCode.Empty;
513 type = type.UnderlyingSystemType;
515 if (!type.IsSystemType)
516 return TypeCode.Object;
518 return GetTypeCodeInternal (type);
521 [MonoTODO("This operation is currently not supported by Mono")]
522 public static Type GetTypeFromCLSID (Guid clsid)
524 throw new NotImplementedException ();
527 [MonoTODO("This operation is currently not supported by Mono")]
528 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
530 throw new NotImplementedException ();
533 [MonoTODO("This operation is currently not supported by Mono")]
534 public static Type GetTypeFromCLSID (Guid clsid, string server)
536 throw new NotImplementedException ();
539 [MonoTODO("This operation is currently not supported by Mono")]
540 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
542 throw new NotImplementedException ();
545 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
547 if (handle.Value == IntPtr.Zero)
549 // This is not consistent with the other GetXXXFromHandle methods, but
550 // MS.NET seems to do this
553 throw new ArgumentException ("The handle is invalid.");
556 return internal_from_handle (handle.Value);
559 [MonoTODO("Mono does not support COM")]
560 public static Type GetTypeFromProgID (string progID)
562 throw new NotImplementedException ();
565 [MonoTODO("Mono does not support COM")]
566 public static Type GetTypeFromProgID (string progID, bool throwOnError)
568 throw new NotImplementedException ();
571 [MonoTODO("Mono does not support COM")]
572 public static Type GetTypeFromProgID (string progID, string server)
574 throw new NotImplementedException ();
577 [MonoTODO("Mono does not support COM")]
578 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
580 throw new NotImplementedException ();
583 public static RuntimeTypeHandle GetTypeHandle (object o)
587 throw new ArgumentNullException ();
589 throw new ArgumentNullException ("o");
592 return o.GetType().TypeHandle;
595 [MethodImplAttribute(MethodImplOptions.InternalCall)]
596 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
598 [MethodImplAttribute(MethodImplOptions.InternalCall)]
599 internal static extern bool type_is_assignable_from (Type a, Type b);
601 public new Type GetType ()
603 return base.GetType ();
609 public virtual bool IsSubclassOf (Type c)
611 if (c == null || c == this)
614 // Fast check for system types
616 return c.IsSystemType && type_is_subtype_of (this, c, false);
618 // User defined types depend on this behavior
619 for (Type type = BaseType; type != null; type = type.BaseType)
626 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
629 throw new ArgumentNullException ("filter");
631 ArrayList ifaces = new ArrayList ();
632 foreach (Type iface in GetInterfaces ()) {
633 if (filter (iface, filterCriteria))
637 return (Type []) ifaces.ToArray (typeof (Type));
640 public Type GetInterface (string name) {
641 return GetInterface (name, false);
644 public abstract Type GetInterface (string name, bool ignoreCase);
646 [MethodImplAttribute(MethodImplOptions.InternalCall)]
647 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
652 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
653 InterfaceMapping res;
654 if (interfaceType == null)
655 throw new ArgumentNullException ("interfaceType");
656 if (!interfaceType.IsInterface)
657 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
659 throw new ArgumentException ("'this' type cannot be an interface itself");
660 res.TargetType = this;
661 res.InterfaceType = interfaceType;
662 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
663 if (res.TargetMethods == null)
664 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
669 public abstract Type[] GetInterfaces ();
671 public virtual bool IsAssignableFrom (Type c)
679 if (c is TypeBuilder)
680 return ((TypeBuilder)c).IsAssignableTo (this);
682 /* Handle user defined type classes */
684 Type systemType = UnderlyingSystemType;
685 if (!systemType.IsSystemType)
687 return systemType.IsAssignableFrom (c);
690 if (!c.IsSystemType) {
691 Type underlyingType = c.UnderlyingSystemType;
692 if (!underlyingType.IsSystemType)
694 return IsAssignableFrom (underlyingType);
697 return type_is_assignable_from (this, c);
700 [MethodImplAttribute(MethodImplOptions.InternalCall)]
701 public extern virtual bool IsInstanceOfType (object o);
703 public virtual int GetArrayRank ()
705 throw new NotSupportedException (); // according to MSDN
708 public abstract Type GetElementType ();
710 public EventInfo GetEvent (string name)
712 return GetEvent (name, DefaultBindingFlags);
715 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
717 public virtual EventInfo[] GetEvents ()
719 return GetEvents (DefaultBindingFlags);
722 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
724 public FieldInfo GetField( string name)
726 return GetField (name, DefaultBindingFlags);
729 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
731 public FieldInfo[] GetFields ()
733 return GetFields (DefaultBindingFlags);
736 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
738 public override int GetHashCode()
740 Type t = UnderlyingSystemType;
741 if (t != null && t != this)
742 return t.GetHashCode ();
743 return (int)_impl.Value;
746 public MemberInfo[] GetMember (string name)
748 return GetMember (name, DefaultBindingFlags);
751 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
753 return GetMember (name, MemberTypes.All, bindingAttr);
756 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
758 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
759 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
761 return FindMembers (type, bindingAttr, FilterName, name);
764 public MemberInfo[] GetMembers ()
766 return GetMembers (DefaultBindingFlags);
769 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
771 public MethodInfo GetMethod (string name)
774 throw new ArgumentNullException ("name");
775 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
778 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
781 throw new ArgumentNullException ("name");
783 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
786 public MethodInfo GetMethod (string name, Type[] types)
788 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
791 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
793 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
796 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
797 Type[] types, ParameterModifier[] modifiers)
799 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
802 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
803 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
806 throw new ArgumentNullException ("name");
808 throw new ArgumentNullException ("types");
810 for (int i = 0; i < types.Length; i++)
811 if (types[i] == null)
812 throw new ArgumentNullException ("types");
814 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
817 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
818 CallingConventions callConvention, Type[] types,
819 ParameterModifier[] modifiers);
821 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
822 CallingConventions callConvention, Type[] types,
823 ParameterModifier[] modifiers)
825 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
828 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
830 throw new System.InvalidOperationException ("can only be called in generic type");
833 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
835 throw new System.InvalidOperationException ("can only be called in generic type");
838 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
840 throw new System.InvalidOperationException ("can only be called in generic type");
844 public MethodInfo[] GetMethods ()
846 return GetMethods (DefaultBindingFlags);
849 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
851 public Type GetNestedType (string name)
853 return GetNestedType (name, DefaultBindingFlags);
856 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
858 public Type[] GetNestedTypes ()
860 return GetNestedTypes (DefaultBindingFlags);
863 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
866 public PropertyInfo[] GetProperties ()
868 return GetProperties (DefaultBindingFlags);
871 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
874 public PropertyInfo GetProperty (string name)
877 throw new ArgumentNullException ("name");
879 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
882 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
885 throw new ArgumentNullException ("name");
886 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
889 public PropertyInfo GetProperty (string name, Type returnType)
892 throw new ArgumentNullException ("name");
893 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
896 public PropertyInfo GetProperty (string name, Type[] types)
898 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
901 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
903 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
906 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
908 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
911 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
912 Type[] types, ParameterModifier[] modifiers)
915 throw new ArgumentNullException ("name");
917 throw new ArgumentNullException ("types");
919 foreach (Type t in types) {
921 throw new ArgumentNullException ("types");
924 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
927 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
928 Type returnType, Type[] types, ParameterModifier[] modifiers);
930 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
931 Type returnType, Type[] types, ParameterModifier[] modifiers)
933 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
936 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
938 CallingConventions callConvention,
940 ParameterModifier[] modifiers);
942 protected abstract TypeAttributes GetAttributeFlagsImpl ();
943 protected abstract bool HasElementTypeImpl ();
944 protected abstract bool IsArrayImpl ();
945 protected abstract bool IsByRefImpl ();
946 protected abstract bool IsCOMObjectImpl ();
947 protected abstract bool IsPointerImpl ();
948 protected abstract bool IsPrimitiveImpl ();
950 [MethodImplAttribute(MethodImplOptions.InternalCall)]
951 internal static extern bool IsArrayImpl (Type type);
953 protected virtual bool IsValueTypeImpl ()
955 if (this == typeof (ValueType) || this == typeof (Enum))
958 return IsSubclassOf (typeof (ValueType));
961 protected virtual bool IsContextfulImpl ()
963 return typeof (ContextBoundObject).IsAssignableFrom (this);
966 protected virtual bool IsMarshalByRefImpl ()
968 return typeof (MarshalByRefObject).IsAssignableFrom (this);
974 public ConstructorInfo GetConstructor (Type[] types)
976 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
982 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
983 Type[] types, ParameterModifier[] modifiers)
985 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
991 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
992 CallingConventions callConvention,
993 Type[] types, ParameterModifier[] modifiers)
996 throw new ArgumentNullException ("types");
998 foreach (Type t in types) {
1000 throw new ArgumentNullException ("types");
1003 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1009 public ConstructorInfo[] GetConstructors ()
1011 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1017 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1019 public virtual MemberInfo[] GetDefaultMembers ()
1021 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1022 if (att.Length == 0)
1023 return new MemberInfo [0];
1025 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1026 return (member != null) ? member : new MemberInfo [0];
1029 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1030 MemberFilter filter, object filterCriteria)
1032 MemberInfo[] result;
1033 ArrayList l = new ArrayList ();
1035 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1036 // this.FullName, this.GetType().FullName, this.obj_address());
1038 if ((memberType & MemberTypes.Constructor) != 0) {
1039 ConstructorInfo[] c = GetConstructors (bindingAttr);
1040 if (filter != null) {
1041 foreach (MemberInfo m in c) {
1042 if (filter (m, filterCriteria))
1049 if ((memberType & MemberTypes.Event) != 0) {
1050 EventInfo[] c = GetEvents (bindingAttr);
1051 if (filter != null) {
1052 foreach (MemberInfo m in c) {
1053 if (filter (m, filterCriteria))
1060 if ((memberType & MemberTypes.Field) != 0) {
1061 FieldInfo[] c = GetFields (bindingAttr);
1062 if (filter != null) {
1063 foreach (MemberInfo m in c) {
1064 if (filter (m, filterCriteria))
1071 if ((memberType & MemberTypes.Method) != 0) {
1072 MethodInfo[] c = GetMethods (bindingAttr);
1073 if (filter != null) {
1074 foreach (MemberInfo m in c) {
1075 if (filter (m, filterCriteria))
1082 if ((memberType & MemberTypes.Property) != 0) {
1084 int count = l.Count;
1086 if (filter != null) {
1088 while ((l.Count == count) && (ptype != null)) {
1089 c = ptype.GetProperties (bindingAttr);
1090 foreach (MemberInfo m in c) {
1091 if (filter (m, filterCriteria))
1094 ptype = ptype.BaseType;
1097 c = GetProperties (bindingAttr);
1101 if ((memberType & MemberTypes.NestedType) != 0) {
1102 Type[] c = GetNestedTypes (bindingAttr);
1103 if (filter != null) {
1104 foreach (MemberInfo m in c) {
1105 if (filter (m, filterCriteria)) {
1114 switch (memberType) {
1115 case MemberTypes.Constructor :
1116 result = new ConstructorInfo [l.Count];
1118 case MemberTypes.Event :
1119 result = new EventInfo [l.Count];
1121 case MemberTypes.Field :
1122 result = new FieldInfo [l.Count];
1124 case MemberTypes.Method :
1125 result = new MethodInfo [l.Count];
1127 case MemberTypes.NestedType :
1128 case MemberTypes.TypeInfo :
1129 result = new Type [l.Count];
1131 case MemberTypes.Property :
1132 result = new PropertyInfo [l.Count];
1135 result = new MemberInfo [l.Count];
1143 [DebuggerStepThrough]
1144 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1146 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1150 [DebuggerStepThrough]
1151 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1152 object target, object[] args, CultureInfo culture)
1154 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1157 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1158 Binder binder, object target, object[] args,
1159 ParameterModifier[] modifiers,
1160 CultureInfo culture, string[] namedParameters);
1162 public override string ToString()
1167 internal bool IsSystemType {
1169 return _impl.Value != IntPtr.Zero;
1173 #if NET_2_0 || BOOTSTRAP_NET_2_0
1174 public virtual Type[] GetGenericArguments ()
1176 throw new NotSupportedException ();
1179 public virtual bool ContainsGenericParameters {
1180 get { return false; }
1183 public virtual extern bool IsGenericTypeDefinition {
1184 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1188 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1189 internal extern Type GetGenericTypeDefinition_impl ();
1191 public virtual Type GetGenericTypeDefinition ()
1193 throw new NotSupportedException ("Derived classes must provide an implementation.");
1196 public virtual extern bool IsGenericType {
1197 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1201 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1202 static extern Type MakeGenericType (Type gt, Type [] types);
1204 public virtual Type MakeGenericType (params Type[] typeArguments)
1206 if (!IsGenericTypeDefinition)
1207 throw new InvalidOperationException ("not a generic type definition");
1208 if (typeArguments == null)
1209 throw new ArgumentNullException ("typeArguments");
1210 if (GetGenericArguments().Length != typeArguments.Length)
1211 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");
1213 Type[] systemTypes = new Type[typeArguments.Length];
1214 for (int i = 0; i < typeArguments.Length; ++i) {
1215 Type t = typeArguments [i];
1217 throw new ArgumentNullException ("typeArguments");
1218 if (!(t is EnumBuilder || t is TypeBuilder))
1219 t = t.UnderlyingSystemType;
1220 if (t == null || !t.IsSystemType)
1221 throw new ArgumentNullException ("typeArguments");
1222 systemTypes [i] = t;
1225 Type res = MakeGenericType (this, systemTypes);
1227 throw new TypeLoadException ();
1231 public virtual bool IsGenericParameter {
1237 public bool IsNested {
1239 return DeclaringType != null;
1243 public bool IsVisible {
1246 return DeclaringType.IsVisible;
1252 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1253 extern int GetGenericParameterPosition ();
1255 public virtual int GenericParameterPosition {
1257 int res = GetGenericParameterPosition ();
1259 throw new InvalidOperationException ();
1264 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1265 extern GenericParameterAttributes GetGenericParameterAttributes ();
1267 public virtual GenericParameterAttributes GenericParameterAttributes {
1269 if (!IsGenericParameter)
1270 throw new InvalidOperationException ();
1272 return GetGenericParameterAttributes ();
1276 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1277 extern Type[] GetGenericParameterConstraints_impl ();
1279 public virtual Type[] GetGenericParameterConstraints ()
1281 if (!IsGenericParameter)
1282 throw new InvalidOperationException ();
1284 return GetGenericParameterConstraints_impl ();
1287 public virtual MethodBase DeclaringMethod {
1293 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1294 extern Type make_array_type (int rank);
1296 public virtual Type MakeArrayType ()
1298 return MakeArrayType (1);
1301 public virtual Type MakeArrayType (int rank)
1304 throw new IndexOutOfRangeException ();
1305 return make_array_type (rank);
1308 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1309 extern Type make_byref_type ();
1311 public virtual Type MakeByRefType ()
1313 return make_byref_type ();
1316 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1317 public extern virtual Type MakePointerType ();
1319 public static Type ReflectionOnlyGetType (string typeName,
1320 bool throwIfNotFound,
1323 if (typeName == null)
1324 throw new ArgumentNullException ("typeName");
1325 int idx = typeName.IndexOf (',');
1326 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1327 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1328 string an = typeName.Substring (idx + 1);
1331 a = Assembly.ReflectionOnlyLoad (an);
1333 if (throwIfNotFound)
1337 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1340 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1341 extern void GetPacking (out int packing, out int size);
1343 public virtual StructLayoutAttribute StructLayoutAttribute {
1347 if (IsLayoutSequential)
1348 kind = LayoutKind.Sequential;
1349 else if (IsExplicitLayout)
1350 kind = LayoutKind.Explicit;
1352 kind = LayoutKind.Auto;
1354 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1357 attr.CharSet = CharSet.Unicode;
1358 else if (IsAnsiClass)
1359 attr.CharSet = CharSet.Ansi;
1361 attr.CharSet = CharSet.Auto;
1363 if (kind != LayoutKind.Auto)
1364 GetPacking (out attr.Pack, out attr.Size);
1370 internal object[] GetPseudoCustomAttributes ()
1374 /* IsSerializable returns true for delegates/enums as well */
1375 if ((Attributes & TypeAttributes.Serializable) != 0)
1377 if ((Attributes & TypeAttributes.Import) != 0)
1382 object[] attrs = new object [count];
1385 if ((Attributes & TypeAttributes.Serializable) != 0)
1386 attrs [count ++] = new SerializableAttribute ();
1387 if ((Attributes & TypeAttributes.Import) != 0)
1388 attrs [count ++] = new ComImportAttribute ();
1396 * Return whenever this object is an instance of a user defined subclass
1397 * of System.Type or an instance of TypeDelegator.
1399 internal bool IsUserType {
1402 * subclasses cannot modify _impl so if it is zero, it means the
1403 * type is not created by the runtime.
1405 return _impl.Value == IntPtr.Zero;
1409 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1411 throw new NotImplementedException ();
1414 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1416 throw new NotImplementedException ();
1419 void _Type.GetTypeInfoCount (out uint pcTInfo)
1421 throw new NotImplementedException ();
1424 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1426 throw new NotImplementedException ();