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