2003-12-24 Ben Maurer <bmaurer@users.sourceforge.net>
[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 // TODO: Mucho left to implement.
10 //
11
12 using System.Reflection;
13 using System.Collections;
14 using System.Runtime.InteropServices;
15 using System.Runtime.CompilerServices;
16 using System.Globalization;
17
18 namespace System {
19
20         //
21         // FIXME: Implement the various IReflect dependencies
22         //
23
24         [MonoTODO]
25         [Serializable]
26         [ClassInterface (ClassInterfaceType.AutoDual)]
27         public abstract class Type : MemberInfo, IReflect {
28                 
29                 internal RuntimeTypeHandle _impl;
30
31                 public static readonly char Delimiter = '.';
32                 public static readonly Type[] EmptyTypes = {};
33                 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
34                 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
35                 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
36                 public static readonly object Missing;
37
38                 protected const BindingFlags DefaultBindingFlags =
39                 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
40
41                 /* implementation of the delegates for MemberFilter */
42                 static bool FilterName_impl (MemberInfo m, object filterCriteria)
43                 {
44                         string name = (string) filterCriteria;
45                         return name.Equals (m.Name);
46                 }
47                 
48                 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
49                 {
50                         string name = (string) filterCriteria;
51                         return String.Compare (name, m.Name, true, CultureInfo.InvariantCulture) == 0;
52                 }
53                 
54                 [MonoTODO]
55                 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
56                 {
57                         throw new NotImplementedException ("FilterAttribute_impl");
58                 }
59
60                 protected Type ()
61                 {
62                 }
63
64                 /// <summary>
65                 ///   The assembly where the type is defined.
66                 /// </summary>
67                 public abstract Assembly Assembly {
68                         get;
69                 }
70
71                 /// <summary>
72                 ///   Gets the fully qualified name for the type including the
73                 ///   assembly name where the type is defined.
74                 /// </summary>
75                 public abstract string AssemblyQualifiedName {
76                         get;
77                 }
78
79                 /// <summary>
80                 ///   Returns the Attributes associated with the type.
81                 /// </summary>
82                 public TypeAttributes Attributes {
83                         get {
84                                 return GetAttributeFlagsImpl ();
85                         }
86                 }
87                 
88                 /// <summary>
89                 ///   Returns the basetype for this type
90                 /// </summary>
91                 public abstract Type BaseType {
92                         get;
93                 }
94                         
95                 /// <summary>
96                 ///   Returns the class that declares the member.
97                 /// </summary>
98                 public override Type DeclaringType {
99                         get {
100                                 return null;
101                         }
102                 }
103
104                 /// <summary>
105                 ///
106                 /// </summary>
107                 public static Binder DefaultBinder {
108                         get {
109                                 return Binder.DefaultBinder;
110                         }
111                 }
112                 
113                 /// <summary>
114                 ///    The full name of the type including its namespace
115                 /// </summary>
116                 public abstract string FullName {
117                         get;
118                 }
119
120                 public abstract Guid GUID {
121                         get;
122                 }
123
124                 public bool HasElementType {
125                         get {
126                                 return HasElementTypeImpl ();
127                         }
128                 }
129
130                 public bool IsAbstract {
131                         get {
132                                 return (Attributes & TypeAttributes.Abstract) != 0;
133                         }
134                 }
135
136                 public bool IsAnsiClass {
137                         get {
138                                 return (Attributes & TypeAttributes.StringFormatMask)
139                                 == TypeAttributes.AnsiClass;
140                         }
141                 }
142
143                 public bool IsArray {
144                         get {
145                                 return IsArrayImpl ();
146                         }
147                 }
148
149                 public bool IsAutoClass {
150                         get {
151                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
152                         }
153                 }
154
155                 public bool IsAutoLayout {
156                         get {
157                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
158                         }
159                 }
160
161                 public bool IsByRef {
162                         get {
163                                 return IsByRefImpl ();
164                         }
165                 }
166
167                 public bool IsClass {
168                         get {
169                                 //
170                                 // This code used to probe for "this == typeof (System.Enum)", but in
171                                 // The .NET Framework 1.0, the above test return false
172                                 //
173                                 if (this == typeof (System.ValueType))
174                                         return true;
175                                 if (IsInterface)
176                                         return false;
177                                 return !type_is_subtype_of (this, typeof (System.ValueType), false);
178                         }
179                 }
180
181                 public bool IsCOMObject {
182                         get {
183                                 return IsCOMObjectImpl ();
184                         }
185                 }
186
187                 public bool IsContextful {
188                         get {
189                                 return IsContextfulImpl ();
190                         }
191                 }
192
193                 public bool IsEnum {
194                         get {
195                                 return type_is_subtype_of (this, typeof (System.Enum), false) &&
196                                         this != typeof (System.Enum);
197                         }
198                 }
199
200                 public bool IsExplicitLayout {
201                         get {
202                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
203                         }
204                 }
205
206                 public bool IsImport {
207                         get {
208                                 return (Attributes & TypeAttributes.Import) != 0;
209                         }
210                 }
211
212                 public bool IsInterface {
213                         get {
214                                 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
215                         }
216                 }
217
218                 public bool IsLayoutSequential {
219                         get {
220                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
221                         }
222                 }
223
224                 public bool IsMarshalByRef {
225                         get {
226                                 return IsMarshalByRefImpl ();
227                         }
228                 }
229
230                 public bool IsNestedAssembly {
231                         get {
232                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
233                         }
234                 }
235
236                 public bool IsNestedFamANDAssem {
237                         get {
238                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
239                         }
240                 }
241
242                 public bool IsNestedFamily {
243                         get {
244                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
245                         }
246                 }
247
248                 public bool IsNestedFamORAssem {
249                         get {
250                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
251                         }
252                 }
253
254                 public bool IsNestedPrivate {
255                         get {
256                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
257                         }
258                 }
259
260                 public bool IsNestedPublic {
261                         get {
262                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
263                         }
264                 }
265
266                 public bool IsNotPublic {
267                         get {
268                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
269                         }
270                 }
271
272                 public bool IsPointer {
273                         get {
274                                 return IsPointerImpl ();
275                         }
276                 }
277
278                 public bool IsPrimitive {
279                         get {
280                                 return IsPrimitiveImpl ();
281                         }
282                 }
283
284                 public bool IsPublic {
285                         get {
286                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
287                         }
288                 }
289
290                 public bool IsSealed {
291                         get {
292                                 return (Attributes & TypeAttributes.Sealed) != 0;
293                         }
294                 }
295
296                 public bool IsSerializable {
297                         get {
298                                 // Enums and delegates are always serializable
299                                 return (Attributes & TypeAttributes.Serializable) != 0 || IsEnum || 
300                                         type_is_subtype_of (this, typeof (System.Delegate), false);
301                         }
302                 }
303
304                 public bool IsSpecialName {
305                         get {
306                                 return (Attributes & TypeAttributes.SpecialName) != 0;
307                         }
308                 }
309
310                 public bool IsUnicodeClass {
311                         get {
312                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
313                         }
314                 }
315
316                 public bool IsValueType {
317                         get {
318                                 return IsValueTypeImpl ();
319                         }
320                 }
321
322                 public override MemberTypes MemberType {
323                         get {return MemberTypes.TypeInfo;}
324                 }
325
326                 public abstract Module Module {get;}
327         
328                 public abstract string Namespace {get;}
329
330                 public override Type ReflectedType {
331                         get {
332                                 return null;
333                         }
334                 }
335
336                 public abstract RuntimeTypeHandle TypeHandle {get;}
337
338                 public ConstructorInfo TypeInitializer {
339                         get {
340                                 return GetConstructorImpl (
341                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
342                                         null,
343                                         CallingConventions.Any,
344                                         EmptyTypes,
345                                         null);
346                         }
347                 }
348
349                 public abstract Type UnderlyingSystemType {get;}
350
351                 public override bool Equals (object o)
352                 {
353                         if (o == null)
354                                 return false;
355                         
356                         // TODO: return UnderlyingSystemType == o.UnderlyingSystemType;
357                         Type cmp = o as Type;
358                         if (cmp == null)
359                                 return false;
360                         return Equals (cmp);
361                 }
362                 
363                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
364                 public extern bool Equals (Type type);
365                 
366                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
367                 private static extern Type internal_from_handle (IntPtr handle);
368                 
369                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
370                 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
371
372                 public static Type GetType(string typeName)
373                 {
374                         if (typeName == null)
375                                 throw new ArgumentNullException ("typeName");
376
377                         return internal_from_name (typeName, false, false);
378                 }
379
380                 public static Type GetType(string typeName, bool throwOnError)
381                 {
382                         if (typeName == null)
383                                 throw new ArgumentNullException ("typeName");
384
385                         Type type = internal_from_name (typeName, throwOnError, false);
386                         if (throwOnError && type == null)
387                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
388
389                         return type;
390                 }
391
392                 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
393                 {
394                         if (typeName == null)
395                                 throw new ArgumentNullException ("typeName");
396
397                         Type t = internal_from_name (typeName, throwOnError, ignoreCase);
398                         if (throwOnError && t == null)
399                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
400
401                         return t;
402                 }
403
404                 public static Type[] GetTypeArray (object[] args) {
405                         if (args == null)
406                                 throw new ArgumentNullException ("args");
407
408                         Type[] ret;
409                         ret = new Type [args.Length];
410                         for (int i = 0; i < args.Length; ++i)
411                                 ret [i] = args[i].GetType ();
412                         return ret;
413                 }
414
415                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
416                 public extern static TypeCode GetTypeCode (Type type);
417
418                 [MonoTODO]
419                 public static Type GetTypeFromCLSID (Guid clsid)
420                 {
421                         throw new NotImplementedException ();
422                 }
423
424                 [MonoTODO]
425                 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
426                 {
427                         throw new NotImplementedException ();
428                 }
429
430                 [MonoTODO]
431                 public static Type GetTypeFromCLSID (Guid clsid, string server)
432                 {
433                         throw new NotImplementedException ();
434                 }
435
436                 [MonoTODO]
437                 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
438                 {
439                         throw new NotImplementedException ();
440                 }
441
442                 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
443                 { 
444                         return internal_from_handle (handle.Value);
445                 }
446
447                 [MonoTODO]
448                 public static Type GetTypeFromProgID (string progID)
449                 {
450                         throw new NotImplementedException ();
451                 }
452
453                 [MonoTODO]
454                 public static Type GetTypeFromProgID (string progID, bool throwOnError)
455                 {
456                         throw new NotImplementedException ();
457                 }
458
459                 [MonoTODO]
460                 public static Type GetTypeFromProgID (string progID, string server)
461                 {
462                         throw new NotImplementedException ();
463                 }
464
465                 [MonoTODO]
466                 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
467                 {
468                         throw new NotImplementedException ();
469                 }
470
471                 public static RuntimeTypeHandle GetTypeHandle (object o)
472                 {
473                         return o.GetType().TypeHandle;
474                 }
475
476                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
477                 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
478
479                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
480                 internal static extern bool type_is_assignable_from (Type a, Type b);
481                 
482                 public virtual bool IsSubclassOf (Type c)
483                 {
484                         if (c == null)
485                                 return false;
486
487                         return (this != c) && type_is_subtype_of (this, c, false);
488                 }
489
490                 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
491                 {
492                         if (filter == null)
493                                 throw new ArgumentNullException ("filter");
494
495                         ArrayList ifaces = new ArrayList ();
496                         foreach (Type iface in GetInterfaces ()) {
497                                 if (filter (iface, filterCriteria))
498                                         ifaces.Add (iface);
499                         }
500
501                         return (Type []) ifaces.ToArray (typeof (Type));
502                 }
503                 
504                 public Type GetInterface (string name) {
505                         return GetInterface (name, false);
506                 }
507
508                 public abstract Type GetInterface (string name, bool ignoreCase);
509
510                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
511                 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
512                 
513                 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
514                         InterfaceMapping res;
515                         if (interfaceType == null)
516                                 throw new ArgumentNullException ("interfaceType");
517                         if (!interfaceType.IsInterface)
518                                 throw new ArgumentException ("type argument must be an interface", "interfaceType");
519                         res.TargetType = this;
520                         res.InterfaceType = interfaceType;
521                         GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
522                         if (res.TargetMethods == null)
523                                 throw new ArgumentException ("interface not found", "interfaceType");
524
525                         return res;
526                 }
527
528                 public abstract Type[] GetInterfaces ();
529
530                 public virtual bool IsAssignableFrom (Type c)
531                 {
532                         if (c == null)
533                                 return false;
534
535                         if (Equals (c))
536                                 return true;
537
538                         return type_is_assignable_from (this, c);
539                 }
540
541                 public virtual bool IsInstanceOfType (object o) {
542                         if (o != null)
543                                 return IsAssignableFrom (o.GetType ());
544
545                         return false;
546                 }
547
548                 public virtual int GetArrayRank ()
549                 {
550                         throw new NotSupportedException ();     // according to MSDN
551                 }
552
553                 public abstract Type GetElementType ();
554
555                 public EventInfo GetEvent (string name)
556                 {
557                         return GetEvent (name, DefaultBindingFlags);
558                 }
559
560                 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
561
562                 public virtual EventInfo[] GetEvents ()
563                 {
564                         return GetEvents (DefaultBindingFlags);
565                 }
566
567                 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
568
569                 public FieldInfo GetField( string name)
570                 {
571                         return GetField (name, DefaultBindingFlags);
572                 }
573
574                 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
575
576                 public FieldInfo[] GetFields ()
577                 {
578                         return GetFields (DefaultBindingFlags);
579                 }
580
581                 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
582                 
583                 public override int GetHashCode()
584                 {
585                         return (int)_impl.Value;
586                 }
587
588                 public MemberInfo[] GetMember( string name)
589                 {
590                         return GetMember (name, DefaultBindingFlags);
591                 }
592                 
593                 public virtual MemberInfo[] GetMember( string name, BindingFlags bindingAttr)
594                 {
595                         return GetMember (name, MemberTypes.All, bindingAttr);
596                 }
597
598                 public virtual MemberInfo[] GetMember (string name, MemberTypes type,
599                                                        BindingFlags bindingAttr)
600                 {
601                         return FindMembers (type, bindingAttr, FilterName, name);
602                 }
603
604                 public MemberInfo[] GetMembers()
605                 {
606                         return GetMembers (DefaultBindingFlags);
607                 }
608
609                 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
610
611                 public MethodInfo GetMethod (string name)
612                 {
613                         if (name == null)
614                                 throw new ArgumentNullException ("name");
615                         return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
616                 }
617
618                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
619                 {
620                         if (name == null)
621                                 throw new ArgumentNullException ("name");
622                         
623                         return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
624                 }
625                 
626                 public MethodInfo GetMethod (string name, Type[] types)
627                 {
628                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
629                 }
630
631                 public MethodInfo GetMethod( string name, Type[] types, ParameterModifier[] modifiers)
632                 {
633                         return GetMethod (name, DefaultBindingFlags, null,
634                                           CallingConventions.Any, types, modifiers);
635                 }
636
637                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
638                                              Type[] types, ParameterModifier[] modifiers)
639                 {
640                         
641                         return GetMethod (name, bindingAttr, binder,
642                                           CallingConventions.Any, types, modifiers);
643                 }
644
645                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr,
646                                              Binder binder, CallingConventions callConvention,
647                                              Type[] types, ParameterModifier[] modifiers)
648                 {
649                         if (name == null)
650                                 throw new ArgumentNullException ("name");
651                         if (types == null)
652                                 throw new ArgumentNullException ("types");
653
654                         for (int i = 0; i < types.Length; i++) 
655                                 if (types[i] == null)
656                                         throw new ArgumentNullException ("types");
657
658                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
659                 }
660
661                 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
662                                                              Binder binder,
663                                                              CallingConventions callConvention,
664                                                              Type[] types, ParameterModifier[] modifiers);
665
666                 public MethodInfo[] GetMethods ()
667                 {
668                         return GetMethods (DefaultBindingFlags);
669                 }
670
671                 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
672
673                 public Type GetNestedType (string name)
674                 {
675                         return GetNestedType (name, DefaultBindingFlags);
676                 }
677
678                 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
679
680                 public Type[] GetNestedTypes ()
681                 {
682                         return GetNestedTypes (DefaultBindingFlags);
683                 }
684
685                 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
686
687
688                 public PropertyInfo[] GetProperties ()
689                 {
690                         return GetProperties (DefaultBindingFlags);
691                 }
692
693                 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
694
695
696                 public PropertyInfo GetProperty (string name)
697                 {
698                         if (name == null)
699                                 throw new ArgumentNullException ("name");
700
701                         return GetPropertyImpl (name, DefaultBindingFlags, null, null, new Type[0], null);
702                 }
703
704                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
705                 {
706                         if (name == null)
707                                 throw new ArgumentNullException ("name");
708                         return GetPropertyImpl (name, bindingAttr, null, null, new Type[0], null);
709                 }
710
711                 public PropertyInfo GetProperty (string name, Type returnType)
712                 {
713                         if (name == null)
714                                 throw new ArgumentNullException ("name");
715                         return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, new Type[0], null);
716                 }
717
718                 public PropertyInfo GetProperty (string name, Type[] types)
719                 {
720                         return GetProperty (name, DefaultBindingFlags, null, null, types, null);
721                 }
722
723                 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
724                 {
725                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
726                 }
727
728                 public PropertyInfo GetProperty( string name, Type returnType, Type[] types,
729                                                  ParameterModifier[] modifiers)
730                 {
731                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
732                 }
733
734                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr,
735                                                  Binder binder, Type returnType,
736                                                  Type[] types, ParameterModifier[] modifiers)
737                 {
738                         if (name == null)
739                                 throw new ArgumentNullException ("name");
740                         if (types == null)
741                                 throw new ArgumentNullException ("types");
742
743                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
744                 }
745
746                 protected abstract PropertyInfo GetPropertyImpl (string name,
747                                                                  BindingFlags bindingAttr,
748                                                                  Binder binder, Type returnType,
749                                                                  Type[] types,
750                                                                  ParameterModifier[] modifiers);
751
752                 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
753                                                                        Binder binder,
754                                                                        CallingConventions callConvention,
755                                                                        Type[] types,
756                                                                        ParameterModifier[] modifiers);
757
758                 protected abstract TypeAttributes GetAttributeFlagsImpl ();
759                 protected abstract bool HasElementTypeImpl ();
760                 protected abstract bool IsArrayImpl ();
761                 protected abstract bool IsByRefImpl ();
762                 protected abstract bool IsCOMObjectImpl ();
763                 protected abstract bool IsPointerImpl ();
764                 protected abstract bool IsPrimitiveImpl ();
765                 
766                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
767                 internal static extern bool IsArrayImpl (Type type);
768
769                 protected virtual bool IsValueTypeImpl ()
770                 {
771                         if (this == typeof (Enum) || this == typeof (ValueType))
772                                 return false;
773
774                         return IsSubclassOf (typeof (ValueType));
775                 }
776                 
777                 protected virtual bool IsContextfulImpl ()
778                 {
779                         return typeof (ContextBoundObject).IsAssignableFrom (this);
780                 }
781
782                 protected virtual bool IsMarshalByRefImpl ()
783                 {
784                         return typeof (MarshalByRefObject).IsAssignableFrom (this);
785                 }
786
787                 public ConstructorInfo GetConstructor (Type[] types)
788                 {
789                         return GetConstructorImpl (
790                                 DefaultBindingFlags, null, CallingConventions.Any, types, null);
791                 }
792
793                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
794                                                        Type[] types, ParameterModifier[] modifiers)
795                 {
796                         return GetConstructorImpl (
797                                 bindingAttr, binder, CallingConventions.Any, types, modifiers);
798                 }
799
800                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
801                                                        CallingConventions callConvention,
802                                                        Type[] types, ParameterModifier[] modifiers)
803                 {
804                         if (types == null)
805                                 throw new ArgumentNullException ("types");
806
807                         return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
808                 }
809
810                 public ConstructorInfo[] GetConstructors ()
811                 {
812                         return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
813                 }
814                 
815                 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
816
817                 public virtual MemberInfo[] GetDefaultMembers ()
818                 {
819                         object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
820                         if (att.Length == 0)
821                                 return new MemberInfo [0];
822
823                         MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
824                         return (member != null) ? member : new MemberInfo [0];
825                 }
826
827                 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
828                                                          MemberFilter filter, object filterCriteria)
829                 {
830                         MemberInfo[] result;
831                         ArrayList l = new ArrayList ();
832
833                         // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
834                         // this.FullName, this.GetType().FullName, this.obj_address());
835
836                         if ((memberType & MemberTypes.Constructor) != 0) {
837                                 ConstructorInfo[] c = GetConstructors (bindingAttr);
838                                 if (filter != null) {
839                                         foreach (MemberInfo m in c) {
840                                                 if (filter (m, filterCriteria))
841                                                         l.Add (m);
842                                         }
843                                 } else {
844                                         l.AddRange (c);
845                                 }
846                         }
847                         if ((memberType & MemberTypes.Event) != 0) {
848                                 EventInfo[] c = GetEvents (bindingAttr);
849                                 if (filter != null) {
850                                         foreach (MemberInfo m in c) {
851                                                 if (filter (m, filterCriteria))
852                                                         l.Add (m);
853                                         }
854                                 } else {
855                                         l.AddRange (c);
856                                 }
857                         }
858                         if ((memberType & MemberTypes.Field) != 0) {
859                                 FieldInfo[] c = GetFields (bindingAttr);
860                                 if (filter != null) {
861                                         foreach (MemberInfo m in c) {
862                                                 if (filter (m, filterCriteria))
863                                                         l.Add (m);
864                                         }
865                                 } else {
866                                         l.AddRange (c);
867                                 }
868                         }
869                         if ((memberType & MemberTypes.Method) != 0) {
870                                 MethodInfo[] c = GetMethods (bindingAttr);
871                                 if (filter != null) {
872                                         foreach (MemberInfo m in c) {
873                                                 if (filter (m, filterCriteria))
874                                                         l.Add (m);
875                                         }
876                                 } else {
877                                         l.AddRange (c);
878                                 }
879                         }
880                         if ((memberType & MemberTypes.Property) != 0) {
881                                 PropertyInfo[] c;
882                                 int count = l.Count;
883                                 Type ptype;
884                                 if (filter != null) {
885                                         ptype = this;
886                                         while ((l.Count == count) && (ptype != null)) {
887                                                 c = ptype.GetProperties (bindingAttr);
888                                                 foreach (MemberInfo m in c) {
889                                                         if (filter (m, filterCriteria))
890                                                                 l.Add (m);
891                                                 }
892                                                 ptype = ptype.BaseType;
893                                         }
894                                 } else {
895                                         c = GetProperties (bindingAttr);
896                                         l.AddRange (c);
897                                 }
898                         }
899                         if ((memberType & MemberTypes.NestedType) != 0) {
900                                 Type[] c = GetNestedTypes (bindingAttr);
901                                 if (filter != null) {
902                                         foreach (MemberInfo m in c) {
903                                                 if (filter (m, filterCriteria)) {
904                                                         l.Add (m);
905                                                 }
906                                         }
907                                 } else {
908                                         l.AddRange (c);
909                                 }
910                         }
911                         result = new MemberInfo [l.Count];
912                         l.CopyTo (result);
913                         return result;
914                 }
915
916                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
917                                             object target, object[] args)
918                 {
919                         return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
920                 }
921
922                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
923                                             object target, object[] args, CultureInfo culture)
924                 {
925                         return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
926                 }
927
928                 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
929                                                      Binder binder, object target, object[] args,
930                                                      ParameterModifier[] modifiers,
931                                                      CultureInfo culture, string[] namedParameters);
932
933                 public override string ToString()
934                 {
935                         return FullName;
936                 }
937
938 #if NET_1_2
939                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
940                 public extern Type [] GetGenericArguments ();
941
942                 public abstract bool HasGenericArguments {
943                         get;
944                 }
945
946                 public abstract bool ContainsGenericParameters {
947                         get;
948                 }
949
950                 public extern bool IsGenericTypeDefinition {
951                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
952                         get;
953                 }
954
955                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
956                 extern Type GetGenericTypeDefinition_impl ();
957
958                 public virtual Type GetGenericTypeDefinition ()
959                 {
960                         Type res = GetGenericTypeDefinition_impl ();
961                         if (res == null)
962                                 throw new InvalidOperationException ();
963
964                         return res;
965                 }
966
967                 public extern bool IsGenericInstance {
968                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
969                         get;
970                 }
971
972                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
973                 static extern Type BindGenericParameters (Type gt, Type [] types);
974                 
975                 public Type BindGenericParameters (Type [] types)
976                 {
977                         if (types == null)
978                                 throw new ArgumentNullException ("types");
979                         foreach (Type t in types) {
980                                 if (t == null)
981                                         throw new ArgumentNullException ("types");
982                         }
983                         Type res = BindGenericParameters (this, types);
984                         if (res == null)
985                                 throw new TypeLoadException ();
986                         return res;
987                 }
988
989                 public abstract bool IsGenericParameter {
990                         get;
991                 }
992
993                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
994                 extern int GetGenericParameterPosition ();
995                 
996                 public virtual int GenericParameterPosition {
997                         get {
998                                 int res = GetGenericParameterPosition ();
999                                 if (res < 0)
1000                                         throw new InvalidOperationException ();
1001                                 return res;
1002                         }
1003                 }
1004
1005                 public abstract MethodInfo DeclaringMethod {
1006                         get;
1007                 }
1008
1009                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1010                 extern Type make_array_type (int rank);
1011
1012                 public virtual Type MakeArrayType ()
1013                 {
1014                         return MakeArrayType (1);
1015                 }
1016
1017                 public virtual Type MakeArrayType (int rank)
1018                 {
1019                         if (rank < 1)
1020                                 throw new IndexOutOfRangeException ();
1021                         return make_array_type (rank);
1022                 }
1023
1024                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1025                 extern Type make_byref_type ();
1026
1027                 public virtual Type MakeByRefType ()
1028                 {
1029                         return make_byref_type ();
1030                 }
1031 #endif
1032         }
1033 }