* roottypes.cs: Rename from tree.cs.
[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                         if (type == null)
472                                 /* MS.NET returns this */
473                                 return TypeCode.Empty;
474
475                         type = type.UnderlyingSystemType;
476
477                         if (!type.IsSystemType)
478                                 return Type.GetTypeCode (typeof (object));
479                         else
480                                 return GetTypeCodeInternal (type);
481                 }
482
483                 [MonoTODO]
484                 public static Type GetTypeFromCLSID (Guid clsid)
485                 {
486                         throw new NotImplementedException ();
487                 }
488
489                 [MonoTODO]
490                 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
491                 {
492                         throw new NotImplementedException ();
493                 }
494
495                 [MonoTODO]
496                 public static Type GetTypeFromCLSID (Guid clsid, string server)
497                 {
498                         throw new NotImplementedException ();
499                 }
500
501                 [MonoTODO]
502                 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
503                 {
504                         throw new NotImplementedException ();
505                 }
506
507                 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
508                 { 
509                         return internal_from_handle (handle.Value);
510                 }
511
512                 [MonoTODO]
513                 public static Type GetTypeFromProgID (string progID)
514                 {
515                         throw new NotImplementedException ();
516                 }
517
518                 [MonoTODO]
519                 public static Type GetTypeFromProgID (string progID, bool throwOnError)
520                 {
521                         throw new NotImplementedException ();
522                 }
523
524                 [MonoTODO]
525                 public static Type GetTypeFromProgID (string progID, string server)
526                 {
527                         throw new NotImplementedException ();
528                 }
529
530                 [MonoTODO]
531                 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
532                 {
533                         throw new NotImplementedException ();
534                 }
535
536                 public static RuntimeTypeHandle GetTypeHandle (object o)
537                 {
538                         return o.GetType().TypeHandle;
539                 }
540
541                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
542                 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
543
544                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
545                 internal static extern bool type_is_assignable_from (Type a, Type b);
546
547                 internal static bool is_subtype_of (Type a, Type b, bool check_interfaces)
548                 {
549                         a = a.UnderlyingSystemType;
550                         b = b.UnderlyingSystemType;
551
552                         if (!a.IsSystemType || !b.IsSystemType)
553                                 return false;
554                         else
555                                 return type_is_subtype_of (a, b, check_interfaces);
556                 }
557
558                 public new Type GetType ()
559                 {
560                         return base.GetType ();
561                 }
562
563 #if NET_2_0
564                 [ComVisible (true)]
565 #endif
566                 public virtual bool IsSubclassOf (Type c)
567                 {
568                         if (c == null)
569                                 return false;
570
571                         return (this != c) && is_subtype_of (this, c, false);
572                 }
573
574                 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
575                 {
576                         if (filter == null)
577                                 throw new ArgumentNullException ("filter");
578
579                         ArrayList ifaces = new ArrayList ();
580                         foreach (Type iface in GetInterfaces ()) {
581                                 if (filter (iface, filterCriteria))
582                                         ifaces.Add (iface);
583                         }
584
585                         return (Type []) ifaces.ToArray (typeof (Type));
586                 }
587                 
588                 public Type GetInterface (string name) {
589                         return GetInterface (name, false);
590                 }
591
592                 public abstract Type GetInterface (string name, bool ignoreCase);
593
594                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
595                 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
596
597 #if NET_2_0
598                 [ComVisible (true)]
599 #endif          
600                 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
601                         InterfaceMapping res;
602                         if (interfaceType == null)
603                                 throw new ArgumentNullException ("interfaceType");
604                         if (!interfaceType.IsInterface)
605                                 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
606                         res.TargetType = this;
607                         res.InterfaceType = interfaceType;
608                         GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
609                         if (res.TargetMethods == null)
610                                 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
611
612                         return res;
613                 }
614
615                 public abstract Type[] GetInterfaces ();
616
617                 public virtual bool IsAssignableFrom (Type c)
618                 {
619                         if (c == null)
620                                 return false;
621
622                         if (Equals (c))
623                                 return true;
624
625                         if (c is TypeBuilder)
626                                 return ((TypeBuilder)c).IsAssignableTo (this);
627
628                         /* Handle user defined type classes */
629                         if (!IsSystemType) {
630                                 Type systemType = UnderlyingSystemType;
631                                 if (!systemType.IsSystemType)
632                                         return false;
633                                 return systemType.IsAssignableFrom (c);
634                         }
635
636                         if (!c.IsSystemType) {
637                                 Type underlyingType = c.UnderlyingSystemType;
638                                 if (!underlyingType.IsSystemType)
639                                         return false;
640                                 return IsAssignableFrom (underlyingType);
641                         }
642
643                         return type_is_assignable_from (this, c);
644                 }
645
646                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
647                 public extern virtual bool IsInstanceOfType (object o);
648
649                 public virtual int GetArrayRank ()
650                 {
651                         throw new NotSupportedException ();     // according to MSDN
652                 }
653
654                 public abstract Type GetElementType ();
655
656                 public EventInfo GetEvent (string name)
657                 {
658                         return GetEvent (name, DefaultBindingFlags);
659                 }
660
661                 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
662
663                 public virtual EventInfo[] GetEvents ()
664                 {
665                         return GetEvents (DefaultBindingFlags);
666                 }
667
668                 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
669
670                 public FieldInfo GetField( string name)
671                 {
672                         return GetField (name, DefaultBindingFlags);
673                 }
674
675                 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
676
677                 public FieldInfo[] GetFields ()
678                 {
679                         return GetFields (DefaultBindingFlags);
680                 }
681
682                 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
683                 
684                 public override int GetHashCode()
685                 {
686                         return (int)_impl.Value;
687                 }
688
689                 public MemberInfo[] GetMember (string name)
690                 {
691                         return GetMember (name, DefaultBindingFlags);
692                 }
693                 
694                 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
695                 {
696                         return GetMember (name, MemberTypes.All, bindingAttr);
697                 }
698
699                 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
700                 {
701                         if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
702                                 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
703                         else
704                                 return FindMembers (type, bindingAttr, FilterName, name);
705                 }
706
707                 public MemberInfo[] GetMembers ()
708                 {
709                         return GetMembers (DefaultBindingFlags);
710                 }
711
712                 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
713
714                 public MethodInfo GetMethod (string name)
715                 {
716                         if (name == null)
717                                 throw new ArgumentNullException ("name");
718                         return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
719                 }
720
721                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
722                 {
723                         if (name == null)
724                                 throw new ArgumentNullException ("name");
725                         
726                         return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
727                 }
728                 
729                 public MethodInfo GetMethod (string name, Type[] types)
730                 {
731                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
732                 }
733
734                 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
735                 {
736                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
737                 }
738
739                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
740                                              Type[] types, ParameterModifier[] modifiers)
741                 {
742                         
743                         return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
744                 }
745
746                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
747                                              CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
748                 {
749                         if (name == null)
750                                 throw new ArgumentNullException ("name");
751                         if (types == null)
752                                 throw new ArgumentNullException ("types");
753
754                         for (int i = 0; i < types.Length; i++) 
755                                 if (types[i] == null)
756                                         throw new ArgumentNullException ("types");
757
758                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
759                 }
760
761                 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
762                                                              CallingConventions callConvention, Type[] types,
763                                                              ParameterModifier[] modifiers);
764
765                 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
766                                                                                                                         CallingConventions callConvention, Type[] types,
767                                                                                                                         ParameterModifier[] modifiers)
768                 {
769                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
770                 }
771
772                 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
773                 {
774                         throw new System.InvalidOperationException ("can only be called in generic type");
775                 }
776
777                 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
778                 {
779                         throw new System.InvalidOperationException ("can only be called in generic type");
780                 }
781
782                 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
783                 {
784                         throw new System.InvalidOperationException ("can only be called in generic type");
785                 }
786
787                 
788                 public MethodInfo[] GetMethods ()
789                 {
790                         return GetMethods (DefaultBindingFlags);
791                 }
792
793                 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
794
795                 public Type GetNestedType (string name)
796                 {
797                         return GetNestedType (name, DefaultBindingFlags);
798                 }
799
800                 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
801
802                 public Type[] GetNestedTypes ()
803                 {
804                         return GetNestedTypes (DefaultBindingFlags);
805                 }
806
807                 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
808
809
810                 public PropertyInfo[] GetProperties ()
811                 {
812                         return GetProperties (DefaultBindingFlags);
813                 }
814
815                 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
816
817
818                 public PropertyInfo GetProperty (string name)
819                 {
820                         if (name == null)
821                                 throw new ArgumentNullException ("name");
822
823                         return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
824                 }
825
826                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
827                 {
828                         if (name == null)
829                                 throw new ArgumentNullException ("name");
830                         return GetPropertyImpl (name, bindingAttr, null, null, null, null);
831                 }
832
833                 public PropertyInfo GetProperty (string name, Type returnType)
834                 {
835                         if (name == null)
836                                 throw new ArgumentNullException ("name");
837                         return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
838                 }
839
840                 public PropertyInfo GetProperty (string name, Type[] types)
841                 {
842                         return GetProperty (name, DefaultBindingFlags, null, null, types, null);
843                 }
844
845                 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
846                 {
847                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
848                 }
849
850                 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
851                 {
852                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
853                 }
854
855                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
856                                                  Type[] types, ParameterModifier[] modifiers)
857                 {
858                         if (name == null)
859                                 throw new ArgumentNullException ("name");
860                         if (types == null)
861                                 throw new ArgumentNullException ("types");
862
863                         foreach (Type t in types) {
864                                 if (t == null)
865                                         throw new ArgumentNullException ("types");
866                         }
867
868                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
869                 }
870
871                 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
872                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers);
873
874                 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
875                                                                                                            Type returnType, Type[] types, ParameterModifier[] modifiers)
876                 {
877                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
878                 }
879
880                 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
881                                                                        Binder binder,
882                                                                        CallingConventions callConvention,
883                                                                        Type[] types,
884                                                                        ParameterModifier[] modifiers);
885
886                 protected abstract TypeAttributes GetAttributeFlagsImpl ();
887                 protected abstract bool HasElementTypeImpl ();
888                 protected abstract bool IsArrayImpl ();
889                 protected abstract bool IsByRefImpl ();
890                 protected abstract bool IsCOMObjectImpl ();
891                 protected abstract bool IsPointerImpl ();
892                 protected abstract bool IsPrimitiveImpl ();
893                 
894                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
895                 internal static extern bool IsArrayImpl (Type type);
896
897                 protected virtual bool IsValueTypeImpl ()
898                 {
899                         if (this == typeof (Enum) || this == typeof (ValueType))
900                                 return false;
901
902                         return IsSubclassOf (typeof (ValueType));
903                 }
904                 
905                 protected virtual bool IsContextfulImpl ()
906                 {
907                         return typeof (ContextBoundObject).IsAssignableFrom (this);
908                 }
909
910                 protected virtual bool IsMarshalByRefImpl ()
911                 {
912                         return typeof (MarshalByRefObject).IsAssignableFrom (this);
913                 }
914
915 #if NET_2_0
916                 [ComVisible (true)]
917 #endif
918                 public ConstructorInfo GetConstructor (Type[] types)
919                 {
920                         return GetConstructor (DefaultBindingFlags, null, CallingConventions.Any, types, null);
921                 }
922
923 #if NET_2_0
924                 [ComVisible (true)]
925 #endif
926                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
927                                                        Type[] types, ParameterModifier[] modifiers)
928                 {
929                         return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
930                 }
931
932 #if NET_2_0
933                 [ComVisible (true)]
934 #endif
935                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
936                                                        CallingConventions callConvention,
937                                                        Type[] types, ParameterModifier[] modifiers)
938                 {
939                         if (types == null)
940                                 throw new ArgumentNullException ("types");
941
942                         foreach (Type t in types) {
943                                 if (t == null)
944                                         throw new ArgumentNullException ("types");
945                         }
946
947                         return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
948                 }
949
950 #if NET_2_0
951                 [ComVisible (true)]
952 #endif
953                 public ConstructorInfo[] GetConstructors ()
954                 {
955                         return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
956                 }
957
958 #if NET_2_0
959                 [ComVisible (true)]
960 #endif          
961                 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
962
963                 public virtual MemberInfo[] GetDefaultMembers ()
964                 {
965                         object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
966                         if (att.Length == 0)
967                                 return new MemberInfo [0];
968
969                         MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
970                         return (member != null) ? member : new MemberInfo [0];
971                 }
972
973                 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
974                                                          MemberFilter filter, object filterCriteria)
975                 {
976                         MemberInfo[] result;
977                         ArrayList l = new ArrayList ();
978
979                         // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
980                         // this.FullName, this.GetType().FullName, this.obj_address());
981
982                         if ((memberType & MemberTypes.Constructor) != 0) {
983                                 ConstructorInfo[] c = GetConstructors (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.Event) != 0) {
994                                 EventInfo[] c = GetEvents (bindingAttr);
995                                 if (filter != null) {
996                                         foreach (MemberInfo m in c) {
997                                                 if (filter (m, filterCriteria))
998                                                         l.Add (m);
999                                         }
1000                                 } else {
1001                                         l.AddRange (c);
1002                                 }
1003                         }
1004                         if ((memberType & MemberTypes.Field) != 0) {
1005                                 FieldInfo[] c = GetFields (bindingAttr);
1006                                 if (filter != null) {
1007                                         foreach (MemberInfo m in c) {
1008                                                 if (filter (m, filterCriteria))
1009                                                         l.Add (m);
1010                                         }
1011                                 } else {
1012                                         l.AddRange (c);
1013                                 }
1014                         }
1015                         if ((memberType & MemberTypes.Method) != 0) {
1016                                 MethodInfo[] c = GetMethods (bindingAttr);
1017                                 if (filter != null) {
1018                                         foreach (MemberInfo m in c) {
1019                                                 if (filter (m, filterCriteria))
1020                                                         l.Add (m);
1021                                         }
1022                                 } else {
1023                                         l.AddRange (c);
1024                                 }
1025                         }
1026                         if ((memberType & MemberTypes.Property) != 0) {
1027                                 PropertyInfo[] c;
1028                                 int count = l.Count;
1029                                 Type ptype;
1030                                 if (filter != null) {
1031                                         ptype = this;
1032                                         while ((l.Count == count) && (ptype != null)) {
1033                                                 c = ptype.GetProperties (bindingAttr);
1034                                                 foreach (MemberInfo m in c) {
1035                                                         if (filter (m, filterCriteria))
1036                                                                 l.Add (m);
1037                                                 }
1038                                                 ptype = ptype.BaseType;
1039                                         }
1040                                 } else {
1041                                         c = GetProperties (bindingAttr);
1042                                         l.AddRange (c);
1043                                 }
1044                         }
1045                         if ((memberType & MemberTypes.NestedType) != 0) {
1046                                 Type[] c = GetNestedTypes (bindingAttr);
1047                                 if (filter != null) {
1048                                         foreach (MemberInfo m in c) {
1049                                                 if (filter (m, filterCriteria)) {
1050                                                         l.Add (m);
1051                                                 }
1052                                         }
1053                                 } else {
1054                                         l.AddRange (c);
1055                                 }
1056                         }
1057                         result = new MemberInfo [l.Count];
1058                         l.CopyTo (result);
1059                         return result;
1060                 }
1061
1062                 [DebuggerHidden]
1063                 [DebuggerStepThrough] 
1064                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1065                 {
1066                         return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1067                 }
1068
1069                 [DebuggerHidden]
1070                 [DebuggerStepThrough] 
1071                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1072                                             object target, object[] args, CultureInfo culture)
1073                 {
1074                         return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1075                 }
1076
1077                 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1078                                                      Binder binder, object target, object[] args,
1079                                                      ParameterModifier[] modifiers,
1080                                                      CultureInfo culture, string[] namedParameters);
1081
1082                 public override string ToString()
1083                 {
1084                         return FullName;
1085                 }
1086
1087                 internal bool IsSystemType {
1088                         get {
1089                                 return _impl.Value != IntPtr.Zero;
1090                         }
1091                 }
1092
1093 #if NET_2_0 || BOOTSTRAP_NET_2_0
1094                 public virtual Type[] GetGenericArguments ()
1095                 {
1096                         throw new NotSupportedException ();
1097                 }
1098
1099                 public abstract bool ContainsGenericParameters {
1100                         get;
1101                 }
1102
1103                 public virtual extern bool IsGenericTypeDefinition {
1104                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1105                         get;
1106                 }
1107
1108                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1109                 extern Type GetGenericTypeDefinition_impl ();
1110
1111                 public virtual Type GetGenericTypeDefinition ()
1112                 {
1113                         Type res = GetGenericTypeDefinition_impl ();
1114                         if (res == null)
1115                                 throw new InvalidOperationException ();
1116
1117                         return res;
1118                 }
1119
1120                 public virtual extern bool IsGenericType {
1121                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1122                         get;
1123                 }
1124
1125                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1126                 static extern Type MakeGenericType (Type gt, Type [] types);
1127
1128                 public virtual Type MakeGenericType (params Type[] types)
1129                 {
1130                         if (types == null)
1131                                 throw new ArgumentNullException ("types");
1132                         foreach (Type t in types) {
1133                                 if (t == null)
1134                                         throw new ArgumentNullException ("types");
1135                         }
1136                         Type res = MakeGenericType (this, types);
1137                         if (res == null)
1138                                 throw new TypeLoadException ();
1139                         return res;
1140                 }
1141
1142                 public virtual bool IsGenericParameter {
1143                         get {
1144                                 return false;
1145                         }
1146                 }
1147
1148                 public bool IsNested {
1149                         get {
1150                                 return DeclaringType != null;
1151                         }
1152                 }
1153
1154                 public bool IsVisible {
1155                         get {
1156                                 if (IsNestedPublic)
1157                                         return DeclaringType.IsVisible;
1158
1159                                 return IsPublic;
1160                         }
1161                 }
1162
1163                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1164                 extern int GetGenericParameterPosition ();
1165                 
1166                 public virtual int GenericParameterPosition {
1167                         get {
1168                                 int res = GetGenericParameterPosition ();
1169                                 if (res < 0)
1170                                         throw new InvalidOperationException ();
1171                                 return res;
1172                         }
1173                 }
1174
1175                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1176                 extern GenericParameterAttributes GetGenericParameterAttributes ();
1177
1178                 public virtual GenericParameterAttributes GenericParameterAttributes {
1179                         get {
1180                                 if (!IsGenericParameter)
1181                                         throw new InvalidOperationException ();
1182
1183                                 return GetGenericParameterAttributes ();
1184                         }
1185                 }
1186
1187                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1188                 extern Type[] GetGenericParameterConstraints_impl ();
1189
1190                 public virtual Type[] GetGenericParameterConstraints ()
1191                 {
1192                         if (!IsGenericParameter)
1193                                 throw new InvalidOperationException ();
1194
1195                         return GetGenericParameterConstraints_impl ();
1196                 }
1197
1198                 public virtual MethodBase DeclaringMethod {
1199                         get {
1200                                 return null;
1201                         }
1202                 }
1203
1204                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1205                 extern Type make_array_type (int rank);
1206
1207                 public virtual Type MakeArrayType ()
1208                 {
1209                         return MakeArrayType (1);
1210                 }
1211
1212                 public virtual Type MakeArrayType (int rank)
1213                 {
1214                         if (rank < 1)
1215                                 throw new IndexOutOfRangeException ();
1216                         return make_array_type (rank);
1217                 }
1218
1219                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1220                 extern Type make_byref_type ();
1221
1222                 public virtual Type MakeByRefType ()
1223                 {
1224                         return make_byref_type ();
1225                 }
1226
1227                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1228                 public extern virtual Type MakePointerType ();
1229
1230                 [MonoTODO]
1231                 public static Type ReflectionOnlyGetType (string typeName, 
1232                                                                                                   bool throwIfNotFound, 
1233                                                                                                   bool ignoreCase)
1234                 {
1235                         throw new NotImplementedException ();
1236                 }
1237
1238                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1239                 extern void GetPacking (out int packing, out int size);         
1240
1241                 public virtual StructLayoutAttribute StructLayoutAttribute {
1242                         get {
1243                                 LayoutKind kind;
1244
1245                                 if (IsLayoutSequential)
1246                                         kind = LayoutKind.Sequential;
1247                                 else if (IsExplicitLayout)
1248                                         kind = LayoutKind.Explicit;
1249                                 else
1250                                         kind = LayoutKind.Auto;
1251
1252                                 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1253
1254                                 if (IsUnicodeClass)
1255                                         attr.CharSet = CharSet.Unicode;
1256                                 else if (IsAnsiClass)
1257                                         attr.CharSet = CharSet.Ansi;
1258                                 else
1259                                         attr.CharSet = CharSet.Auto;
1260
1261                                 if (kind != LayoutKind.Auto)
1262                                         GetPacking (out attr.Pack, out attr.Size);
1263
1264                                 return attr;
1265                         }
1266                 }
1267
1268                 internal object[] GetPseudoCustomAttributes ()
1269                 {
1270                         int count = 0;
1271
1272                         /* IsSerializable returns true for delegates/enums as well */
1273                         if ((Attributes & TypeAttributes.Serializable) != 0)
1274                                 count ++;
1275                         if ((Attributes & TypeAttributes.Import) != 0)
1276                                 count ++;
1277
1278                         if (count == 0)
1279                                 return null;
1280                         object[] attrs = new object [count];
1281                         count = 0;
1282
1283                         if ((Attributes & TypeAttributes.Serializable) != 0)
1284                                 attrs [count ++] = new SerializableAttribute ();
1285                         if ((Attributes & TypeAttributes.Import) != 0)
1286                                 attrs [count ++] = new ComImportAttribute ();
1287
1288                         return attrs;
1289                 }                       
1290
1291 #endif
1292
1293                 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1294                 {
1295                         throw new NotImplementedException ();
1296                 }
1297
1298                 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1299                 {
1300                         throw new NotImplementedException ();
1301                 }
1302
1303                 void _Type.GetTypeInfoCount (out uint pcTInfo)
1304                 {
1305                         throw new NotImplementedException ();
1306                 }
1307
1308                 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1309                 {
1310                         throw new NotImplementedException ();
1311                 }
1312         }
1313 }