5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
9 // TODO: Mucho left to implement.
12 using System.Reflection;
13 using System.Collections;
14 using System.Runtime.CompilerServices;
15 using System.Globalization;
20 // FIXME: Implement the various IReflect dependencies
25 public abstract class Type : MemberInfo, IReflect {
27 internal RuntimeTypeHandle _impl;
29 public static readonly char Delimiter = '.';
30 public static readonly Type[] EmptyTypes = {};
31 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
32 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
33 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
34 public static readonly object Missing;
36 /* implementation of the delegates for MemberFilter */
37 static bool FilterName_impl (MemberInfo m, object filterCriteria) {
38 string name = (string) filterCriteria;
39 return name.Equals (m.Name);
42 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria) {
43 string name = (string) filterCriteria;
44 return String.Compare (name, m.Name, true) == 0;
48 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria) {
49 throw new NotImplementedException ("FilterAttribute_impl");
56 /// The assembly where the type is defined.
58 public abstract Assembly Assembly {
63 /// Gets the fully qualified name for the type including the
64 /// assembly name where the type is defined.
66 public abstract string AssemblyQualifiedName {
71 /// Returns the Attributes associated with the type.
73 public TypeAttributes Attributes {
75 return GetAttributeFlagsImpl ();
80 /// Returns the basetype for this type
82 public abstract Type BaseType {
87 /// Returns the class that declares the member.
90 public override Type DeclaringType {
92 throw new NotImplementedException ();
100 public static Binder DefaultBinder {
102 throw new NotImplementedException ();
107 /// The full name of the type including its namespace
109 public abstract string FullName {
113 public abstract Guid GUID {
118 public bool HasElementType {
119 get {return false;} // FIXME
122 public bool IsAbstract {
124 return (Attributes & TypeAttributes.Abstract) != 0;
128 public bool IsAnsiClass {
130 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass;
134 public bool IsArray {
136 return IsArrayImpl ();
140 public bool IsAutoClass {
142 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
146 public bool IsAutoLayout {
148 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
152 public bool IsByRef {
154 return IsByRefImpl ();
158 public bool IsClass {
160 if (this == typeof (System.Enum) || this == typeof (System.ValueType))
164 return !type_is_subtype_of (this, typeof (System.ValueType), false);
168 public bool IsCOMObject {
170 return IsCOMObjectImpl ();
174 public bool IsContextful {
176 return IsContextfulImpl ();
182 return type_is_subtype_of (this, typeof (System.Enum), false) &&
183 this != typeof (System.Enum);
187 public bool IsExplicitLayout {
189 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
193 public bool IsImport {
195 return (Attributes & TypeAttributes.Import) != 0;
199 public bool IsInterface {
201 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
205 public bool IsLayoutSequential {
207 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
211 public bool IsMarshalByRef {
213 return IsMarshalByRefImpl ();
217 public bool IsNestedAssembly {
219 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
223 public bool IsNestedFamANDAssem {
225 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
229 public bool IsNestedFamily {
231 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
235 public bool IsNestedFamORAssem {
237 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
241 public bool IsNestedPrivate {
243 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
247 public bool IsNestedPublic {
249 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
253 public bool IsNotPublic {
259 public bool IsPointer {
261 return IsPointerImpl ();
265 public bool IsPrimitive {
267 return IsPrimitiveImpl ();
272 public bool IsPublic {
274 // FIXME: handle nestedpublic, too?
275 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
279 public bool IsSealed {
281 return (Attributes & TypeAttributes.Sealed) != 0;
285 public bool IsSerializable {
287 return (Attributes & TypeAttributes.Serializable) != 0;
291 public bool IsSpecialName {
293 return (Attributes & TypeAttributes.SpecialName) != 0;
297 public bool IsUnicodeClass {
299 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
303 public bool IsValueType {
305 return IsValueTypeImpl ();
310 public override MemberTypes MemberType {
311 get {return MemberTypes.TypeInfo;} // FIXME
314 public abstract Module Module {get;}
316 public abstract string Namespace {get;}
319 public override Type ReflectedType {
321 throw new NotImplementedException ();
325 public abstract RuntimeTypeHandle TypeHandle {get;}
328 public ConstructorInfo TypeInitializer {
330 throw new NotImplementedException ();
334 public abstract Type UnderlyingSystemType {get;}
336 public override bool Equals (object o) {
339 Type cmp = o as Type;
345 [MethodImplAttribute(MethodImplOptions.InternalCall)]
346 public extern bool Equals (Type type);
348 [MethodImplAttribute(MethodImplOptions.InternalCall)]
349 private static extern Type internal_from_handle (RuntimeTypeHandle handle);
350 [MethodImplAttribute(MethodImplOptions.InternalCall)]
351 private static extern Type internal_from_name (string name);
353 public static Type GetType(string typeName)
355 return internal_from_name (typeName);
358 public static Type GetType(string typeName, bool throwOnError)
360 // LAMESPEC: what kinds of errors cause exception to be thrown?
361 return internal_from_name (typeName);
365 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
367 throw new NotImplementedException ();
370 public static Type[] GetTypeArray (object[] args) {
373 ret = new Type [args.Length];
374 for (int i = 0; i < args.Length; ++i)
375 ret [i] = args[i].GetType ();
380 public static TypeCode GetTypeCode (Type type)
382 // FIXME -- this is most likely too simplistic for all cases;
385 Type ust = type.UnderlyingSystemType;
387 if (ust == typeof (Boolean))
388 return TypeCode.Boolean;
389 if (ust == typeof (Byte))
390 return TypeCode.Byte;
391 if (ust == typeof (Char))
392 return TypeCode.Char;
393 if (ust == typeof (DateTime))
394 return TypeCode.DateTime;
395 if (ust == typeof (DBNull))
396 return TypeCode.DBNull;
397 if (ust == typeof (Decimal))
398 return TypeCode.Decimal;
399 if (ust == typeof (Double))
400 return TypeCode.Double;
401 if (ust == typeof (Int16))
402 return TypeCode.Int16;
403 if (ust == typeof (Int32))
404 return TypeCode.Int32;
405 if (ust == typeof (Int64))
406 return TypeCode.Int64;
407 if (ust == typeof (SByte))
408 return TypeCode.SByte;
409 if (ust == typeof (Single))
410 return TypeCode.Single;
411 if (ust == typeof (String))
412 return TypeCode.String;
413 if (ust == typeof (UInt16))
414 return TypeCode.UInt16;
415 if (ust == typeof (UInt32))
416 return TypeCode.UInt32;
417 if (ust == typeof (UInt64))
418 return TypeCode.UInt64;
420 return TypeCode.Empty;
424 public static Type GetTypeFromCLSID (Guid clsid) {
425 throw new NotImplementedException ();
429 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError) {
430 throw new NotImplementedException ();
434 public static Type GetTypeFromCLSID (Guid clsid, string server) {
435 throw new NotImplementedException ();
439 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError) {
440 throw new NotImplementedException ();
443 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
445 return internal_from_handle (handle);
449 public static Type GetTypeFromProgID (string progID) {
450 throw new NotImplementedException ();
454 public static Type GetTypeFromProgID (string progID, bool throwOnError) {
455 throw new NotImplementedException ();
459 public static Type GetTypeFromProgID (string progID, string server) {
460 throw new NotImplementedException ();
464 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError) {
465 throw new NotImplementedException ();
468 public static RuntimeTypeHandle GetTypeHandle (object o) {
469 return o.GetType().TypeHandle;
472 [MethodImplAttribute(MethodImplOptions.InternalCall)]
473 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
475 public virtual bool IsSubclassOf (Type c)
477 return type_is_subtype_of (this, c, false);
481 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
484 throw new NotImplementedException ();
487 public Type GetInterface (string name) {
488 return GetInterface (name, false);
491 public abstract Type GetInterface (string name, bool ignoreCase);
493 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
494 throw new NotImplementedException ();
497 public abstract Type[] GetInterfaces ();
500 public virtual bool IsAssignableFrom (Type c)
503 return type_is_subtype_of (c, this, true);
506 public virtual bool IsInstanceOfType (object o) {
508 return o.GetType().IsSubclassOf (this);
514 public virtual int GetArrayRank ()
517 throw new NotImplementedException ();
520 public abstract Type GetElementType ();
523 public EventInfo GetEvent (string name) {
524 throw new NotImplementedException ();
527 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
529 public virtual EventInfo[] GetEvents () {
530 return GetEvents (BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance);
533 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
535 public FieldInfo GetField( string name) {
536 return GetField (name, BindingFlags.Public);
539 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
541 public FieldInfo[] GetFields ()
543 return GetFields (BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static);
546 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
548 public override int GetHashCode() {
549 return (int)_impl.Value;
552 public MemberInfo[] GetMember( string name) {
553 return GetMember (name, BindingFlags.Public);
557 public virtual MemberInfo[] GetMember( string name, BindingFlags bindingAttr) {
559 throw new NotImplementedException ();
563 public virtual MemberInfo[] GetMember( string name, MemberTypes type, BindingFlags bindingAttr) {
565 throw new NotImplementedException ();
568 public MemberInfo[] GetMembers() {
569 return GetMembers (BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance);
572 public abstract MemberInfo[] GetMembers( BindingFlags bindingAttr);
574 [MethodImplAttribute(MethodImplOptions.InternalCall)]
575 private static extern MethodInfo get_method (Type type, string name, Type[] types);
577 public MethodInfo GetMethod( string name) {
578 return GetMethod (name, BindingFlags.Public);
582 public MethodInfo GetMethod( string name, BindingFlags bindingAttr) {
584 throw new NotImplementedException ();
587 public MethodInfo GetMethod (string name, Type[] types)
589 return get_method (this, name, types);
593 public MethodInfo GetMethod( string name, Type[] types, ParameterModifier[] modifiers) {
595 throw new NotImplementedException ();
599 public MethodInfo GetMethod( string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) {
601 throw new NotImplementedException ();
605 public MethodInfo GetMethod( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
607 throw new NotImplementedException ();
610 protected abstract MethodInfo GetMethodImpl( string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers);
612 public MethodInfo[] GetMethods ()
614 return GetMethods (BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static);
617 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
619 public Type GetNestedType( string name) {
620 return GetNestedType (name, BindingFlags.Public);
623 public abstract Type GetNestedType( string name, BindingFlags bindingAttr);
625 public Type[] GetNestedTypes () {
626 return GetNestedTypes (BindingFlags.Public);
629 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
631 public PropertyInfo[] GetProperties ()
633 return GetProperties (BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance);
636 public abstract PropertyInfo[] GetProperties( BindingFlags bindingAttr);
638 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639 private static extern PropertyInfo get_property (Type type, string name, Type[] types);
641 public PropertyInfo GetProperty (string name)
643 return GetProperty (name, BindingFlags.Public);
647 public PropertyInfo GetProperty( string name, BindingFlags bindingAttr) {
649 throw new NotImplementedException ();
653 public PropertyInfo GetProperty( string name, Type returnType) {
655 throw new NotImplementedException ();
658 public PropertyInfo GetProperty (string name, Type[] types)
660 return get_property (this, name, types);
664 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
667 throw new NotImplementedException ();
671 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers) {
673 throw new NotImplementedException ();
677 public PropertyInfo GetProperty( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) {
679 throw new NotImplementedException ();
682 protected abstract PropertyInfo GetPropertyImpl( string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);
684 [MethodImplAttribute(MethodImplOptions.InternalCall)]
685 private static extern ConstructorInfo get_constructor (Type type, Type[] types);
687 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers);
689 protected abstract TypeAttributes GetAttributeFlagsImpl ();
690 protected abstract bool HasElementTypeImpl ();
691 protected abstract bool IsArrayImpl ();
692 protected abstract bool IsByRefImpl ();
693 protected abstract bool IsCOMObjectImpl ();
694 protected virtual bool IsContextfulImpl () {
695 return typeof (ContextBoundObject).IsAssignableFrom (this);
698 protected virtual bool IsMarshalByRefImpl () {
702 protected abstract bool IsPointerImpl ();
703 protected abstract bool IsPrimitiveImpl ();
704 protected abstract bool IsValueTypeImpl ();
706 public ConstructorInfo GetConstructor (Type[] types)
708 return get_constructor (this, types);
712 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) {
713 throw new NotImplementedException ();
717 public ConstructorInfo GetConstructor( BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) {
718 throw new NotImplementedException ();
721 public ConstructorInfo[] GetConstructors () {
722 return GetConstructors (BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance);
725 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
728 public virtual MemberInfo[] GetDefaultMembers () {
729 throw new NotImplementedException ();
732 public virtual MemberInfo[] FindMembers( MemberTypes memberType, BindingFlags bindingAttr,
733 MemberFilter filter, object filterCriteria) {
735 ArrayList l = new ArrayList ();
737 //Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}", this.FullName, this.GetType().FullName, this.obj_address());
739 if ((memberType & MemberTypes.Constructor) != 0) {
740 ConstructorInfo[] c = GetConstructors (bindingAttr);
741 if (filter != null) {
742 foreach (MemberInfo m in c) {
743 if (filter (m, filterCriteria))
750 if ((memberType & MemberTypes.Event) != 0) {
751 EventInfo[] c = GetEvents (bindingAttr);
752 if (filter != null) {
753 foreach (MemberInfo m in c) {
754 if (filter (m, filterCriteria))
761 if ((memberType & MemberTypes.Field) != 0) {
762 FieldInfo[] c = GetFields (bindingAttr);
763 if (filter != null) {
764 foreach (MemberInfo m in c) {
765 if (filter (m, filterCriteria))
772 if ((memberType & MemberTypes.Method) != 0) {
773 MethodInfo[] c = GetMethods (bindingAttr);
774 if (filter != null) {
775 foreach (MemberInfo m in c) {
776 if (filter (m, filterCriteria))
783 if ((memberType & MemberTypes.Property) != 0) {
787 if (filter != null) {
789 while ((l.Count == count) && (ptype != null)) {
790 c = ptype.GetProperties (bindingAttr);
791 foreach (MemberInfo m in c) {
792 if (filter (m, filterCriteria))
795 ptype = ptype.BaseType;
798 c = GetProperties (bindingAttr);
802 result = new MemberInfo [l.Count];
808 public object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args) {
814 public object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, CultureInfo culture) {
819 public abstract object InvokeMember( string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters);
821 public override string ToString()
823 string res = FullName;