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