2008-05-05 Zoltan Varga <vargaz@gmail.com>
[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                                 if (IsInterface)
212                                         return false;
213
214                                 return !IsSubclassOf (typeof (ValueType));
215                         }
216                 }
217
218                 public bool IsCOMObject {
219                         get {
220                                 return IsCOMObjectImpl ();
221                         }
222                 }
223
224                 public bool IsContextful {
225                         get {
226                                 return IsContextfulImpl ();
227                         }
228                 }
229
230                 public bool IsEnum {
231                         get {
232                                 return IsSubclassOf (typeof (Enum));
233                         }
234                 }
235
236                 public bool IsExplicitLayout {
237                         get {
238                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
239                         }
240                 }
241
242                 public bool IsImport {
243                         get {
244                                 return (Attributes & TypeAttributes.Import) != 0;
245                         }
246                 }
247
248                 public bool IsInterface {
249                         get {
250                                 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
251                         }
252                 }
253
254                 public bool IsLayoutSequential {
255                         get {
256                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
257                         }
258                 }
259
260                 public bool IsMarshalByRef {
261                         get {
262                                 return IsMarshalByRefImpl ();
263                         }
264                 }
265
266                 public bool IsNestedAssembly {
267                         get {
268                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
269                         }
270                 }
271
272                 public bool IsNestedFamANDAssem {
273                         get {
274                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
275                         }
276                 }
277
278                 public bool IsNestedFamily {
279                         get {
280                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
281                         }
282                 }
283
284                 public bool IsNestedFamORAssem {
285                         get {
286                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
287                         }
288                 }
289
290                 public bool IsNestedPrivate {
291                         get {
292                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
293                         }
294                 }
295
296                 public bool IsNestedPublic {
297                         get {
298                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
299                         }
300                 }
301
302                 public bool IsNotPublic {
303                         get {
304                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
305                         }
306                 }
307
308                 public bool IsPointer {
309                         get {
310                                 return IsPointerImpl ();
311                         }
312                 }
313
314                 public bool IsPrimitive {
315                         get {
316                                 return IsPrimitiveImpl ();
317                         }
318                 }
319
320                 public bool IsPublic {
321                         get {
322                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
323                         }
324                 }
325
326                 public bool IsSealed {
327                         get {
328                                 return (Attributes & TypeAttributes.Sealed) != 0;
329                         }
330                 }
331
332                 public bool IsSerializable {
333                         get {
334                                 if ((Attributes & TypeAttributes.Serializable) != 0)
335                                         return true;
336
337                                 // Enums and delegates are always serializable
338
339                                 Type type = UnderlyingSystemType;
340                                 if (type == null)
341                                         return false;
342
343                                 // Fast check for system types
344                                 if (type.IsSystemType)
345                                         return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
346
347                                 // User defined types depend on this behavior
348                                 do {
349                                         if ((type == typeof (Enum)) || (type == typeof (Delegate)))
350                                                 return true;
351
352                                         type = type.BaseType;
353                                 } while (type != null);
354
355                                 return false;
356                         }
357                 }
358
359                 public bool IsSpecialName {
360                         get {
361                                 return (Attributes & TypeAttributes.SpecialName) != 0;
362                         }
363                 }
364
365                 public bool IsUnicodeClass {
366                         get {
367                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
368                         }
369                 }
370
371                 public bool IsValueType {
372                         get {
373                                 return IsValueTypeImpl ();
374                         }
375                 }
376
377                 public override MemberTypes MemberType {
378                         get {return MemberTypes.TypeInfo;}
379                 }
380
381 #if NET_2_0 || BOOTSTRAP_NET_2_0
382                 override
383 #endif
384                 public abstract Module Module {get;}
385         
386                 public abstract string Namespace {get;}
387
388                 public override Type ReflectedType {
389                         get {
390                                 return null;
391                         }
392                 }
393
394 #if NET_2_0
395                 public virtual RuntimeTypeHandle TypeHandle {
396                         get { return default (RuntimeTypeHandle); }
397                 }
398 #else
399                 public abstract RuntimeTypeHandle TypeHandle {get;}
400 #endif
401
402 #if NET_2_0
403                 [ComVisible (true)]
404 #endif
405                 public ConstructorInfo TypeInitializer {
406                         get {
407                                 return GetConstructorImpl (
408                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
409                                         null,
410                                         CallingConventions.Any,
411                                         EmptyTypes,
412                                         null);
413                         }
414                 }
415
416                 /*
417                  * This has NOTHING to do with getting the base type of an enum. Use
418                  * Enum.GetUnderlyingType () for that.
419                  */
420                 public abstract Type UnderlyingSystemType {get;}
421
422                 public override bool Equals (object o)
423                 {
424                         if (o == null)
425                                 return false;
426                         
427                         Type cmp = o as Type;
428                         if (cmp == null)
429                                 return false;
430                         return Equals (cmp);
431                 }
432
433                 public bool Equals (Type type) {
434                         if (type == null)
435                                 return false;
436                         return UnderlyingSystemType.EqualsInternal (type.UnderlyingSystemType);
437                 }
438
439                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
440                 internal extern bool EqualsInternal (Type type);
441                 
442                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
443                 private static extern Type internal_from_handle (IntPtr handle);
444                 
445                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
446                 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
447
448                 public static Type GetType(string typeName)
449                 {
450                         if (typeName == null)
451                                 throw new ArgumentNullException ("typeName");
452
453                         return internal_from_name (typeName, false, false);
454                 }
455
456                 public static Type GetType(string typeName, bool throwOnError)
457                 {
458                         if (typeName == null)
459                                 throw new ArgumentNullException ("typeName");
460
461                         Type type = internal_from_name (typeName, throwOnError, false);
462                         if (throwOnError && type == null)
463                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
464
465                         return type;
466                 }
467
468                 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
469                 {
470                         if (typeName == null)
471                                 throw new ArgumentNullException ("typeName");
472
473                         Type t = internal_from_name (typeName, throwOnError, ignoreCase);
474                         if (throwOnError && t == null)
475                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
476
477                         return t;
478                 }
479
480                 public static Type[] GetTypeArray (object[] args) {
481                         if (args == null)
482                                 throw new ArgumentNullException ("args");
483
484                         Type[] ret;
485                         ret = new Type [args.Length];
486                         for (int i = 0; i < args.Length; ++i)
487                                 ret [i] = args[i].GetType ();
488                         return ret;
489                 }
490
491                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
492                 internal extern static TypeCode GetTypeCodeInternal (Type type);
493
494                 public static TypeCode GetTypeCode (Type type) {
495                         if (type is MonoType)
496                                 return GetTypeCodeInternal (type);
497                         if (type == null)
498                                 /* MS.NET returns this */
499                                 return TypeCode.Empty;
500
501                         type = type.UnderlyingSystemType;
502
503                         if (!type.IsSystemType)
504                                 return TypeCode.Object;
505                         else
506                                 return GetTypeCodeInternal (type);
507                 }
508
509                 [MonoTODO("Mono does not support COM")]
510                 public static Type GetTypeFromCLSID (Guid clsid)
511                 {
512                         throw new NotImplementedException ();
513                 }
514
515                 [MonoTODO("Mono does not support COM")]
516                 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
517                 {
518                         throw new NotImplementedException ();
519                 }
520
521                 [MonoTODO("Mono does not support COM")]
522                 public static Type GetTypeFromCLSID (Guid clsid, string server)
523                 {
524                         throw new NotImplementedException ();
525                 }
526
527                 [MonoTODO("Mono does not support COM")]
528                 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
529                 {
530                         throw new NotImplementedException ();
531                 }
532
533                 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
534                 { 
535                         if (handle.Value == IntPtr.Zero)
536                                 // This is not consistent with the other GetXXXFromHandle methods, but
537                                 // MS.NET seems to do this
538                                 return null;
539                         return internal_from_handle (handle.Value);
540                 }
541
542                 [MonoTODO("Mono does not support COM")]
543                 public static Type GetTypeFromProgID (string progID)
544                 {
545                         throw new NotImplementedException ();
546                 }
547
548                 [MonoTODO("Mono does not support COM")]
549                 public static Type GetTypeFromProgID (string progID, bool throwOnError)
550                 {
551                         throw new NotImplementedException ();
552                 }
553
554                 [MonoTODO("Mono does not support COM")]
555                 public static Type GetTypeFromProgID (string progID, string server)
556                 {
557                         throw new NotImplementedException ();
558                 }
559
560                 [MonoTODO("Mono does not support COM")]
561                 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
562                 {
563                         throw new NotImplementedException ();
564                 }
565
566                 public static RuntimeTypeHandle GetTypeHandle (object o)
567                 {
568                         return o.GetType().TypeHandle;
569                 }
570
571                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
572                 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
573
574                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
575                 internal static extern bool type_is_assignable_from (Type a, Type b);
576
577                 public new Type GetType ()
578                 {
579                         return base.GetType ();
580                 }
581
582 #if NET_2_0
583                 [ComVisible (true)]
584 #endif
585                 public virtual bool IsSubclassOf (Type c)
586                 {
587                         if (c == null || c == this)
588                                 return false;
589
590                         // Fast check for system types
591                         if (IsSystemType)
592                                 return c.IsSystemType && type_is_subtype_of (this, c, false);
593
594                         // User defined types depend on this behavior
595                         for (Type type = BaseType; type != null; type = type.BaseType)
596                                 if (type == c)
597                                         return true;
598
599                         return false;
600                 }
601
602                 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
603                 {
604                         if (filter == null)
605                                 throw new ArgumentNullException ("filter");
606
607                         ArrayList ifaces = new ArrayList ();
608                         foreach (Type iface in GetInterfaces ()) {
609                                 if (filter (iface, filterCriteria))
610                                         ifaces.Add (iface);
611                         }
612
613                         return (Type []) ifaces.ToArray (typeof (Type));
614                 }
615                 
616                 public Type GetInterface (string name) {
617                         return GetInterface (name, false);
618                 }
619
620                 public abstract Type GetInterface (string name, bool ignoreCase);
621
622                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
623                 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
624
625 #if NET_2_0
626                 [ComVisible (true)]
627 #endif          
628                 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
629                         InterfaceMapping res;
630                         if (interfaceType == null)
631                                 throw new ArgumentNullException ("interfaceType");
632                         if (!interfaceType.IsInterface)
633                                 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
634                         res.TargetType = this;
635                         res.InterfaceType = interfaceType;
636                         GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
637                         if (res.TargetMethods == null)
638                                 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
639
640                         return res;
641                 }
642
643                 public abstract Type[] GetInterfaces ();
644
645                 public virtual bool IsAssignableFrom (Type c)
646                 {
647                         if (c == null)
648                                 return false;
649
650                         if (Equals (c))
651                                 return true;
652
653                         if (c is TypeBuilder)
654                                 return ((TypeBuilder)c).IsAssignableTo (this);
655
656                         /* Handle user defined type classes */
657                         if (!IsSystemType) {
658                                 Type systemType = UnderlyingSystemType;
659                                 if (!systemType.IsSystemType)
660                                         return false;
661                                 return systemType.IsAssignableFrom (c);
662                         }
663
664                         if (!c.IsSystemType) {
665                                 Type underlyingType = c.UnderlyingSystemType;
666                                 if (!underlyingType.IsSystemType)
667                                         return false;
668                                 return IsAssignableFrom (underlyingType);
669                         }
670
671                         return type_is_assignable_from (this, c);
672                 }
673
674                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
675                 public extern virtual bool IsInstanceOfType (object o);
676
677                 public virtual int GetArrayRank ()
678                 {
679                         throw new NotSupportedException ();     // according to MSDN
680                 }
681
682                 public abstract Type GetElementType ();
683
684                 public EventInfo GetEvent (string name)
685                 {
686                         return GetEvent (name, DefaultBindingFlags);
687                 }
688
689                 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
690
691                 public virtual EventInfo[] GetEvents ()
692                 {
693                         return GetEvents (DefaultBindingFlags);
694                 }
695
696                 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
697
698                 public FieldInfo GetField( string name)
699                 {
700                         return GetField (name, DefaultBindingFlags);
701                 }
702
703                 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
704
705                 public FieldInfo[] GetFields ()
706                 {
707                         return GetFields (DefaultBindingFlags);
708                 }
709
710                 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
711                 
712                 public override int GetHashCode()
713                 {
714                         return (int)_impl.Value;
715                 }
716
717                 public MemberInfo[] GetMember (string name)
718                 {
719                         return GetMember (name, DefaultBindingFlags);
720                 }
721                 
722                 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
723                 {
724                         return GetMember (name, MemberTypes.All, bindingAttr);
725                 }
726
727                 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
728                 {
729                         if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
730                                 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
731                         else
732                                 return FindMembers (type, bindingAttr, FilterName, name);
733                 }
734
735                 public MemberInfo[] GetMembers ()
736                 {
737                         return GetMembers (DefaultBindingFlags);
738                 }
739
740                 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
741
742                 public MethodInfo GetMethod (string name)
743                 {
744                         if (name == null)
745                                 throw new ArgumentNullException ("name");
746                         return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
747                 }
748
749                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
750                 {
751                         if (name == null)
752                                 throw new ArgumentNullException ("name");
753                         
754                         return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
755                 }
756                 
757                 public MethodInfo GetMethod (string name, Type[] types)
758                 {
759                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
760                 }
761
762                 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
763                 {
764                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
765                 }
766
767                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
768                                              Type[] types, ParameterModifier[] modifiers)
769                 {
770                         
771                         return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
772                 }
773
774                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
775                                              CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
776                 {
777                         if (name == null)
778                                 throw new ArgumentNullException ("name");
779                         if (types == null)
780                                 throw new ArgumentNullException ("types");
781
782                         for (int i = 0; i < types.Length; i++) 
783                                 if (types[i] == null)
784                                         throw new ArgumentNullException ("types");
785
786                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
787                 }
788
789                 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
790                                                              CallingConventions callConvention, Type[] types,
791                                                              ParameterModifier[] modifiers);
792
793                 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
794                                                                                                                         CallingConventions callConvention, Type[] types,
795                                                                                                                         ParameterModifier[] modifiers)
796                 {
797                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
798                 }
799
800                 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
801                 {
802                         throw new System.InvalidOperationException ("can only be called in generic type");
803                 }
804
805                 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
806                 {
807                         throw new System.InvalidOperationException ("can only be called in generic type");
808                 }
809
810                 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
811                 {
812                         throw new System.InvalidOperationException ("can only be called in generic type");
813                 }
814
815                 
816                 public MethodInfo[] GetMethods ()
817                 {
818                         return GetMethods (DefaultBindingFlags);
819                 }
820
821                 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
822
823                 public Type GetNestedType (string name)
824                 {
825                         return GetNestedType (name, DefaultBindingFlags);
826                 }
827
828                 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
829
830                 public Type[] GetNestedTypes ()
831                 {
832                         return GetNestedTypes (DefaultBindingFlags);
833                 }
834
835                 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
836
837
838                 public PropertyInfo[] GetProperties ()
839                 {
840                         return GetProperties (DefaultBindingFlags);
841                 }
842
843                 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
844
845
846                 public PropertyInfo GetProperty (string name)
847                 {
848                         if (name == null)
849                                 throw new ArgumentNullException ("name");
850
851                         return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
852                 }
853
854                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
855                 {
856                         if (name == null)
857                                 throw new ArgumentNullException ("name");
858                         return GetPropertyImpl (name, bindingAttr, null, null, null, null);
859                 }
860
861                 public PropertyInfo GetProperty (string name, Type returnType)
862                 {
863                         if (name == null)
864                                 throw new ArgumentNullException ("name");
865                         return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
866                 }
867
868                 public PropertyInfo GetProperty (string name, Type[] types)
869                 {
870                         return GetProperty (name, DefaultBindingFlags, null, null, types, null);
871                 }
872
873                 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
874                 {
875                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
876                 }
877
878                 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
879                 {
880                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
881                 }
882
883                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
884                                                  Type[] types, ParameterModifier[] modifiers)
885                 {
886                         if (name == null)
887                                 throw new ArgumentNullException ("name");
888                         if (types == null)
889                                 throw new ArgumentNullException ("types");
890
891                         foreach (Type t in types) {
892                                 if (t == null)
893                                         throw new ArgumentNullException ("types");
894                         }
895
896                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
897                 }
898
899                 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
900                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers);
901
902                 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
903                                                                                                            Type returnType, Type[] types, ParameterModifier[] modifiers)
904                 {
905                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
906                 }
907
908                 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
909                                                                        Binder binder,
910                                                                        CallingConventions callConvention,
911                                                                        Type[] types,
912                                                                        ParameterModifier[] modifiers);
913
914                 protected abstract TypeAttributes GetAttributeFlagsImpl ();
915                 protected abstract bool HasElementTypeImpl ();
916                 protected abstract bool IsArrayImpl ();
917                 protected abstract bool IsByRefImpl ();
918                 protected abstract bool IsCOMObjectImpl ();
919                 protected abstract bool IsPointerImpl ();
920                 protected abstract bool IsPrimitiveImpl ();
921                 
922                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
923                 internal static extern bool IsArrayImpl (Type type);
924
925                 protected virtual bool IsValueTypeImpl ()
926                 {
927                         if (this == typeof (ValueType) || this == typeof (Enum))
928                                 return false;
929
930                         return IsSubclassOf (typeof (ValueType));
931                 }
932                 
933                 protected virtual bool IsContextfulImpl ()
934                 {
935                         return typeof (ContextBoundObject).IsAssignableFrom (this);
936                 }
937
938                 protected virtual bool IsMarshalByRefImpl ()
939                 {
940                         return typeof (MarshalByRefObject).IsAssignableFrom (this);
941                 }
942
943 #if NET_2_0
944                 [ComVisible (true)]
945 #endif
946                 public ConstructorInfo GetConstructor (Type[] types)
947                 {
948                         return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
949                 }
950
951 #if NET_2_0
952                 [ComVisible (true)]
953 #endif
954                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
955                                                        Type[] types, ParameterModifier[] modifiers)
956                 {
957                         return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
958                 }
959
960 #if NET_2_0
961                 [ComVisible (true)]
962 #endif
963                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
964                                                        CallingConventions callConvention,
965                                                        Type[] types, ParameterModifier[] modifiers)
966                 {
967                         if (types == null)
968                                 throw new ArgumentNullException ("types");
969
970                         foreach (Type t in types) {
971                                 if (t == null)
972                                         throw new ArgumentNullException ("types");
973                         }
974
975                         return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
976                 }
977
978 #if NET_2_0
979                 [ComVisible (true)]
980 #endif
981                 public ConstructorInfo[] GetConstructors ()
982                 {
983                         return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
984                 }
985
986 #if NET_2_0
987                 [ComVisible (true)]
988 #endif          
989                 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
990
991                 public virtual MemberInfo[] GetDefaultMembers ()
992                 {
993                         object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
994                         if (att.Length == 0)
995                                 return new MemberInfo [0];
996
997                         MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
998                         return (member != null) ? member : new MemberInfo [0];
999                 }
1000
1001                 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1002                                                          MemberFilter filter, object filterCriteria)
1003                 {
1004                         MemberInfo[] result;
1005                         ArrayList l = new ArrayList ();
1006
1007                         // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1008                         // this.FullName, this.GetType().FullName, this.obj_address());
1009
1010                         if ((memberType & MemberTypes.Constructor) != 0) {
1011                                 ConstructorInfo[] c = GetConstructors (bindingAttr);
1012                                 if (filter != null) {
1013                                         foreach (MemberInfo m in c) {
1014                                                 if (filter (m, filterCriteria))
1015                                                         l.Add (m);
1016                                         }
1017                                 } else {
1018                                         l.AddRange (c);
1019                                 }
1020                         }
1021                         if ((memberType & MemberTypes.Event) != 0) {
1022                                 EventInfo[] c = GetEvents (bindingAttr);
1023                                 if (filter != null) {
1024                                         foreach (MemberInfo m in c) {
1025                                                 if (filter (m, filterCriteria))
1026                                                         l.Add (m);
1027                                         }
1028                                 } else {
1029                                         l.AddRange (c);
1030                                 }
1031                         }
1032                         if ((memberType & MemberTypes.Field) != 0) {
1033                                 FieldInfo[] c = GetFields (bindingAttr);
1034                                 if (filter != null) {
1035                                         foreach (MemberInfo m in c) {
1036                                                 if (filter (m, filterCriteria))
1037                                                         l.Add (m);
1038                                         }
1039                                 } else {
1040                                         l.AddRange (c);
1041                                 }
1042                         }
1043                         if ((memberType & MemberTypes.Method) != 0) {
1044                                 MethodInfo[] c = GetMethods (bindingAttr);
1045                                 if (filter != null) {
1046                                         foreach (MemberInfo m in c) {
1047                                                 if (filter (m, filterCriteria))
1048                                                         l.Add (m);
1049                                         }
1050                                 } else {
1051                                         l.AddRange (c);
1052                                 }
1053                         }
1054                         if ((memberType & MemberTypes.Property) != 0) {
1055                                 PropertyInfo[] c;
1056                                 int count = l.Count;
1057                                 Type ptype;
1058                                 if (filter != null) {
1059                                         ptype = this;
1060                                         while ((l.Count == count) && (ptype != null)) {
1061                                                 c = ptype.GetProperties (bindingAttr);
1062                                                 foreach (MemberInfo m in c) {
1063                                                         if (filter (m, filterCriteria))
1064                                                                 l.Add (m);
1065                                                 }
1066                                                 ptype = ptype.BaseType;
1067                                         }
1068                                 } else {
1069                                         c = GetProperties (bindingAttr);
1070                                         l.AddRange (c);
1071                                 }
1072                         }
1073                         if ((memberType & MemberTypes.NestedType) != 0) {
1074                                 Type[] c = GetNestedTypes (bindingAttr);
1075                                 if (filter != null) {
1076                                         foreach (MemberInfo m in c) {
1077                                                 if (filter (m, filterCriteria)) {
1078                                                         l.Add (m);
1079                                                 }
1080                                         }
1081                                 } else {
1082                                         l.AddRange (c);
1083                                 }
1084                         }
1085                         result = new MemberInfo [l.Count];
1086                         l.CopyTo (result);
1087                         return result;
1088                 }
1089
1090                 [DebuggerHidden]
1091                 [DebuggerStepThrough] 
1092                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1093                 {
1094                         return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1095                 }
1096
1097                 [DebuggerHidden]
1098                 [DebuggerStepThrough] 
1099                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1100                                             object target, object[] args, CultureInfo culture)
1101                 {
1102                         return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1103                 }
1104
1105                 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1106                                                      Binder binder, object target, object[] args,
1107                                                      ParameterModifier[] modifiers,
1108                                                      CultureInfo culture, string[] namedParameters);
1109
1110                 public override string ToString()
1111                 {
1112                         return FullName;
1113                 }
1114
1115                 internal bool IsSystemType {
1116                         get {
1117                                 return _impl.Value != IntPtr.Zero;
1118                         }
1119                 }
1120
1121 #if NET_2_0 || BOOTSTRAP_NET_2_0
1122                 public virtual Type[] GetGenericArguments ()
1123                 {
1124                         throw new NotSupportedException ();
1125                 }
1126
1127                 public virtual bool ContainsGenericParameters {
1128                         get { return false; }
1129                 }
1130
1131                 public virtual extern bool IsGenericTypeDefinition {
1132                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1133                         get;
1134                 }
1135
1136                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1137                 extern Type GetGenericTypeDefinition_impl ();
1138
1139                 public virtual Type GetGenericTypeDefinition ()
1140                 {
1141                         Type res = GetGenericTypeDefinition_impl ();
1142                         if (res == null)
1143                                 throw new InvalidOperationException ();
1144
1145                         return res;
1146                 }
1147
1148                 public virtual extern bool IsGenericType {
1149                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1150                         get;
1151                 }
1152
1153                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1154                 static extern Type MakeGenericType (Type gt, Type [] types);
1155
1156                 public virtual Type MakeGenericType (params Type[] types)
1157                 {
1158                         if (!IsGenericTypeDefinition)
1159                                 throw new InvalidOperationException ("not a generic type definition");
1160                         if (types == null)
1161                                 throw new ArgumentNullException ("types");
1162                         if (GetGenericArguments().Length != types.Length)
1163                                 throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", GetGenericArguments ().Length, types.Length), "types");
1164
1165                         Type[] systemTypes = new Type[types.Length];
1166                         for (int i = 0; i < types.Length; ++i) {
1167                                 Type t = types [i];
1168                                 if (t == null)
1169                                         throw new ArgumentNullException ("types");
1170
1171                                 t = t.UnderlyingSystemType;
1172                                 if (t == null || !t.IsSystemType)
1173                                         throw new ArgumentNullException ("types");
1174                                 systemTypes [i] = types [i].UnderlyingSystemType;
1175                         }
1176
1177                         Type res = MakeGenericType (this, systemTypes);
1178                         if (res == null)
1179                                 throw new TypeLoadException ();
1180                         return res;
1181                 }
1182
1183                 public virtual bool IsGenericParameter {
1184                         get {
1185                                 return false;
1186                         }
1187                 }
1188
1189                 public bool IsNested {
1190                         get {
1191                                 return DeclaringType != null;
1192                         }
1193                 }
1194
1195                 public bool IsVisible {
1196                         get {
1197                                 if (IsNestedPublic)
1198                                         return DeclaringType.IsVisible;
1199
1200                                 return IsPublic;
1201                         }
1202                 }
1203
1204                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1205                 extern int GetGenericParameterPosition ();
1206                 
1207                 public virtual int GenericParameterPosition {
1208                         get {
1209                                 int res = GetGenericParameterPosition ();
1210                                 if (res < 0)
1211                                         throw new InvalidOperationException ();
1212                                 return res;
1213                         }
1214                 }
1215
1216                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1217                 extern GenericParameterAttributes GetGenericParameterAttributes ();
1218
1219                 public virtual GenericParameterAttributes GenericParameterAttributes {
1220                         get {
1221                                 if (!IsGenericParameter)
1222                                         throw new InvalidOperationException ();
1223
1224                                 return GetGenericParameterAttributes ();
1225                         }
1226                 }
1227
1228                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1229                 extern Type[] GetGenericParameterConstraints_impl ();
1230
1231                 public virtual Type[] GetGenericParameterConstraints ()
1232                 {
1233                         if (!IsGenericParameter)
1234                                 throw new InvalidOperationException ();
1235
1236                         return GetGenericParameterConstraints_impl ();
1237                 }
1238
1239                 public virtual MethodBase DeclaringMethod {
1240                         get {
1241                                 return null;
1242                         }
1243                 }
1244
1245                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1246                 extern Type make_array_type (int rank);
1247
1248                 public virtual Type MakeArrayType ()
1249                 {
1250                         return MakeArrayType (1);
1251                 }
1252
1253                 public virtual Type MakeArrayType (int rank)
1254                 {
1255                         if (rank < 1)
1256                                 throw new IndexOutOfRangeException ();
1257                         return make_array_type (rank);
1258                 }
1259
1260                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1261                 extern Type make_byref_type ();
1262
1263                 public virtual Type MakeByRefType ()
1264                 {
1265                         return make_byref_type ();
1266                 }
1267
1268                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1269                 public extern virtual Type MakePointerType ();
1270
1271                 public static Type ReflectionOnlyGetType (string typeName, 
1272                                                           bool throwIfNotFound, 
1273                                                           bool ignoreCase)
1274                 {
1275                         if (typeName == null)
1276                                 throw new ArgumentNullException ("typeName");
1277                         int idx = typeName.IndexOf (',');
1278                         if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1279                                 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1280                         string an = typeName.Substring (idx + 1);
1281                         Assembly a;
1282                         try {
1283                                 a = Assembly.ReflectionOnlyLoad (an);
1284                         } catch {
1285                                 if (throwIfNotFound)
1286                                         throw;
1287                                 return null;
1288                         }
1289                         return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1290                 }
1291
1292                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1293                 extern void GetPacking (out int packing, out int size);         
1294
1295                 public virtual StructLayoutAttribute StructLayoutAttribute {
1296                         get {
1297                                 LayoutKind kind;
1298
1299                                 if (IsLayoutSequential)
1300                                         kind = LayoutKind.Sequential;
1301                                 else if (IsExplicitLayout)
1302                                         kind = LayoutKind.Explicit;
1303                                 else
1304                                         kind = LayoutKind.Auto;
1305
1306                                 StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1307
1308                                 if (IsUnicodeClass)
1309                                         attr.CharSet = CharSet.Unicode;
1310                                 else if (IsAnsiClass)
1311                                         attr.CharSet = CharSet.Ansi;
1312                                 else
1313                                         attr.CharSet = CharSet.Auto;
1314
1315                                 if (kind != LayoutKind.Auto)
1316                                         GetPacking (out attr.Pack, out attr.Size);
1317
1318                                 return attr;
1319                         }
1320                 }
1321
1322                 internal object[] GetPseudoCustomAttributes ()
1323                 {
1324                         int count = 0;
1325
1326                         /* IsSerializable returns true for delegates/enums as well */
1327                         if ((Attributes & TypeAttributes.Serializable) != 0)
1328                                 count ++;
1329                         if ((Attributes & TypeAttributes.Import) != 0)
1330                                 count ++;
1331
1332                         if (count == 0)
1333                                 return null;
1334                         object[] attrs = new object [count];
1335                         count = 0;
1336
1337                         if ((Attributes & TypeAttributes.Serializable) != 0)
1338                                 attrs [count ++] = new SerializableAttribute ();
1339                         if ((Attributes & TypeAttributes.Import) != 0)
1340                                 attrs [count ++] = new ComImportAttribute ();
1341
1342                         return attrs;
1343                 }                       
1344
1345 #endif
1346
1347                 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1348                 {
1349                         throw new NotImplementedException ();
1350                 }
1351
1352                 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1353                 {
1354                         throw new NotImplementedException ();
1355                 }
1356
1357                 void _Type.GetTypeInfoCount (out uint pcTInfo)
1358                 {
1359                         throw new NotImplementedException ();
1360                 }
1361
1362                 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1363                 {
1364                         throw new NotImplementedException ();
1365                 }
1366         }
1367 }