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