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