235ad54a047972e3e02f8373339d2743ba6898be
[mono.git] / mcs / class / corlib / System / Type.cs
1 //
2 // System.Type.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
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;
40
41 namespace System {
42
43         [Serializable]
44         [ClassInterface (ClassInterfaceType.None)]
45 #if NET_2_0
46         [ComVisible (true)]
47         [ComDefaultInterface (typeof (_Type))]
48 #endif
49         public abstract class Type : MemberInfo, IReflect, _Type, _MemberInfo {
50                 
51                 internal RuntimeTypeHandle _impl;
52
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;
59
60                 internal const BindingFlags DefaultBindingFlags =
61                 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
62
63                 /* implementation of the delegates for MemberFilter */
64                 static bool FilterName_impl (MemberInfo m, object filterCriteria)
65                 {
66                         string name = (string) filterCriteria;
67                 if (name == null || name.Length == 0 )
68                                 return false; // because m.Name cannot be null or empty
69                                 
70                         if (name [name.Length-1] == '*')
71                                 return string.Compare (name, 0, m.Name, 0, name.Length-1, false, CultureInfo.InvariantCulture) == 0;
72
73                 return name.Equals (m.Name);                    
74                 }
75
76                 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
77                 {
78                         string name = (string) filterCriteria;
79                 if (name == null || name.Length == 0 )
80                                 return false; // because m.Name cannot be null or empty
81                                 
82                         if (name [name.Length-1] == '*')
83                                 return string.Compare (name, 0, m.Name, 0, name.Length-1, true, CultureInfo.InvariantCulture) == 0;
84
85                         return String.Compare (name, m.Name, true, CultureInfo.InvariantCulture) == 0;                          
86                 }
87
88                 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
89                 {
90                         int flags = ((IConvertible)filterCriteria).ToInt32 (null);
91                         if (m is MethodInfo)
92                                 return ((int)((MethodInfo)m).Attributes & flags) != 0;
93                         if (m is FieldInfo)
94                                 return ((int)((FieldInfo)m).Attributes & flags) != 0;
95                         if (m is PropertyInfo)
96                                 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
97                         if (m is EventInfo)
98                                 return ((int)((EventInfo)m).Attributes & flags) != 0;
99                         return false;
100                 }
101
102                 protected Type ()
103                 {
104                 }
105
106                 /// <summary>
107                 ///   The assembly where the type is defined.
108                 /// </summary>
109                 public abstract Assembly Assembly {
110                         get;
111                 }
112
113                 /// <summary>
114                 ///   Gets the fully qualified name for the type including the
115                 ///   assembly name where the type is defined.
116                 /// </summary>
117                 public abstract string AssemblyQualifiedName {
118                         get;
119                 }
120
121                 /// <summary>
122                 ///   Returns the Attributes associated with the type.
123                 /// </summary>
124                 public TypeAttributes Attributes {
125                         get {
126                                 return GetAttributeFlagsImpl ();
127                         }
128                 }
129
130                 /// <summary>
131                 ///   Returns the basetype for this type
132                 /// </summary>
133                 public abstract Type BaseType {
134                         get;
135                 }
136
137                 /// <summary>
138                 ///   Returns the class that declares the member.
139                 /// </summary>
140                 public override Type DeclaringType {
141                         get {
142                                 return null;
143                         }
144                 }
145
146                 /// <summary>
147                 ///
148                 /// </summary>
149                 public static Binder DefaultBinder {
150                         get {
151                                 return Binder.DefaultBinder;
152                         }
153                 }
154
155                 /// <summary>
156                 ///    The full name of the type including its namespace
157                 /// </summary>
158                 public abstract string FullName {
159                         get;
160                 }
161
162                 public abstract Guid GUID {
163                         get;
164                 }
165
166                 public bool HasElementType {
167                         get {
168                                 return HasElementTypeImpl ();
169                         }
170                 }
171
172                 public bool IsAbstract {
173                         get {
174                                 return (Attributes & TypeAttributes.Abstract) != 0;
175                         }
176                 }
177
178                 public bool IsAnsiClass {
179                         get {
180                                 return (Attributes & TypeAttributes.StringFormatMask)
181                                 == TypeAttributes.AnsiClass;
182                         }
183                 }
184
185                 public bool IsArray {
186                         get {
187                                 return IsArrayImpl ();
188                         }
189                 }
190
191                 public bool IsAutoClass {
192                         get {
193                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
194                         }
195                 }
196
197                 public bool IsAutoLayout {
198                         get {
199                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
200                         }
201                 }
202
203                 public bool IsByRef {
204                         get {
205                                 return IsByRefImpl ();
206                         }
207                 }
208
209                 public bool IsClass {
210                         get {
211                                 //
212                                 // This code used to probe for "this == typeof (System.Enum)", but in
213                                 // The .NET Framework 1.0, the above test return false
214                                 //
215                                 if (this == typeof (System.ValueType))
216                                         return true;
217                                 if (IsInterface)
218                                         return false;
219                                 return !is_subtype_of (this, typeof (System.ValueType), false);
220                         }
221                 }
222
223                 public bool IsCOMObject {
224                         get {
225                                 return IsCOMObjectImpl ();
226                         }
227                 }
228
229                 public bool IsContextful {
230                         get {
231                                 return IsContextfulImpl ();
232                         }
233                 }
234
235                 public bool IsEnum {
236                         get {
237                                 // This hack is needed because EnumBuilder's UnderlyingSystemType returns the enum's basetype
238                                 if (this is EnumBuilder)
239                                         return true;
240
241                                 return is_subtype_of (this, typeof (System.Enum), false) &&
242                                         this != typeof (System.Enum);
243                         }
244                 }
245
246                 public bool IsExplicitLayout {
247                         get {
248                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
249                         }
250                 }
251
252                 public bool IsImport {
253                         get {
254                                 return (Attributes & TypeAttributes.Import) != 0;
255                         }
256                 }
257
258                 public bool IsInterface {
259                         get {
260                                 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
261                         }
262                 }
263
264                 public bool IsLayoutSequential {
265                         get {
266                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
267                         }
268                 }
269
270                 public bool IsMarshalByRef {
271                         get {
272                                 return IsMarshalByRefImpl ();
273                         }
274                 }
275
276                 public bool IsNestedAssembly {
277                         get {
278                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
279                         }
280                 }
281
282                 public bool IsNestedFamANDAssem {
283                         get {
284                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
285                         }
286                 }
287
288                 public bool IsNestedFamily {
289                         get {
290                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
291                         }
292                 }
293
294                 public bool IsNestedFamORAssem {
295                         get {
296                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
297                         }
298                 }
299
300                 public bool IsNestedPrivate {
301                         get {
302                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
303                         }
304                 }
305
306                 public bool IsNestedPublic {
307                         get {
308                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
309                         }
310                 }
311
312                 public bool IsNotPublic {
313                         get {
314                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
315                         }
316                 }
317
318                 public bool IsPointer {
319                         get {
320                                 return IsPointerImpl ();
321                         }
322                 }
323
324                 public bool IsPrimitive {
325                         get {
326                                 return IsPrimitiveImpl ();
327                         }
328                 }
329
330                 public bool IsPublic {
331                         get {
332                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
333                         }
334                 }
335
336                 public bool IsSealed {
337                         get {
338                                 return (Attributes & TypeAttributes.Sealed) != 0;
339                         }
340                 }
341
342                 public bool IsSerializable {
343                         get {
344                                 // Enums and delegates are always serializable
345                                 return (Attributes & TypeAttributes.Serializable) != 0 || IsEnum || 
346                                         is_subtype_of (this, typeof (System.Delegate), false);
347                         }
348                 }
349
350                 public bool IsSpecialName {
351                         get {
352                                 return (Attributes & TypeAttributes.SpecialName) != 0;
353                         }
354                 }
355
356                 public bool IsUnicodeClass {
357                         get {
358                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
359                         }
360                 }
361
362                 public bool IsValueType {
363                         get {
364                                 return IsValueTypeImpl ();
365                         }
366                 }
367
368                 public override MemberTypes MemberType {
369                         get {return MemberTypes.TypeInfo;}
370                 }
371
372 #if NET_2_0 || BOOTSTRAP_NET_2_0
373                 override
374 #endif
375                 public abstract Module Module {get;}
376         
377                 public abstract string Namespace {get;}
378
379                 public override Type ReflectedType {
380                         get {
381                                 return null;
382                         }
383                 }
384
385                 public abstract RuntimeTypeHandle TypeHandle {get;}
386
387 #if NET_2_0
388                 [ComVisible (true)]
389 #endif
390                 public ConstructorInfo TypeInitializer {
391                         get {
392                                 return GetConstructorImpl (
393                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
394                                         null,
395                                         CallingConventions.Any,
396                                         EmptyTypes,
397                                         null);
398                         }
399                 }
400
401                 public abstract Type UnderlyingSystemType {get;}
402
403                 public override bool Equals (object o)
404                 {
405                         if (o == null)
406                                 return false;
407                         
408                         // TODO: return UnderlyingSystemType == o.UnderlyingSystemType;
409                         Type cmp = o as Type;
410                         if (cmp == null)
411                                 return false;
412                         return Equals (cmp);
413                 }
414
415                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
416                 public extern bool Equals (Type type);
417                 
418                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
419                 private static extern Type internal_from_handle (IntPtr handle);
420                 
421                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
422                 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
423
424                 public static Type GetType(string typeName)
425                 {
426                         if (typeName == null)
427                                 throw new ArgumentNullException ("typeName");
428
429                         return internal_from_name (typeName, false, false);
430                 }
431
432                 public static Type GetType(string typeName, bool throwOnError)
433                 {
434                         if (typeName == null)
435                                 throw new ArgumentNullException ("typeName");
436
437                         Type type = internal_from_name (typeName, throwOnError, false);
438                         if (throwOnError && type == null)
439                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
440
441                         return type;
442                 }
443
444                 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
445                 {
446                         if (typeName == null)
447                                 throw new ArgumentNullException ("typeName");
448
449                         Type t = internal_from_name (typeName, throwOnError, ignoreCase);
450                         if (throwOnError && t == null)
451                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
452
453                         return t;
454                 }
455
456                 public static Type[] GetTypeArray (object[] args) {
457                         if (args == null)
458                                 throw new ArgumentNullException ("args");
459
460                         Type[] ret;
461                         ret = new Type [args.Length];
462                         for (int i = 0; i < args.Length; ++i)
463                                 ret [i] = args[i].GetType ();
464                         return ret;
465                 }
466
467                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
468                 internal extern static TypeCode GetTypeCodeInternal (Type type);
469
470                 public static TypeCode GetTypeCode (Type type) {
471                         type = type.UnderlyingSystemType;
472
473                         if (!type.IsSystemType)
474                                 return Type.GetTypeCode (typeof (object));
475                         else
476                                 return GetTypeCodeInternal (type);
477                 }
478
479                 [MonoTODO]
480                 public static Type GetTypeFromCLSID (Guid clsid)
481                 {
482                         throw new NotImplementedException ();
483                 }
484
485                 [MonoTODO]
486                 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
487                 {
488                         throw new NotImplementedException ();
489                 }
490
491                 [MonoTODO]
492                 public static Type GetTypeFromCLSID (Guid clsid, string server)
493                 {
494                         throw new NotImplementedException ();
495                 }
496
497                 [MonoTODO]
498                 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
499                 {
500                         throw new NotImplementedException ();
501                 }
502
503                 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
504                 { 
505                         return internal_from_handle (handle.Value);
506                 }
507
508                 [MonoTODO]
509                 public static Type GetTypeFromProgID (string progID)
510                 {
511                         throw new NotImplementedException ();
512                 }
513
514                 [MonoTODO]
515                 public static Type GetTypeFromProgID (string progID, bool throwOnError)
516                 {
517                         throw new NotImplementedException ();
518                 }
519
520                 [MonoTODO]
521                 public static Type GetTypeFromProgID (string progID, string server)
522                 {
523                         throw new NotImplementedException ();
524                 }
525
526                 [MonoTODO]
527                 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
528                 {
529                         throw new NotImplementedException ();
530                 }
531
532                 public static RuntimeTypeHandle GetTypeHandle (object o)
533                 {
534                         return o.GetType().TypeHandle;
535                 }
536
537                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
538                 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
539
540                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
541                 internal static extern bool type_is_assignable_from (Type a, Type b);
542
543                 internal static bool is_subtype_of (Type a, Type b, bool check_interfaces)
544                 {
545                         a = a.UnderlyingSystemType;
546                         b = b.UnderlyingSystemType;
547
548                         if (!a.IsSystemType || !b.IsSystemType)
549                                 return false;
550                         else
551                                 return type_is_subtype_of (a, b, check_interfaces);
552                 }
553
554 #if NET_2_0
555                 [ComVisible (true)]
556 #endif
557                 public virtual bool IsSubclassOf (Type c)
558                 {
559                         if (c == null)
560                                 return false;
561
562                         return (this != c) && is_subtype_of (this, c, false);
563                 }
564
565                 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
566                 {
567                         if (filter == null)
568                                 throw new ArgumentNullException ("filter");
569
570                         ArrayList ifaces = new ArrayList ();
571                         foreach (Type iface in GetInterfaces ()) {
572                                 if (filter (iface, filterCriteria))
573                                         ifaces.Add (iface);
574                         }
575
576                         return (Type []) ifaces.ToArray (typeof (Type));
577                 }
578                 
579                 public Type GetInterface (string name) {
580                         return GetInterface (name, false);
581                 }
582
583                 public abstract Type GetInterface (string name, bool ignoreCase);
584
585                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
586                 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
587
588 #if NET_2_0
589                 [ComVisible (true)]
590 #endif          
591                 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
592                         InterfaceMapping res;
593                         if (interfaceType == null)
594                                 throw new ArgumentNullException ("interfaceType");
595                         if (!interfaceType.IsInterface)
596                                 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
597                         res.TargetType = this;
598                         res.InterfaceType = interfaceType;
599                         GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
600                         if (res.TargetMethods == null)
601                                 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
602
603                         return res;
604                 }
605
606                 public abstract Type[] GetInterfaces ();
607
608                 public virtual bool IsAssignableFrom (Type c)
609                 {
610                         if (c == null)
611                                 return false;
612
613                         if (Equals (c))
614                                 return true;
615
616                         if (c is TypeBuilder)
617                                 return ((TypeBuilder)c).IsAssignableTo (this);
618
619                         /* Handle user defined type classes */
620                         if (!IsSystemType) {
621                                 Type systemType = UnderlyingSystemType;
622                                 if (!systemType.IsSystemType)
623                                         return false;
624                                 return systemType.IsAssignableFrom (c);
625                         }
626
627                         if (!c.IsSystemType) {
628                                 Type underlyingType = c.UnderlyingSystemType;
629                                 if (!underlyingType.IsSystemType)
630                                         return false;
631                                 return IsAssignableFrom (underlyingType);
632                         }
633
634                         return type_is_assignable_from (this, c);
635                 }
636
637                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
638                 public extern virtual bool IsInstanceOfType (object o);
639
640                 public virtual int GetArrayRank ()
641                 {
642                         throw new NotSupportedException ();     // according to MSDN
643                 }
644
645                 public abstract Type GetElementType ();
646
647                 public EventInfo GetEvent (string name)
648                 {
649                         return GetEvent (name, DefaultBindingFlags);
650                 }
651
652                 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
653
654                 public virtual EventInfo[] GetEvents ()
655                 {
656                         return GetEvents (DefaultBindingFlags);
657                 }
658
659                 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
660
661                 public FieldInfo GetField( string name)
662                 {
663                         return GetField (name, DefaultBindingFlags);
664                 }
665
666                 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
667
668                 public FieldInfo[] GetFields ()
669                 {
670                         return GetFields (DefaultBindingFlags);
671                 }
672
673                 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
674                 
675                 public override int GetHashCode()
676                 {
677                         return (int)_impl.Value;
678                 }
679
680                 public MemberInfo[] GetMember (string name)
681                 {
682                         return GetMember (name, DefaultBindingFlags);
683                 }
684                 
685                 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
686                 {
687                         return GetMember (name, MemberTypes.All, bindingAttr);
688                 }
689
690                 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
691                 {
692                         if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
693                                 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
694                         else
695                                 return FindMembers (type, bindingAttr, FilterName, name);
696                 }
697
698                 public MemberInfo[] GetMembers ()
699                 {
700                         return GetMembers (DefaultBindingFlags);
701                 }
702
703                 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
704
705                 public MethodInfo GetMethod (string name)
706                 {
707                         if (name == null)
708                                 throw new ArgumentNullException ("name");
709                         return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
710                 }
711
712                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
713                 {
714                         if (name == null)
715                                 throw new ArgumentNullException ("name");
716                         
717                         return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
718                 }
719                 
720                 public MethodInfo GetMethod (string name, Type[] types)
721                 {
722                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
723                 }
724
725                 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
726                 {
727                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
728                 }
729
730                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
731                                              Type[] types, ParameterModifier[] modifiers)
732                 {
733                         
734                         return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
735                 }
736
737                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
738                                              CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
739                 {
740                         if (name == null)
741                                 throw new ArgumentNullException ("name");
742                         if (types == null)
743                                 throw new ArgumentNullException ("types");
744
745                         for (int i = 0; i < types.Length; i++) 
746                                 if (types[i] == null)
747                                         throw new ArgumentNullException ("types");
748
749                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
750                 }
751
752                 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
753                                                              CallingConventions callConvention, Type[] types,
754                                                              ParameterModifier[] modifiers);
755
756                 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
757                                                                                                                         CallingConventions callConvention, Type[] types,
758                                                                                                                         ParameterModifier[] modifiers)
759                 {
760                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
761                 }
762
763                 public MethodInfo[] GetMethods ()
764                 {
765                         return GetMethods (DefaultBindingFlags);
766                 }
767
768                 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
769
770                 public Type GetNestedType (string name)
771                 {
772                         return GetNestedType (name, DefaultBindingFlags);
773                 }
774
775                 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
776
777                 public Type[] GetNestedTypes ()
778                 {
779                         return GetNestedTypes (DefaultBindingFlags);
780                 }
781
782                 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
783
784
785                 public PropertyInfo[] GetProperties ()
786                 {
787                         return GetProperties (DefaultBindingFlags);
788                 }
789
790                 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
791
792
793                 public PropertyInfo GetProperty (string name)
794                 {
795                         if (name == null)
796                                 throw new ArgumentNullException ("name");
797
798                         return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
799                 }
800
801                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
802                 {
803                         if (name == null)
804                                 throw new ArgumentNullException ("name");
805                         return GetPropertyImpl (name, bindingAttr, null, null, null, null);
806                 }
807
808                 public PropertyInfo GetProperty (string name, Type returnType)
809                 {
810                         if (name == null)
811                                 throw new ArgumentNullException ("name");
812                         return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
813                 }
814
815                 public PropertyInfo GetProperty (string name, Type[] types)
816                 {
817                         return GetProperty (name, DefaultBindingFlags, null, null, types, null);
818                 }
819
820                 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
821                 {
822                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
823                 }
824
825                 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
826                 {
827                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
828                 }
829
830                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
831                                                  Type[] types, ParameterModifier[] modifiers)
832                 {
833                         if (name == null)
834                                 throw new ArgumentNullException ("name");
835                         if (types == null)
836                                 throw new ArgumentNullException ("types");
837
838                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
839                 }
840
841                 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
842                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers);
843
844                 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
845                                                                                                            Type returnType, Type[] types, ParameterModifier[] modifiers)
846                 {
847                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
848                 }
849
850                 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
851                                                                        Binder binder,
852                                                                        CallingConventions callConvention,
853                                                                        Type[] types,
854                                                                        ParameterModifier[] modifiers);
855
856                 protected abstract TypeAttributes GetAttributeFlagsImpl ();
857                 protected abstract bool HasElementTypeImpl ();
858                 protected abstract bool IsArrayImpl ();
859                 protected abstract bool IsByRefImpl ();
860                 protected abstract bool IsCOMObjectImpl ();
861                 protected abstract bool IsPointerImpl ();
862                 protected abstract bool IsPrimitiveImpl ();
863                 
864                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
865                 internal static extern bool IsArrayImpl (Type type);
866
867                 protected virtual bool IsValueTypeImpl ()
868                 {
869                         if (this == typeof (Enum) || this == typeof (ValueType))
870                                 return false;
871
872                         return IsSubclassOf (typeof (ValueType));
873                 }
874                 
875                 protected virtual bool IsContextfulImpl ()
876                 {
877                         return typeof (ContextBoundObject).IsAssignableFrom (this);
878                 }
879
880                 protected virtual bool IsMarshalByRefImpl ()
881                 {
882                         return typeof (MarshalByRefObject).IsAssignableFrom (this);
883                 }
884
885 #if NET_2_0
886                 [ComVisible (true)]
887 #endif
888                 public ConstructorInfo GetConstructor (Type[] types)
889                 {
890                         return GetConstructorImpl (
891                                 DefaultBindingFlags, null, CallingConventions.Any, types, null);
892                 }
893
894 #if NET_2_0
895                 [ComVisible (true)]
896 #endif
897                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
898                                                        Type[] types, ParameterModifier[] modifiers)
899                 {
900                         return GetConstructorImpl (
901                                 bindingAttr, binder, CallingConventions.Any, types, modifiers);
902                 }
903
904 #if NET_2_0
905                 [ComVisible (true)]
906 #endif
907                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
908                                                        CallingConventions callConvention,
909                                                        Type[] types, ParameterModifier[] modifiers)
910                 {
911                         if (types == null)
912                                 throw new ArgumentNullException ("types");
913
914                         return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
915                 }
916
917 #if NET_2_0
918                 [ComVisible (true)]
919 #endif
920                 public ConstructorInfo[] GetConstructors ()
921                 {
922                         return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
923                 }
924
925 #if NET_2_0
926                 [ComVisible (true)]
927 #endif          
928                 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
929
930                 public virtual MemberInfo[] GetDefaultMembers ()
931                 {
932                         object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
933                         if (att.Length == 0)
934                                 return new MemberInfo [0];
935
936                         MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
937                         return (member != null) ? member : new MemberInfo [0];
938                 }
939
940                 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
941                                                          MemberFilter filter, object filterCriteria)
942                 {
943                         MemberInfo[] result;
944                         ArrayList l = new ArrayList ();
945
946                         // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
947                         // this.FullName, this.GetType().FullName, this.obj_address());
948
949                         if ((memberType & MemberTypes.Constructor) != 0) {
950                                 ConstructorInfo[] c = GetConstructors (bindingAttr);
951                                 if (filter != null) {
952                                         foreach (MemberInfo m in c) {
953                                                 if (filter (m, filterCriteria))
954                                                         l.Add (m);
955                                         }
956                                 } else {
957                                         l.AddRange (c);
958                                 }
959                         }
960                         if ((memberType & MemberTypes.Event) != 0) {
961                                 EventInfo[] c = GetEvents (bindingAttr);
962                                 if (filter != null) {
963                                         foreach (MemberInfo m in c) {
964                                                 if (filter (m, filterCriteria))
965                                                         l.Add (m);
966                                         }
967                                 } else {
968                                         l.AddRange (c);
969                                 }
970                         }
971                         if ((memberType & MemberTypes.Field) != 0) {
972                                 FieldInfo[] c = GetFields (bindingAttr);
973                                 if (filter != null) {
974                                         foreach (MemberInfo m in c) {
975                                                 if (filter (m, filterCriteria))
976                                                         l.Add (m);
977                                         }
978                                 } else {
979                                         l.AddRange (c);
980                                 }
981                         }
982                         if ((memberType & MemberTypes.Method) != 0) {
983                                 MethodInfo[] c = GetMethods (bindingAttr);
984                                 if (filter != null) {
985                                         foreach (MemberInfo m in c) {
986                                                 if (filter (m, filterCriteria))
987                                                         l.Add (m);
988                                         }
989                                 } else {
990                                         l.AddRange (c);
991                                 }
992                         }
993                         if ((memberType & MemberTypes.Property) != 0) {
994                                 PropertyInfo[] c;
995                                 int count = l.Count;
996                                 Type ptype;
997                                 if (filter != null) {
998                                         ptype = this;
999                                         while ((l.Count == count) && (ptype != null)) {
1000                                                 c = ptype.GetProperties (bindingAttr);
1001                                                 foreach (MemberInfo m in c) {
1002                                                         if (filter (m, filterCriteria))
1003                                                                 l.Add (m);
1004                                                 }
1005                                                 ptype = ptype.BaseType;
1006                                         }
1007                                 } else {
1008                                         c = GetProperties (bindingAttr);
1009                                         l.AddRange (c);
1010                                 }
1011                         }
1012                         if ((memberType & MemberTypes.NestedType) != 0) {
1013                                 Type[] c = GetNestedTypes (bindingAttr);
1014                                 if (filter != null) {
1015                                         foreach (MemberInfo m in c) {
1016                                                 if (filter (m, filterCriteria)) {
1017                                                         l.Add (m);
1018                                                 }
1019                                         }
1020                                 } else {
1021                                         l.AddRange (c);
1022                                 }
1023                         }
1024                         result = new MemberInfo [l.Count];
1025                         l.CopyTo (result);
1026                         return result;
1027                 }
1028
1029                 [DebuggerHidden]
1030                 [DebuggerStepThrough] 
1031                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1032                 {
1033                         return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1034                 }
1035
1036                 [DebuggerHidden]
1037                 [DebuggerStepThrough] 
1038                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1039                                             object target, object[] args, CultureInfo culture)
1040                 {
1041                         return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1042                 }
1043
1044                 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1045                                                      Binder binder, object target, object[] args,
1046                                                      ParameterModifier[] modifiers,
1047                                                      CultureInfo culture, string[] namedParameters);
1048
1049                 public override string ToString()
1050                 {
1051                         return FullName;
1052                 }
1053
1054                 internal bool IsSystemType {
1055                         get {
1056                                 return _impl.Value != IntPtr.Zero;
1057                         }
1058                 }
1059
1060 #if NET_2_0 || BOOTSTRAP_NET_2_0
1061                 public abstract Type[] GetGenericArguments ();
1062
1063                 public abstract bool HasGenericArguments {
1064                         get;
1065                 }
1066
1067                 public abstract bool ContainsGenericParameters {
1068                         get;
1069                 }
1070
1071                 public extern bool IsGenericTypeDefinition {
1072                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1073                         get;
1074                 }
1075
1076                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1077                 extern Type GetGenericTypeDefinition_impl ();
1078
1079                 public virtual Type GetGenericTypeDefinition ()
1080                 {
1081                         Type res = GetGenericTypeDefinition_impl ();
1082                         if (res == null)
1083                                 throw new InvalidOperationException ();
1084
1085                         return res;
1086                 }
1087
1088                 public extern bool IsGenericInstance {
1089                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1090                         get;
1091                 }
1092
1093                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1094                 static extern Type BindGenericParameters (Type gt, Type [] types);
1095
1096 #if NET_2_0
1097                 [ComVisible (true)]
1098 #endif          
1099                 public Type BindGenericParameters (Type [] types)
1100                 {
1101                         if (types == null)
1102                                 throw new ArgumentNullException ("types");
1103                         foreach (Type t in types) {
1104                                 if (t == null)
1105                                         throw new ArgumentNullException ("types");
1106                         }
1107                         Type res = BindGenericParameters (this, types);
1108                         if (res == null)
1109                                 throw new TypeLoadException ();
1110                         return res;
1111                 }
1112
1113                 public abstract bool IsGenericParameter {
1114                         get;
1115                 }
1116
1117                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1118                 extern int GetGenericParameterPosition ();
1119                 
1120                 public virtual int GenericParameterPosition {
1121                         get {
1122                                 int res = GetGenericParameterPosition ();
1123                                 if (res < 0)
1124                                         throw new InvalidOperationException ();
1125                                 return res;
1126                         }
1127                 }
1128
1129                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1130                 extern GenericParameterAttributes GetGenericParameterAttributes ();
1131
1132                 public virtual GenericParameterAttributes GenericParameterAttributes {
1133                         get {
1134                                 if (!IsGenericParameter)
1135                                         throw new InvalidOperationException ();
1136
1137                                 return GetGenericParameterAttributes ();
1138                         }
1139                 }
1140
1141                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1142                 extern Type[] GetGenericParameterConstraints_impl ();
1143
1144                 public virtual Type[] GetGenericParameterConstraints ()
1145                 {
1146                         if (!IsGenericParameter)
1147                                 throw new InvalidOperationException ();
1148
1149                         return GetGenericParameterConstraints_impl ();
1150                 }
1151
1152                 public abstract MethodInfo DeclaringMethod {
1153                         get;
1154                 }
1155
1156                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1157                 extern Type make_array_type (int rank);
1158
1159                 public virtual Type MakeArrayType ()
1160                 {
1161                         return MakeArrayType (1);
1162                 }
1163
1164                 public virtual Type MakeArrayType (int rank)
1165                 {
1166                         if (rank < 1)
1167                                 throw new IndexOutOfRangeException ();
1168                         return make_array_type (rank);
1169                 }
1170
1171                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1172                 extern Type make_byref_type ();
1173
1174                 public virtual Type MakeByRefType ()
1175                 {
1176                         return make_byref_type ();
1177                 }
1178
1179                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1180                 public extern virtual Type MakePointerType ();
1181
1182                 [MonoTODO]
1183                 public static Type ReflectionOnlyGetType (string typeName, 
1184                                                                                                   bool throwIfNotFound, 
1185                                                                                                   bool ignoreCase)
1186                 {
1187                         throw new NotImplementedException ();
1188                 }
1189
1190                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1191                 extern void GetPacking (out int packing, out int size);         
1192
1193                 public virtual StructLayoutAttribute StructLayoutAttribute {
1194                         get {
1195                                 LayoutKind kind;
1196
1197                                 if (IsLayoutSequential)
1198                                         kind = LayoutKind.Sequential;
1199                                 else if (IsExplicitLayout)
1200                                         kind = LayoutKind.Explicit;
1201                                 else
1202                                         kind = LayoutKind.Auto;
1203
1204                                 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1205
1206                                 if (IsUnicodeClass)
1207                                         attr.CharSet = CharSet.Unicode;
1208                                 else if (IsAnsiClass)
1209                                         attr.CharSet = CharSet.Ansi;
1210                                 else
1211                                         attr.CharSet = CharSet.Auto;
1212
1213                                 if (kind != LayoutKind.Auto)
1214                                         GetPacking (out attr.Pack, out attr.Size);
1215
1216                                 return attr;
1217                         }
1218                 }
1219
1220                 internal object[] GetPseudoCustomAttributes () {
1221                         int count = 0;
1222
1223                         if (IsSerializable)
1224                                 count ++;
1225
1226                         if (count == 0)
1227                                 return null;
1228                         object[] attrs = new object [count];
1229                         count = 0;
1230
1231                         if (IsSerializable)
1232                                 attrs [count ++] = new SerializableAttribute ();
1233
1234                         return attrs;
1235                 }                       
1236
1237 #endif
1238         }
1239 }