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("Mono does not support COM")]
522 public static Type GetTypeFromCLSID (Guid clsid)
524 throw new NotImplementedException ();
527 [MonoTODO("Mono does not support COM")]
528 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
530 throw new NotImplementedException ();
533 [MonoTODO("Mono does not support COM")]
534 public static Type GetTypeFromCLSID (Guid clsid, string server)
536 throw new NotImplementedException ();
539 [MonoTODO("Mono does not support COM")]
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");
658 res.TargetType = this;
659 res.InterfaceType = interfaceType;
660 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
661 if (res.TargetMethods == null)
662 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
667 public abstract Type[] GetInterfaces ();
669 public virtual bool IsAssignableFrom (Type c)
677 if (c is TypeBuilder)
678 return ((TypeBuilder)c).IsAssignableTo (this);
680 /* Handle user defined type classes */
682 Type systemType = UnderlyingSystemType;
683 if (!systemType.IsSystemType)
685 return systemType.IsAssignableFrom (c);
688 if (!c.IsSystemType) {
689 Type underlyingType = c.UnderlyingSystemType;
690 if (!underlyingType.IsSystemType)
692 return IsAssignableFrom (underlyingType);
695 return type_is_assignable_from (this, c);
698 [MethodImplAttribute(MethodImplOptions.InternalCall)]
699 public extern virtual bool IsInstanceOfType (object o);
701 public virtual int GetArrayRank ()
703 throw new NotSupportedException (); // according to MSDN
706 public abstract Type GetElementType ();
708 public EventInfo GetEvent (string name)
710 return GetEvent (name, DefaultBindingFlags);
713 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
715 public virtual EventInfo[] GetEvents ()
717 return GetEvents (DefaultBindingFlags);
720 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
722 public FieldInfo GetField( string name)
724 return GetField (name, DefaultBindingFlags);
727 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
729 public FieldInfo[] GetFields ()
731 return GetFields (DefaultBindingFlags);
734 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
736 public override int GetHashCode()
738 Type t = UnderlyingSystemType;
739 if (t != null && t != this)
740 return t.GetHashCode ();
741 return (int)_impl.Value;
744 public MemberInfo[] GetMember (string name)
746 return GetMember (name, DefaultBindingFlags);
749 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
751 return GetMember (name, MemberTypes.All, bindingAttr);
754 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
756 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
757 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
759 return FindMembers (type, bindingAttr, FilterName, name);
762 public MemberInfo[] GetMembers ()
764 return GetMembers (DefaultBindingFlags);
767 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
769 public MethodInfo GetMethod (string name)
772 throw new ArgumentNullException ("name");
773 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
776 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
779 throw new ArgumentNullException ("name");
781 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
784 public MethodInfo GetMethod (string name, Type[] types)
786 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
789 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
791 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
794 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
795 Type[] types, ParameterModifier[] modifiers)
797 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
800 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
801 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
804 throw new ArgumentNullException ("name");
806 throw new ArgumentNullException ("types");
808 for (int i = 0; i < types.Length; i++)
809 if (types[i] == null)
810 throw new ArgumentNullException ("types");
812 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
815 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
816 CallingConventions callConvention, Type[] types,
817 ParameterModifier[] modifiers);
819 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
820 CallingConventions callConvention, Type[] types,
821 ParameterModifier[] modifiers)
823 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
826 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
828 throw new System.InvalidOperationException ("can only be called in generic type");
831 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
833 throw new System.InvalidOperationException ("can only be called in generic type");
836 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
838 throw new System.InvalidOperationException ("can only be called in generic type");
842 public MethodInfo[] GetMethods ()
844 return GetMethods (DefaultBindingFlags);
847 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
849 public Type GetNestedType (string name)
851 return GetNestedType (name, DefaultBindingFlags);
854 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
856 public Type[] GetNestedTypes ()
858 return GetNestedTypes (DefaultBindingFlags);
861 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
864 public PropertyInfo[] GetProperties ()
866 return GetProperties (DefaultBindingFlags);
869 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
872 public PropertyInfo GetProperty (string name)
875 throw new ArgumentNullException ("name");
877 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
880 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
883 throw new ArgumentNullException ("name");
884 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
887 public PropertyInfo GetProperty (string name, Type returnType)
890 throw new ArgumentNullException ("name");
891 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
894 public PropertyInfo GetProperty (string name, Type[] types)
896 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
899 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
901 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
904 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
906 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
909 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
910 Type[] types, ParameterModifier[] modifiers)
913 throw new ArgumentNullException ("name");
915 throw new ArgumentNullException ("types");
917 foreach (Type t in types) {
919 throw new ArgumentNullException ("types");
922 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
925 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
926 Type returnType, Type[] types, ParameterModifier[] modifiers);
928 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
929 Type returnType, Type[] types, ParameterModifier[] modifiers)
931 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
934 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
936 CallingConventions callConvention,
938 ParameterModifier[] modifiers);
940 protected abstract TypeAttributes GetAttributeFlagsImpl ();
941 protected abstract bool HasElementTypeImpl ();
942 protected abstract bool IsArrayImpl ();
943 protected abstract bool IsByRefImpl ();
944 protected abstract bool IsCOMObjectImpl ();
945 protected abstract bool IsPointerImpl ();
946 protected abstract bool IsPrimitiveImpl ();
948 [MethodImplAttribute(MethodImplOptions.InternalCall)]
949 internal static extern bool IsArrayImpl (Type type);
951 protected virtual bool IsValueTypeImpl ()
953 if (this == typeof (ValueType) || this == typeof (Enum))
956 return IsSubclassOf (typeof (ValueType));
959 protected virtual bool IsContextfulImpl ()
961 return typeof (ContextBoundObject).IsAssignableFrom (this);
964 protected virtual bool IsMarshalByRefImpl ()
966 return typeof (MarshalByRefObject).IsAssignableFrom (this);
972 public ConstructorInfo GetConstructor (Type[] types)
974 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
980 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
981 Type[] types, ParameterModifier[] modifiers)
983 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
989 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
990 CallingConventions callConvention,
991 Type[] types, ParameterModifier[] modifiers)
994 throw new ArgumentNullException ("types");
996 foreach (Type t in types) {
998 throw new ArgumentNullException ("types");
1001 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1007 public ConstructorInfo[] GetConstructors ()
1009 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1015 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1017 public virtual MemberInfo[] GetDefaultMembers ()
1019 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1020 if (att.Length == 0)
1021 return new MemberInfo [0];
1023 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1024 return (member != null) ? member : new MemberInfo [0];
1027 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1028 MemberFilter filter, object filterCriteria)
1030 MemberInfo[] result;
1031 ArrayList l = new ArrayList ();
1033 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1034 // this.FullName, this.GetType().FullName, this.obj_address());
1036 if ((memberType & MemberTypes.Constructor) != 0) {
1037 ConstructorInfo[] c = GetConstructors (bindingAttr);
1038 if (filter != null) {
1039 foreach (MemberInfo m in c) {
1040 if (filter (m, filterCriteria))
1047 if ((memberType & MemberTypes.Event) != 0) {
1048 EventInfo[] c = GetEvents (bindingAttr);
1049 if (filter != null) {
1050 foreach (MemberInfo m in c) {
1051 if (filter (m, filterCriteria))
1058 if ((memberType & MemberTypes.Field) != 0) {
1059 FieldInfo[] c = GetFields (bindingAttr);
1060 if (filter != null) {
1061 foreach (MemberInfo m in c) {
1062 if (filter (m, filterCriteria))
1069 if ((memberType & MemberTypes.Method) != 0) {
1070 MethodInfo[] c = GetMethods (bindingAttr);
1071 if (filter != null) {
1072 foreach (MemberInfo m in c) {
1073 if (filter (m, filterCriteria))
1080 if ((memberType & MemberTypes.Property) != 0) {
1082 int count = l.Count;
1084 if (filter != null) {
1086 while ((l.Count == count) && (ptype != null)) {
1087 c = ptype.GetProperties (bindingAttr);
1088 foreach (MemberInfo m in c) {
1089 if (filter (m, filterCriteria))
1092 ptype = ptype.BaseType;
1095 c = GetProperties (bindingAttr);
1099 if ((memberType & MemberTypes.NestedType) != 0) {
1100 Type[] c = GetNestedTypes (bindingAttr);
1101 if (filter != null) {
1102 foreach (MemberInfo m in c) {
1103 if (filter (m, filterCriteria)) {
1112 switch (memberType) {
1113 case MemberTypes.Constructor :
1114 result = new ConstructorInfo [l.Count];
1116 case MemberTypes.Event :
1117 result = new EventInfo [l.Count];
1119 case MemberTypes.Field :
1120 result = new FieldInfo [l.Count];
1122 case MemberTypes.Method :
1123 result = new MethodInfo [l.Count];
1125 case MemberTypes.NestedType :
1126 case MemberTypes.TypeInfo :
1127 result = new Type [l.Count];
1129 case MemberTypes.Property :
1130 result = new PropertyInfo [l.Count];
1133 result = new MemberInfo [l.Count];
1141 [DebuggerStepThrough]
1142 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1144 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1148 [DebuggerStepThrough]
1149 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1150 object target, object[] args, CultureInfo culture)
1152 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1155 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1156 Binder binder, object target, object[] args,
1157 ParameterModifier[] modifiers,
1158 CultureInfo culture, string[] namedParameters);
1160 public override string ToString()
1165 internal bool IsSystemType {
1167 return _impl.Value != IntPtr.Zero;
1171 #if NET_2_0 || BOOTSTRAP_NET_2_0
1172 public virtual Type[] GetGenericArguments ()
1174 throw new NotSupportedException ();
1177 public virtual bool ContainsGenericParameters {
1178 get { return false; }
1181 public virtual extern bool IsGenericTypeDefinition {
1182 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1186 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1187 extern Type GetGenericTypeDefinition_impl ();
1189 public virtual Type GetGenericTypeDefinition ()
1191 Type res = GetGenericTypeDefinition_impl ();
1193 throw new InvalidOperationException ();
1198 public virtual extern bool IsGenericType {
1199 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1203 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1204 static extern Type MakeGenericType (Type gt, Type [] types);
1206 public virtual Type MakeGenericType (params Type[] typeArguments)
1208 if (!IsGenericTypeDefinition)
1209 throw new InvalidOperationException ("not a generic type definition");
1210 if (typeArguments == null)
1211 throw new ArgumentNullException ("typeArguments");
1212 if (GetGenericArguments().Length != typeArguments.Length)
1213 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");
1215 Type[] systemTypes = new Type[typeArguments.Length];
1216 for (int i = 0; i < typeArguments.Length; ++i) {
1217 Type t = typeArguments [i];
1219 throw new ArgumentNullException ("typeArguments");
1220 if (!(t is EnumBuilder || t is TypeBuilder))
1221 t = t.UnderlyingSystemType;
1222 if (t == null || !t.IsSystemType)
1223 throw new ArgumentNullException ("typeArguments");
1224 systemTypes [i] = t;
1227 Type res = MakeGenericType (this, systemTypes);
1229 throw new TypeLoadException ();
1233 public virtual bool IsGenericParameter {
1239 public bool IsNested {
1241 return DeclaringType != null;
1245 public bool IsVisible {
1248 return DeclaringType.IsVisible;
1254 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1255 extern int GetGenericParameterPosition ();
1257 public virtual int GenericParameterPosition {
1259 int res = GetGenericParameterPosition ();
1261 throw new InvalidOperationException ();
1266 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1267 extern GenericParameterAttributes GetGenericParameterAttributes ();
1269 public virtual GenericParameterAttributes GenericParameterAttributes {
1271 if (!IsGenericParameter)
1272 throw new InvalidOperationException ();
1274 return GetGenericParameterAttributes ();
1278 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1279 extern Type[] GetGenericParameterConstraints_impl ();
1281 public virtual Type[] GetGenericParameterConstraints ()
1283 if (!IsGenericParameter)
1284 throw new InvalidOperationException ();
1286 return GetGenericParameterConstraints_impl ();
1289 public virtual MethodBase DeclaringMethod {
1295 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1296 extern Type make_array_type (int rank);
1298 public virtual Type MakeArrayType ()
1300 return MakeArrayType (1);
1303 public virtual Type MakeArrayType (int rank)
1306 throw new IndexOutOfRangeException ();
1307 return make_array_type (rank);
1310 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1311 extern Type make_byref_type ();
1313 public virtual Type MakeByRefType ()
1315 return make_byref_type ();
1318 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1319 public extern virtual Type MakePointerType ();
1321 public static Type ReflectionOnlyGetType (string typeName,
1322 bool throwIfNotFound,
1325 if (typeName == null)
1326 throw new ArgumentNullException ("typeName");
1327 int idx = typeName.IndexOf (',');
1328 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1329 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1330 string an = typeName.Substring (idx + 1);
1333 a = Assembly.ReflectionOnlyLoad (an);
1335 if (throwIfNotFound)
1339 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1342 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1343 extern void GetPacking (out int packing, out int size);
1345 public virtual StructLayoutAttribute StructLayoutAttribute {
1349 if (IsLayoutSequential)
1350 kind = LayoutKind.Sequential;
1351 else if (IsExplicitLayout)
1352 kind = LayoutKind.Explicit;
1354 kind = LayoutKind.Auto;
1356 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1359 attr.CharSet = CharSet.Unicode;
1360 else if (IsAnsiClass)
1361 attr.CharSet = CharSet.Ansi;
1363 attr.CharSet = CharSet.Auto;
1365 if (kind != LayoutKind.Auto)
1366 GetPacking (out attr.Pack, out attr.Size);
1372 internal object[] GetPseudoCustomAttributes ()
1376 /* IsSerializable returns true for delegates/enums as well */
1377 if ((Attributes & TypeAttributes.Serializable) != 0)
1379 if ((Attributes & TypeAttributes.Import) != 0)
1384 object[] attrs = new object [count];
1387 if ((Attributes & TypeAttributes.Serializable) != 0)
1388 attrs [count ++] = new SerializableAttribute ();
1389 if ((Attributes & TypeAttributes.Import) != 0)
1390 attrs [count ++] = new ComImportAttribute ();
1397 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1399 throw new NotImplementedException ();
1402 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1404 throw new NotImplementedException ();
1407 void _Type.GetTypeInfoCount (out uint pcTInfo)
1409 throw new NotImplementedException ();
1412 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1414 throw new NotImplementedException ();