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