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