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