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)
416 Type me = UnderlyingSystemType;
419 return me.EqualsInternal (o as Type);
422 public bool Equals (Type o) {
427 Type me = UnderlyingSystemType;
430 return me.EqualsInternal (o.UnderlyingSystemType);
434 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
435 public static bool operator == (Type left, Type right)
437 return Object.ReferenceEquals (left, right);
440 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
441 public static bool operator != (Type left, Type right)
443 return !Object.ReferenceEquals (left, right);
447 [MethodImplAttribute(MethodImplOptions.InternalCall)]
448 internal extern bool EqualsInternal (Type type);
450 [MethodImplAttribute(MethodImplOptions.InternalCall)]
451 private static extern Type internal_from_handle (IntPtr handle);
453 [MethodImplAttribute(MethodImplOptions.InternalCall)]
454 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
456 public static Type GetType(string typeName)
458 if (typeName == null)
459 throw new ArgumentNullException ("TypeName");
461 return internal_from_name (typeName, false, false);
464 public static Type GetType(string typeName, bool throwOnError)
466 if (typeName == null)
467 throw new ArgumentNullException ("TypeName");
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)
479 throw new ArgumentNullException ("TypeName");
481 Type t = internal_from_name (typeName, throwOnError, ignoreCase);
482 if (throwOnError && t == null)
483 throw new TypeLoadException ("Error loading '" + typeName + "'");
488 public static Type[] GetTypeArray (object[] args) {
490 throw new ArgumentNullException ("args");
493 ret = new Type [args.Length];
494 for (int i = 0; i < args.Length; ++i)
495 ret [i] = args[i].GetType ();
499 [MethodImplAttribute(MethodImplOptions.InternalCall)]
500 internal extern static TypeCode GetTypeCodeInternal (Type type);
502 public static TypeCode GetTypeCode (Type type) {
503 if (type is MonoType)
504 return GetTypeCodeInternal (type);
506 /* MS.NET returns this */
507 return TypeCode.Empty;
509 type = type.UnderlyingSystemType;
511 if (!type.IsSystemType)
512 return TypeCode.Object;
514 return GetTypeCodeInternal (type);
517 [MonoTODO("This operation is currently not supported by Mono")]
518 public static Type GetTypeFromCLSID (Guid clsid)
520 throw new NotImplementedException ();
523 [MonoTODO("This operation is currently not supported by Mono")]
524 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
526 throw new NotImplementedException ();
529 [MonoTODO("This operation is currently not supported by Mono")]
530 public static Type GetTypeFromCLSID (Guid clsid, string server)
532 throw new NotImplementedException ();
535 [MonoTODO("This operation is currently not supported by Mono")]
536 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
538 throw new NotImplementedException ();
541 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
543 if (handle.Value == IntPtr.Zero)
544 // This is not consistent with the other GetXXXFromHandle methods, but
545 // MS.NET seems to do this
548 return internal_from_handle (handle.Value);
551 [MonoTODO("Mono does not support COM")]
552 public static Type GetTypeFromProgID (string progID)
554 throw new NotImplementedException ();
557 [MonoTODO("Mono does not support COM")]
558 public static Type GetTypeFromProgID (string progID, bool throwOnError)
560 throw new NotImplementedException ();
563 [MonoTODO("Mono does not support COM")]
564 public static Type GetTypeFromProgID (string progID, string server)
566 throw new NotImplementedException ();
569 [MonoTODO("Mono does not support COM")]
570 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
572 throw new NotImplementedException ();
575 public static RuntimeTypeHandle GetTypeHandle (object o)
578 throw new ArgumentNullException ();
580 return o.GetType().TypeHandle;
583 [MethodImplAttribute(MethodImplOptions.InternalCall)]
584 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
586 [MethodImplAttribute(MethodImplOptions.InternalCall)]
587 internal static extern bool type_is_assignable_from (Type a, Type b);
589 public new Type GetType ()
591 return base.GetType ();
595 public virtual bool IsSubclassOf (Type c)
597 if (c == null || c == this)
600 // Fast check for system types
602 return c.IsSystemType && type_is_subtype_of (this, c, false);
604 // User defined types depend on this behavior
605 for (Type type = BaseType; type != null; type = type.BaseType)
612 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
615 throw new ArgumentNullException ("filter");
617 ArrayList ifaces = new ArrayList ();
618 foreach (Type iface in GetInterfaces ()) {
619 if (filter (iface, filterCriteria))
623 return (Type []) ifaces.ToArray (typeof (Type));
626 public Type GetInterface (string name) {
627 return GetInterface (name, false);
630 public abstract Type GetInterface (string name, bool ignoreCase);
632 [MethodImplAttribute(MethodImplOptions.InternalCall)]
633 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
636 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
638 throw new NotSupportedException ("Derived classes must provide an implementation.");
639 if (!interfaceType.IsSystemType)
640 throw new ArgumentException ("interfaceType", "Type is an user type");
641 InterfaceMapping res;
642 if (interfaceType == null)
643 throw new ArgumentNullException ("interfaceType");
644 if (!interfaceType.IsInterface)
645 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
647 throw new ArgumentException ("'this' type cannot be an interface itself");
648 res.TargetType = this;
649 res.InterfaceType = interfaceType;
650 GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
651 if (res.TargetMethods == null)
652 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
657 public abstract Type[] GetInterfaces ();
659 public virtual bool IsAssignableFrom (Type c)
667 if (c is TypeBuilder)
668 return ((TypeBuilder)c).IsAssignableTo (this);
670 /* Handle user defined type classes */
672 Type systemType = UnderlyingSystemType;
673 if (!systemType.IsSystemType)
676 Type other = c.UnderlyingSystemType;
677 if (!other.IsSystemType)
680 return systemType.IsAssignableFrom (other);
683 if (!c.IsSystemType) {
684 Type underlyingType = c.UnderlyingSystemType;
685 if (!underlyingType.IsSystemType)
687 return IsAssignableFrom (underlyingType);
690 return type_is_assignable_from (this, c);
693 [MethodImplAttribute(MethodImplOptions.InternalCall)]
694 extern static bool IsInstanceOfType (Type type, object o);
696 public virtual bool IsInstanceOfType (object o)
698 Type type = UnderlyingSystemType;
699 if (!type.IsSystemType)
701 return IsInstanceOfType (type, o);
704 public virtual int GetArrayRank ()
706 throw new NotSupportedException (); // according to MSDN
709 public abstract Type GetElementType ();
711 public EventInfo GetEvent (string name)
713 return GetEvent (name, DefaultBindingFlags);
716 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
718 public virtual EventInfo[] GetEvents ()
720 return GetEvents (DefaultBindingFlags);
723 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
725 public FieldInfo GetField( string name)
727 return GetField (name, DefaultBindingFlags);
730 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
732 public FieldInfo[] GetFields ()
734 return GetFields (DefaultBindingFlags);
737 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
739 public override int GetHashCode()
741 Type t = UnderlyingSystemType;
742 if (t != null && t != this)
743 return t.GetHashCode ();
744 return (int)_impl.Value;
747 public MemberInfo[] GetMember (string name)
749 return GetMember (name, MemberTypes.All, DefaultBindingFlags);
752 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
754 return GetMember (name, MemberTypes.All, bindingAttr);
757 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
760 throw new ArgumentNullException ("name");
761 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
762 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
764 return FindMembers (type, bindingAttr, FilterName, name);
767 public MemberInfo[] GetMembers ()
769 return GetMembers (DefaultBindingFlags);
772 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
774 public MethodInfo GetMethod (string name)
777 throw new ArgumentNullException ("name");
778 return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
781 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
784 throw new ArgumentNullException ("name");
786 return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
789 public MethodInfo GetMethod (string name, Type[] types)
791 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
794 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
796 return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
799 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
800 Type[] types, ParameterModifier[] modifiers)
802 return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
805 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
806 CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
809 throw new ArgumentNullException ("name");
811 throw new ArgumentNullException ("types");
813 for (int i = 0; i < types.Length; i++)
814 if (types[i] == null)
815 throw new ArgumentNullException ("types");
817 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
820 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
821 CallingConventions callConvention, Type[] types,
822 ParameterModifier[] modifiers);
824 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
825 CallingConventions callConvention, Type[] types,
826 ParameterModifier[] modifiers)
828 return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
831 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
833 throw new System.InvalidOperationException ("can only be called in generic type");
836 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
838 throw new System.InvalidOperationException ("can only be called in generic type");
841 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
843 throw new System.InvalidOperationException ("can only be called in generic type");
847 public MethodInfo[] GetMethods ()
849 return GetMethods (DefaultBindingFlags);
852 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
854 public Type GetNestedType (string name)
856 return GetNestedType (name, DefaultBindingFlags);
859 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
861 public Type[] GetNestedTypes ()
863 return GetNestedTypes (DefaultBindingFlags);
866 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
869 public PropertyInfo[] GetProperties ()
871 return GetProperties (DefaultBindingFlags);
874 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
877 public PropertyInfo GetProperty (string name)
880 throw new ArgumentNullException ("name");
882 return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
885 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
888 throw new ArgumentNullException ("name");
889 return GetPropertyImpl (name, bindingAttr, null, null, null, null);
892 public PropertyInfo GetProperty (string name, Type returnType)
895 throw new ArgumentNullException ("name");
896 return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
899 public PropertyInfo GetProperty (string name, Type[] types)
901 return GetProperty (name, DefaultBindingFlags, null, null, types, null);
904 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
906 return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
909 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
911 return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
914 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
915 Type[] types, ParameterModifier[] modifiers)
918 throw new ArgumentNullException ("name");
920 throw new ArgumentNullException ("types");
922 foreach (Type t in types) {
924 throw new ArgumentNullException ("types");
927 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
930 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
931 Type returnType, Type[] types, ParameterModifier[] modifiers);
933 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
934 Type returnType, Type[] types, ParameterModifier[] modifiers)
936 return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
939 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
941 CallingConventions callConvention,
943 ParameterModifier[] modifiers);
945 protected abstract TypeAttributes GetAttributeFlagsImpl ();
946 protected abstract bool HasElementTypeImpl ();
947 protected abstract bool IsArrayImpl ();
948 protected abstract bool IsByRefImpl ();
949 protected abstract bool IsCOMObjectImpl ();
950 protected abstract bool IsPointerImpl ();
951 protected abstract bool IsPrimitiveImpl ();
953 [MethodImplAttribute(MethodImplOptions.InternalCall)]
954 internal static extern bool IsArrayImpl (Type type);
956 protected virtual bool IsValueTypeImpl ()
958 if (this == typeof (ValueType) || this == typeof (Enum))
961 return IsSubclassOf (typeof (ValueType));
964 protected virtual bool IsContextfulImpl ()
966 return typeof (ContextBoundObject).IsAssignableFrom (this);
969 protected virtual bool IsMarshalByRefImpl ()
971 return typeof (MarshalByRefObject).IsAssignableFrom (this);
975 public ConstructorInfo GetConstructor (Type[] types)
977 return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
981 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
982 Type[] types, ParameterModifier[] modifiers)
984 return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
988 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
989 CallingConventions callConvention,
990 Type[] types, ParameterModifier[] modifiers)
993 throw new ArgumentNullException ("types");
995 foreach (Type t in types) {
997 throw new ArgumentNullException ("types");
1000 return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1004 public ConstructorInfo[] GetConstructors ()
1006 return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1010 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1012 public virtual MemberInfo[] GetDefaultMembers ()
1014 object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1015 if (att.Length == 0)
1016 return new MemberInfo [0];
1018 MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1019 return (member != null) ? member : new MemberInfo [0];
1022 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1023 MemberFilter filter, object filterCriteria)
1025 MemberInfo[] result;
1026 ArrayList l = new ArrayList ();
1028 // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1029 // this.FullName, this.GetType().FullName, this.obj_address());
1030 if ((memberType & MemberTypes.Method) != 0) {
1031 MethodInfo[] c = GetMethods (bindingAttr);
1032 if (filter != null) {
1033 foreach (MemberInfo m in c) {
1034 if (filter (m, filterCriteria))
1041 if ((memberType & MemberTypes.Constructor) != 0) {
1042 ConstructorInfo[] c = GetConstructors (bindingAttr);
1043 if (filter != null) {
1044 foreach (MemberInfo m in c) {
1045 if (filter (m, filterCriteria))
1052 if ((memberType & MemberTypes.Property) != 0) {
1054 int count = l.Count;
1056 if (filter != null) {
1058 while ((l.Count == count) && (ptype != null)) {
1059 c = ptype.GetProperties (bindingAttr);
1060 foreach (MemberInfo m in c) {
1061 if (filter (m, filterCriteria))
1064 ptype = ptype.BaseType;
1067 c = GetProperties (bindingAttr);
1071 if ((memberType & MemberTypes.Event) != 0) {
1072 EventInfo[] c = GetEvents (bindingAttr);
1073 if (filter != null) {
1074 foreach (MemberInfo m in c) {
1075 if (filter (m, filterCriteria))
1082 if ((memberType & MemberTypes.Field) != 0) {
1083 FieldInfo[] c = GetFields (bindingAttr);
1084 if (filter != null) {
1085 foreach (MemberInfo m in c) {
1086 if (filter (m, filterCriteria))
1093 if ((memberType & MemberTypes.NestedType) != 0) {
1094 Type[] c = GetNestedTypes (bindingAttr);
1095 if (filter != null) {
1096 foreach (MemberInfo m in c) {
1097 if (filter (m, filterCriteria)) {
1106 switch (memberType) {
1107 case MemberTypes.Constructor :
1108 result = new ConstructorInfo [l.Count];
1110 case MemberTypes.Event :
1111 result = new EventInfo [l.Count];
1113 case MemberTypes.Field :
1114 result = new FieldInfo [l.Count];
1116 case MemberTypes.Method :
1117 result = new MethodInfo [l.Count];
1119 case MemberTypes.NestedType :
1120 case MemberTypes.TypeInfo :
1121 result = new Type [l.Count];
1123 case MemberTypes.Property :
1124 result = new PropertyInfo [l.Count];
1127 result = new MemberInfo [l.Count];
1135 [DebuggerStepThrough]
1136 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1138 return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1142 [DebuggerStepThrough]
1143 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1144 object target, object[] args, CultureInfo culture)
1146 return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1149 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1150 Binder binder, object target, object[] args,
1151 ParameterModifier[] modifiers,
1152 CultureInfo culture, string[] namedParameters);
1154 public override string ToString()
1159 internal virtual bool IsCompilerContext {
1161 AssemblyBuilder builder = Assembly as AssemblyBuilder;
1162 return builder != null && builder.IsCompilerContext;
1166 internal bool IsSystemType {
1168 return _impl.Value != IntPtr.Zero;
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 internal extern Type GetGenericTypeDefinition_impl ();
1189 public virtual Type GetGenericTypeDefinition ()
1191 throw new NotSupportedException ("Derived classes must provide an implementation.");
1194 public virtual extern bool IsGenericType {
1195 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1199 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1200 static extern Type MakeGenericType (Type gt, Type [] types);
1202 static AssemblyBuilder PeelAssemblyBuilder (Type type)
1204 if (type.Assembly is AssemblyBuilder)
1205 return (AssemblyBuilder)type.Assembly;
1207 if (type.HasElementType)
1208 return PeelAssemblyBuilder (type.GetElementType ());
1210 if (!type.IsGenericType || type.IsGenericParameter || type.IsGenericTypeDefinition)
1213 foreach (Type arg in type.GetGenericArguments ()) {
1214 AssemblyBuilder ab = PeelAssemblyBuilder (arg);
1221 public virtual Type MakeGenericType (params Type[] typeArguments)
1224 throw new NotSupportedException ();
1225 if (!IsGenericTypeDefinition)
1226 throw new InvalidOperationException ("not a generic type definition");
1227 if (typeArguments == null)
1228 throw new ArgumentNullException ("typeArguments");
1229 if (GetGenericArguments().Length != typeArguments.Length)
1230 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");
1232 bool hasUserType = false;
1233 AssemblyBuilder compilerContext = null;
1235 Type[] systemTypes = new Type[typeArguments.Length];
1236 for (int i = 0; i < typeArguments.Length; ++i) {
1237 Type t = typeArguments [i];
1239 throw new ArgumentNullException ("typeArguments");
1241 if (!(t is MonoType))
1243 if (t.IsCompilerContext)
1244 compilerContext = PeelAssemblyBuilder (t);
1245 systemTypes [i] = t;
1249 if (compilerContext != null)
1250 return compilerContext.MakeGenericType (this, typeArguments);
1251 return new MonoGenericClass (this, typeArguments);
1254 Type res = MakeGenericType (this, systemTypes);
1256 throw new TypeLoadException ();
1260 public virtual bool IsGenericParameter {
1266 public bool IsNested {
1268 return DeclaringType != null;
1272 public bool IsVisible {
1275 return DeclaringType.IsVisible;
1281 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1282 extern int GetGenericParameterPosition ();
1284 public virtual int GenericParameterPosition {
1286 int res = GetGenericParameterPosition ();
1288 throw new InvalidOperationException ();
1293 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1294 extern GenericParameterAttributes GetGenericParameterAttributes ();
1296 public virtual GenericParameterAttributes GenericParameterAttributes {
1299 throw new NotSupportedException ("Derived classes must provide an implementation.");
1301 if (!IsGenericParameter)
1302 throw new InvalidOperationException ();
1304 return GetGenericParameterAttributes ();
1308 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1309 extern Type[] GetGenericParameterConstraints_impl ();
1311 public virtual Type[] GetGenericParameterConstraints ()
1314 throw new InvalidOperationException ();
1316 if (!IsGenericParameter)
1317 throw new InvalidOperationException ();
1319 return GetGenericParameterConstraints_impl ();
1322 public virtual MethodBase DeclaringMethod {
1328 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1329 extern Type make_array_type (int rank);
1331 public virtual Type MakeArrayType ()
1334 throw new NotSupportedException ("Derived classes must provide an implementation.");
1335 return make_array_type (0);
1338 public virtual Type MakeArrayType (int rank)
1341 throw new NotSupportedException ("Derived classes must provide an implementation.");
1342 if (rank < 1 || rank > 255)
1343 throw new IndexOutOfRangeException ();
1344 return make_array_type (rank);
1347 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1348 extern Type make_byref_type ();
1350 public virtual Type MakeByRefType ()
1353 throw new NotSupportedException ("Derived classes must provide an implementation.");
1355 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1356 return make_byref_type ();
1359 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1360 static extern Type MakePointerType (Type type);
1362 public virtual Type MakePointerType ()
1365 throw new NotSupportedException ("Derived classes must provide an implementation.");
1366 return MakePointerType (this);
1369 public static Type ReflectionOnlyGetType (string typeName,
1370 bool throwIfNotFound,
1373 if (typeName == null)
1374 throw new ArgumentNullException ("typeName");
1375 int idx = typeName.IndexOf (',');
1376 if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1377 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1378 string an = typeName.Substring (idx + 1);
1381 a = Assembly.ReflectionOnlyLoad (an);
1383 if (throwIfNotFound)
1387 return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1390 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1391 extern void GetPacking (out int packing, out int size);
1393 public virtual StructLayoutAttribute StructLayoutAttribute {
1397 if (IsLayoutSequential)
1398 kind = LayoutKind.Sequential;
1399 else if (IsExplicitLayout)
1400 kind = LayoutKind.Explicit;
1402 kind = LayoutKind.Auto;
1404 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1407 attr.CharSet = CharSet.Unicode;
1408 else if (IsAnsiClass)
1409 attr.CharSet = CharSet.Ansi;
1411 attr.CharSet = CharSet.Auto;
1413 if (kind != LayoutKind.Auto)
1414 GetPacking (out attr.Pack, out attr.Size);
1420 internal object[] GetPseudoCustomAttributes ()
1424 /* IsSerializable returns true for delegates/enums as well */
1425 if ((Attributes & TypeAttributes.Serializable) != 0)
1427 if ((Attributes & TypeAttributes.Import) != 0)
1432 object[] attrs = new object [count];
1435 if ((Attributes & TypeAttributes.Serializable) != 0)
1436 attrs [count ++] = new SerializableAttribute ();
1437 if ((Attributes & TypeAttributes.Import) != 0)
1438 attrs [count ++] = new ComImportAttribute ();
1444 #if NET_4_0 || BOOTSTRAP_NET_4_0
1445 public virtual bool IsEquivalentTo (Type other)
1447 return this == other;
1452 * Return whenever this object is an instance of a user defined subclass
1453 * of System.Type or an instance of TypeDelegator.
1455 internal bool IsUserType {
1458 * subclasses cannot modify _impl so if it is zero, it means the
1459 * type is not created by the runtime.
1461 return _impl.Value == IntPtr.Zero &&
1462 (GetType ().Assembly != typeof (Type).Assembly || GetType () == typeof (TypeDelegator));
1466 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1468 throw new NotImplementedException ();
1471 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1473 throw new NotImplementedException ();
1476 void _Type.GetTypeInfoCount (out uint pcTInfo)
1478 throw new NotImplementedException ();
1481 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1483 throw new NotImplementedException ();