b07e088c684c6f569f9a11f4c151fea16f18f78a
[mono.git] / mcs / class / corlib / System / Type.cs
1 //
2 // System.Type.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@gmail.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Diagnostics;
35 using System.Reflection;
36 #if !FULL_AOT_RUNTIME
37 using System.Reflection.Emit;
38 #endif
39 using System.Collections;
40 using System.Collections.Generic;
41 using System.Runtime.InteropServices;
42 using System.Runtime.CompilerServices;
43 using System.Threading;
44 using System.Globalization;
45
46 namespace System {
47
48         [Serializable]
49         [ClassInterface (ClassInterfaceType.None)]
50         [ComVisible (true)]
51         [ComDefaultInterface (typeof (_Type))]
52         [StructLayout (LayoutKind.Sequential)]
53 #if MOBILE
54         public abstract class Type : MemberInfo, IReflect {
55 #else
56         public abstract class Type : MemberInfo, IReflect, _Type {
57 #endif
58
59                 internal RuntimeTypeHandle _impl;
60
61                 public static readonly char Delimiter = '.';
62                 public static readonly Type[] EmptyTypes = {};
63                 public static readonly MemberFilter FilterAttribute = new MemberFilter (FilterAttribute_impl);
64                 public static readonly MemberFilter FilterName = new MemberFilter (FilterName_impl);
65                 public static readonly MemberFilter FilterNameIgnoreCase = new MemberFilter (FilterNameIgnoreCase_impl);
66                 public static readonly object Missing = System.Reflection.Missing.Value;
67
68                 internal const BindingFlags DefaultBindingFlags =
69                 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
70
71                 /* implementation of the delegates for MemberFilter */
72                 static bool FilterName_impl (MemberInfo m, object filterCriteria)
73                 {
74                         string name = (string) filterCriteria;
75                         if (name == null || name.Length == 0 )
76                                 return false; // because m.Name cannot be null or empty
77                         
78                         if (name [name.Length - 1] == '*')
79                                 return m.Name.StartsWithOrdinalUnchecked (name.Substring (0, name.Length - 1));
80                         
81                         return m.Name == name;
82                 }
83
84                 static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
85                 {
86                         string name = (string) filterCriteria;
87                         if (name == null || name.Length == 0 )
88                                 return false; // because m.Name cannot be null or empty
89                                 
90                         if (name [name.Length - 1] == '*')
91                                 return m.Name.StartsWithOrdinalCaseInsensitiveUnchecked (name.Substring (0, name.Length - 1));
92                         
93                         return string.CompareOrdinalCaseInsensitiveUnchecked (m.Name, name) == 0;
94                 }
95
96                 static bool FilterAttribute_impl (MemberInfo m, object filterCriteria)
97                 {
98                         if (!(filterCriteria is int))
99                                 throw new InvalidFilterCriteriaException ("Int32 value is expected for filter criteria");
100
101                         int flags = (int) filterCriteria;
102                         if (m is MethodInfo)
103                                 return ((int)((MethodInfo)m).Attributes & flags) != 0;
104                         if (m is FieldInfo)
105                                 return ((int)((FieldInfo)m).Attributes & flags) != 0;
106                         if (m is PropertyInfo)
107                                 return ((int)((PropertyInfo)m).Attributes & flags) != 0;
108                         if (m is EventInfo)
109                                 return ((int)((EventInfo)m).Attributes & flags) != 0;
110                         return false;
111                 }
112
113                 protected Type ()
114                 {
115                 }
116
117                 /// <summary>
118                 ///   The assembly where the type is defined.
119                 /// </summary>
120                 public abstract Assembly Assembly {
121                         get;
122                 }
123
124                 /// <summary>
125                 ///   Gets the fully qualified name for the type including the
126                 ///   assembly name where the type is defined.
127                 /// </summary>
128                 public abstract string AssemblyQualifiedName {
129                         get;
130                 }
131
132                 /// <summary>
133                 ///   Returns the Attributes associated with the type.
134                 /// </summary>
135                 public TypeAttributes Attributes {
136                         get {
137                                 return GetAttributeFlagsImpl ();
138                         }
139                 }
140
141                 /// <summary>
142                 ///   Returns the basetype for this type
143                 /// </summary>
144                 public abstract Type BaseType {
145                         get;
146                 }
147
148                 /// <summary>
149                 ///   Returns the class that declares the member.
150                 /// </summary>
151                 public override Type DeclaringType {
152                         get {
153                                 return null;
154                         }
155                 }
156
157                 /// <summary>
158                 ///
159                 /// </summary>
160                 public static Binder DefaultBinder {
161                         get {
162                                 return Binder.DefaultBinder;
163                         }
164                 }
165
166                 /// <summary>
167                 ///    The full name of the type including its namespace
168                 /// </summary>
169                 public abstract string FullName {
170                         get;
171                 }
172
173                 public abstract Guid GUID {
174                         get;
175                 }
176
177                 public bool HasElementType {
178                         get {
179                                 return HasElementTypeImpl ();
180                         }
181                 }
182
183                 public bool IsAbstract {
184                         get {
185                                 return (Attributes & TypeAttributes.Abstract) != 0;
186                         }
187                 }
188
189                 public bool IsAnsiClass {
190                         get {
191                                 return (Attributes & TypeAttributes.StringFormatMask)
192                                 == TypeAttributes.AnsiClass;
193                         }
194                 }
195
196                 public bool IsArray {
197                         get {
198                                 return IsArrayImpl ();
199                         }
200                 }
201
202                 public bool IsAutoClass {
203                         get {
204                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
205                         }
206                 }
207
208                 public bool IsAutoLayout {
209                         get {
210                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
211                         }
212                 }
213
214                 public bool IsByRef {
215                         get {
216                                 return IsByRefImpl ();
217                         }
218                 }
219
220                 public bool IsClass {
221                         get {
222                                 if (IsInterface)
223                                         return false;
224
225                                 return !IsValueType;
226                         }
227                 }
228
229                 public bool IsCOMObject {
230                         get {
231                                 return IsCOMObjectImpl ();
232                         }
233                 }
234                 
235 #if NET_4_5
236                 public virtual bool IsConstructedGenericType {
237                         get {
238                                 throw new NotImplementedException ();
239                         }
240                 }
241 #endif
242
243                 public bool IsContextful {
244                         get {
245                                 return IsContextfulImpl ();
246                         }
247                 }
248
249                 public
250 #if NET_4_0
251                 virtual
252 #endif
253                 bool IsEnum {
254                         get {
255                                 return IsSubclassOf (typeof (Enum));
256                         }
257                 }
258
259                 public bool IsExplicitLayout {
260                         get {
261                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
262                         }
263                 }
264
265                 public bool IsImport {
266                         get {
267                                 return (Attributes & TypeAttributes.Import) != 0;
268                         }
269                 }
270
271                 public bool IsInterface {
272                         get {
273                                 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
274                         }
275                 }
276
277                 public bool IsLayoutSequential {
278                         get {
279                                 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
280                         }
281                 }
282
283                 public bool IsMarshalByRef {
284                         get {
285                                 return IsMarshalByRefImpl ();
286                         }
287                 }
288
289                 public bool IsNestedAssembly {
290                         get {
291                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
292                         }
293                 }
294
295                 public bool IsNestedFamANDAssem {
296                         get {
297                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
298                         }
299                 }
300
301                 public bool IsNestedFamily {
302                         get {
303                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
304                         }
305                 }
306
307                 public bool IsNestedFamORAssem {
308                         get {
309                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
310                         }
311                 }
312
313                 public bool IsNestedPrivate {
314                         get {
315                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
316                         }
317                 }
318
319                 public bool IsNestedPublic {
320                         get {
321                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
322                         }
323                 }
324
325                 public bool IsNotPublic {
326                         get {
327                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
328                         }
329                 }
330
331                 public bool IsPointer {
332                         get {
333                                 return IsPointerImpl ();
334                         }
335                 }
336
337                 public bool IsPrimitive {
338                         get {
339                                 return IsPrimitiveImpl ();
340                         }
341                 }
342
343                 public bool IsPublic {
344                         get {
345                                 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
346                         }
347                 }
348
349                 public bool IsSealed {
350                         get {
351                                 return (Attributes & TypeAttributes.Sealed) != 0;
352                         }
353                 }
354
355                 public
356 #if NET_4_0
357                 virtual
358 #endif
359                 bool IsSerializable {
360                         get {
361                                 if ((Attributes & TypeAttributes.Serializable) != 0)
362                                         return true;
363
364                                 // Enums and delegates are always serializable
365
366                                 Type type = UnderlyingSystemType;
367                                 if (type == null)
368                                         return false;
369
370                                 // Fast check for system types
371                                 if (type.IsSystemType)
372                                         return type_is_subtype_of (type, typeof (Enum), false) || type_is_subtype_of (type, typeof (Delegate), false);
373
374                                 // User defined types depend on this behavior
375                                 do {
376                                         if ((type == typeof (Enum)) || (type == typeof (Delegate)))
377                                                 return true;
378
379                                         type = type.BaseType;
380                                 } while (type != null);
381
382                                 return false;
383                         }
384                 }
385
386                 public bool IsSpecialName {
387                         get {
388                                 return (Attributes & TypeAttributes.SpecialName) != 0;
389                         }
390                 }
391
392                 public bool IsUnicodeClass {
393                         get {
394                                 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
395                         }
396                 }
397
398                 public bool IsValueType {
399                         get {
400                                 return IsValueTypeImpl ();
401                         }
402                 }
403
404                 public override MemberTypes MemberType {
405                         get {
406                                 return MemberTypes.TypeInfo;
407                         }
408                 }
409
410                 public abstract override Module Module {
411                         get;
412                 }
413         
414                 public abstract string Namespace {get;}
415
416                 public override Type ReflectedType {
417                         get {
418                                 return null;
419                         }
420                 }
421
422                 public virtual RuntimeTypeHandle TypeHandle {
423                         get { throw new ArgumentException ("Derived class must provide implementation."); }
424                 }
425
426                 [ComVisible (true)]
427                 public ConstructorInfo TypeInitializer {
428                         get {
429                                 return GetConstructorImpl (
430                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
431                                         null,
432                                         CallingConventions.Any,
433                                         EmptyTypes,
434                                         null);
435                         }
436                 }
437
438                 /*
439                  * This has NOTHING to do with getting the base type of an enum. Use
440                  * Enum.GetUnderlyingType () for that.
441                  */
442                 public abstract Type UnderlyingSystemType {get;}
443
444                 public override bool Equals (object o)
445                 {
446 #if NET_4_0
447                         return Equals (o as Type);
448 #else
449                         if (o == this)
450                                 return true;
451
452                         Type me = UnderlyingSystemType;
453                         if (me == null)
454                                 return false;
455                         return me.EqualsInternal (o as Type);
456 #endif
457                 }
458
459 #if NET_4_0
460                 public virtual bool Equals (Type o)
461                 {
462                         if ((object)o == (object)this)
463                                 return true;
464                         if ((object)o == null)
465                                 return false;
466                         Type me = UnderlyingSystemType;
467                         if ((object)me == null)
468                                 return false;
469
470                         o = o.UnderlyingSystemType;
471                         if ((object)o == null)
472                                 return false;
473                         if ((object)o == (object)this)
474                                 return true;
475                         return me.EqualsInternal (o);
476                 }               
477 #else
478                 public bool Equals (Type o)
479                 {
480
481                         if (o == this)
482                                 return true;
483                         if (o == null)
484                                 return false;
485                         Type me = UnderlyingSystemType;
486                         if (me == null)
487                                 return false;
488                         return me.EqualsInternal (o.UnderlyingSystemType);
489                 }
490 #endif
491 #if NET_4_0
492                 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
493                 public static bool operator == (Type left, Type right)
494                 {
495                         return Object.ReferenceEquals (left, right);
496                 }
497
498                 [MonoTODO ("Implement it properly once 4.0 impl details are known.")]
499                 public static bool operator != (Type left, Type right)
500                 {
501                         return !Object.ReferenceEquals (left, right);
502                 }
503
504                 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
505                 public virtual Type GetEnumUnderlyingType () {
506                         if (!IsEnum)
507                                 throw new ArgumentException ("Type is not an enumeration", "enumType");
508
509                         var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
510
511                         if (fields == null || fields.Length != 1)
512                                 throw new ArgumentException ("An enum must have exactly one instance field", "enumType");
513
514                         return fields [0].FieldType;
515                 }
516
517                 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
518                 public virtual string[] GetEnumNames () {
519                         if (!IsEnum)
520                                 throw new ArgumentException ("Type is not an enumeration", "enumType");
521
522                         var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
523
524                         string [] names = new string [fields.Length];
525                         if (0 != names.Length) {
526                                 for (int i = 0; i < fields.Length; ++i)
527                                         names [i] = fields [i].Name;
528                                         
529                                 var et = GetEnumUnderlyingType ();
530                                 var values = Array.CreateInstance (et, names.Length);
531                                 for (int i = 0; i < fields.Length; ++i)
532                                         values.SetValue (fields [i].GetValue (null), i);
533                                 MonoEnumInfo.SortEnums (et, values, names);
534                         }
535
536                         return names;
537                 }
538
539                 static NotImplementedException CreateNIE () {
540                         return new NotImplementedException ();
541                 }
542
543                 public virtual Array GetEnumValues () {
544                         if (!IsEnum)
545                                 throw new ArgumentException ("Type is not an enumeration", "enumType");
546
547                         throw CreateNIE ();
548                 }
549
550                 bool IsValidEnumType (Type type) {
551                         return (type.IsPrimitive && type != typeof (bool) && type != typeof (double) && type != typeof (float)) || type.IsEnum;
552                 }
553
554                 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
555                 public virtual string GetEnumName (object value) {
556                         if (value == null)
557                                 throw new ArgumentException ("Value is null", "value");
558                         if (!IsValidEnumType (value.GetType ()))
559                                 throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
560                         if (!IsEnum)
561                                 throw new ArgumentException ("Type is not an enumeration", "enumType");
562
563                         object obj = null;
564                         var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
565                         
566                         for (int i = 0; i < fields.Length; ++i) {
567                                 var fv = fields [i].GetValue (null);
568                                 if (obj == null) {
569                                         try {
570                                                 //XXX we can't use 'this' as argument as it might be an UserType
571                                                 obj = Enum.ToObject (fv.GetType (), value);
572                                         } catch (OverflowException) {
573                                                 return null;
574                                         } catch (InvalidCastException) {
575                                                 throw new ArgumentException ("Value is not valid", "value");
576                                         }
577                                 }
578                                 if (fv.Equals (obj))
579                                         return fields [i].Name;
580                         }
581
582                         return null;
583                 }
584
585                 [MonoInternalNote ("Reimplement this in MonoType for bonus speed")]
586                 public virtual bool IsEnumDefined (object value) {
587                         if (value == null)
588                                 throw new ArgumentException ("Value is null", "value");
589                         if (!IsEnum)
590                                 throw new ArgumentException ("Type is not an enumeration", "enumType");
591
592                         Type vt = value.GetType ();
593                         if (!IsValidEnumType (vt) && vt != typeof (string))
594                                 throw new InvalidOperationException ("Value is not the enum or a valid enum underlying type");
595
596                         var fields = GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
597
598                         if (value is string) {
599                                 for (int i = 0; i < fields.Length; ++i) {
600                                         if (fields [i].Name.Equals (value))
601                                                 return true;
602                                 }
603                         } else {
604                                 if (vt != this && vt != GetEnumUnderlyingType ())
605                                         throw new ArgumentException ("Value is not the enum or a valid enum underlying type", "value");
606
607                                 object obj = null;
608                                 for (int i = 0; i < fields.Length; ++i) {
609                                         var fv = fields [i].GetValue (null);
610                                         if (obj == null) {
611                                                 try {
612                                                         //XXX we can't use 'this' as argument as it might be an UserType
613                                                         obj = Enum.ToObject (fv.GetType (), value);
614                                                 } catch (OverflowException) {
615                                                         return false;
616                                                 } catch (InvalidCastException) {
617                                                         throw new ArgumentException ("Value is not valid", "value");
618                                                 }
619                                         }
620                                         if (fv.Equals (obj))
621                                                 return true;
622                                 }
623                         }
624                         return false;
625                 }
626         
627                 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver)
628                 {
629                         return GetType (typeName, assemblyResolver, typeResolver, false, false);
630                 }
631         
632                 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError)
633                 {
634                         return GetType (typeName, assemblyResolver, typeResolver, throwOnError, false);
635                 }
636         
637                 public static Type GetType (string typeName, Func<AssemblyName,Assembly> assemblyResolver, Func<Assembly,string,bool,Type> typeResolver, bool throwOnError, bool ignoreCase)
638                 {
639                         TypeSpec spec = TypeSpec.Parse (typeName);
640                         return spec.Resolve (assemblyResolver, typeResolver, throwOnError, ignoreCase);
641                 }
642
643                 public virtual bool IsSecurityTransparent
644                 {
645                         get { throw CreateNIE (); }
646                 }
647
648                 public virtual bool IsSecurityCritical
649                 {
650                         get { throw CreateNIE (); }
651                 }
652
653                 public virtual bool IsSecuritySafeCritical
654                 {
655                         get { throw CreateNIE (); }
656                 }
657 #endif
658                 
659                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
660                 internal extern bool EqualsInternal (Type type);
661                 
662                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
663                 private static extern Type internal_from_handle (IntPtr handle);
664                 
665                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
666                 private static extern Type internal_from_name (string name, bool throwOnError, bool ignoreCase);
667
668                 public static Type GetType(string typeName)
669                 {
670                         if (typeName == null)
671                                 throw new ArgumentNullException ("TypeName");
672
673                         return internal_from_name (typeName, false, false);
674                 }
675
676                 public static Type GetType(string typeName, bool throwOnError)
677                 {
678                         if (typeName == null)
679                                 throw new ArgumentNullException ("TypeName");
680
681                         Type type = internal_from_name (typeName, throwOnError, false);
682                         if (throwOnError && type == null)
683                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
684
685                         return type;
686                 }
687
688                 public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
689                 {
690                         if (typeName == null)
691                                 throw new ArgumentNullException ("TypeName");
692
693                         Type t = internal_from_name (typeName, throwOnError, ignoreCase);
694                         if (throwOnError && t == null)
695                                 throw new TypeLoadException ("Error loading '" + typeName + "'");
696
697                         return t;
698                 }
699
700                 public static Type[] GetTypeArray (object[] args) {
701                         if (args == null)
702                                 throw new ArgumentNullException ("args");
703
704                         Type[] ret;
705                         ret = new Type [args.Length];
706                         for (int i = 0; i < args.Length; ++i)
707                                 ret [i] = args[i].GetType ();
708                         return ret;
709                 }
710
711                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
712                 internal extern static TypeCode GetTypeCodeInternal (Type type);
713
714 #if NET_4_0
715                 protected virtual
716 #endif
717                 TypeCode GetTypeCodeImpl () {
718                         Type type = this;
719                         if (type is MonoType)
720                                 return GetTypeCodeInternal (type);
721 #if !FULL_AOT_RUNTIME
722                         if (type is TypeBuilder)
723                                 return ((TypeBuilder)type).GetTypeCodeInternal ();
724 #endif
725
726                         type = type.UnderlyingSystemType;
727
728                         if (!type.IsSystemType)
729                                 return TypeCode.Object;
730                         else
731                                 return GetTypeCodeInternal (type);
732                 }
733
734                 public static TypeCode GetTypeCode (Type type) {
735                         if (type == null)
736                                 /* MS.NET returns this */
737                                 return TypeCode.Empty;
738                         return type.GetTypeCodeImpl ();
739                 }
740
741 #if !FULL_AOT_RUNTIME
742                 private static Dictionary<Guid, Type> clsid_types;
743 #if !FULL_AOT_RUNTIME
744                 private static AssemblyBuilder clsid_assemblybuilder;
745 #endif
746
747                 [MonoTODO("COM servers only work on Windows")]
748                 public static Type GetTypeFromCLSID (Guid clsid)
749                 {
750                         return GetTypeFromCLSID (clsid, null, true);
751                 }
752
753                 [MonoTODO("COM servers only work on Windows")]
754                 public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError)
755                 {
756                         return GetTypeFromCLSID (clsid, null, throwOnError);
757                 }
758
759                 [MonoTODO("COM servers only work on Windows")]
760                 public static Type GetTypeFromCLSID (Guid clsid, string server)
761                 {
762                         return GetTypeFromCLSID (clsid, server, true);
763                 }
764
765                 [MonoTODO("COM servers only work on Windows")]
766                 public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError)
767                 {
768 #if !FULL_AOT_RUNTIME
769                         Type result;
770
771                         if (clsid_types == null)
772                         {
773                                 Dictionary<Guid, Type> new_clsid_types = new Dictionary<Guid, Type> ();
774                                 Interlocked.CompareExchange<Dictionary<Guid, Type>>(
775                                         ref clsid_types, new_clsid_types, null);
776                         }
777
778                         lock (clsid_types) {
779                                 if (clsid_types.TryGetValue(clsid, out result))
780                                         return result;
781
782                                 if (clsid_assemblybuilder == null)
783                                 {
784                                         AssemblyName assemblyname = new AssemblyName ();
785                                         assemblyname.Name = "GetTypeFromCLSIDDummyAssembly";
786                                         clsid_assemblybuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (
787                                                 assemblyname, AssemblyBuilderAccess.Run);
788                                 }
789                                 ModuleBuilder modulebuilder = clsid_assemblybuilder.DefineDynamicModule (
790                                         clsid.ToString ());
791
792                                 TypeBuilder typebuilder = modulebuilder.DefineType ("System.__ComObject",
793                                         TypeAttributes.Public | TypeAttributes.Class, typeof(System.__ComObject));
794
795                                 Type[] guidattrtypes = new Type[] { typeof(string) };
796
797                                 CustomAttributeBuilder customattr = new CustomAttributeBuilder (
798                                         typeof(GuidAttribute).GetConstructor (guidattrtypes),
799                                         new object[] { clsid.ToString () });
800
801                                 typebuilder.SetCustomAttribute (customattr);
802
803                                 customattr = new CustomAttributeBuilder (
804                                         typeof(ComImportAttribute).GetConstructor (EmptyTypes),
805                                         new object[0] {});
806
807                                 typebuilder.SetCustomAttribute (customattr);
808
809                                 result = typebuilder.CreateType ();
810
811                                 clsid_types.Add(clsid, result);
812
813                                 return result;
814                         }
815 #else
816                         throw new NotImplementedException ();
817 #endif
818                 }
819 #endif
820                 public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
821                 {
822                         if (handle.Value == IntPtr.Zero)
823                                 // This is not consistent with the other GetXXXFromHandle methods, but
824                                 // MS.NET seems to do this
825                                 return null;
826
827                         return internal_from_handle (handle.Value);
828                 }
829
830                 [MonoTODO("Mono does not support COM")]
831                 public static Type GetTypeFromProgID (string progID)
832                 {
833                         throw new NotImplementedException ();
834                 }
835
836                 [MonoTODO("Mono does not support COM")]
837                 public static Type GetTypeFromProgID (string progID, bool throwOnError)
838                 {
839                         throw new NotImplementedException ();
840                 }
841
842                 [MonoTODO("Mono does not support COM")]
843                 public static Type GetTypeFromProgID (string progID, string server)
844                 {
845                         throw new NotImplementedException ();
846                 }
847
848                 [MonoTODO("Mono does not support COM")]
849                 public static Type GetTypeFromProgID (string progID, string server, bool throwOnError)
850                 {
851                         throw new NotImplementedException ();
852                 }
853
854                 public static RuntimeTypeHandle GetTypeHandle (object o)
855                 {
856                         if (o == null)
857                                 throw new ArgumentNullException ();
858
859                         return o.GetType().TypeHandle;
860                 }
861
862                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
863                 internal static extern bool type_is_subtype_of (Type a, Type b, bool check_interfaces);
864
865                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
866                 internal static extern bool type_is_assignable_from (Type a, Type b);
867
868                 public new Type GetType ()
869                 {
870                         return base.GetType ();
871                 }
872
873                 [ComVisible (true)]
874                 public virtual bool IsSubclassOf (Type c)
875                 {
876                         if (c == null || c == this)
877                                 return false;
878
879                         // Fast check for system types
880                         if (IsSystemType)
881                                 return c.IsSystemType && type_is_subtype_of (this, c, false);
882
883                         // User defined types depend on this behavior
884                         for (Type type = BaseType; type != null; type = type.BaseType)
885                                 if (type == c)
886                                         return true;
887
888                         return false;
889                 }
890
891                 public virtual Type[] FindInterfaces (TypeFilter filter, object filterCriteria)
892                 {
893                         if (filter == null)
894                                 throw new ArgumentNullException ("filter");
895
896                         var ifaces = new List<Type> ();
897                         foreach (Type iface in GetInterfaces ()) {
898                                 if (filter (iface, filterCriteria))
899                                         ifaces.Add (iface);
900                         }
901
902                         return ifaces.ToArray ();
903                 }
904                 
905                 public Type GetInterface (string name) {
906                         return GetInterface (name, false);
907                 }
908
909                 public abstract Type GetInterface (string name, bool ignoreCase);
910
911                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
912                 internal static extern void GetInterfaceMapData (Type t, Type iface, out MethodInfo[] targets, out MethodInfo[] methods);
913
914                 [ComVisible (true)]
915                 public virtual InterfaceMapping GetInterfaceMap (Type interfaceType) {
916                         if (!IsSystemType)
917                                 throw new NotSupportedException ("Derived classes must provide an implementation.");
918                         if (interfaceType == null)
919                                 throw new ArgumentNullException ("interfaceType");
920                         if (!interfaceType.IsSystemType)
921                                 throw new ArgumentException ("interfaceType", "Type is an user type");
922                         InterfaceMapping res;
923                         if (!interfaceType.IsInterface)
924                                 throw new ArgumentException (Locale.GetText ("Argument must be an interface."), "interfaceType");
925                         if (IsInterface)
926                                 throw new ArgumentException ("'this' type cannot be an interface itself");
927                         res.TargetType = this;
928                         res.InterfaceType = interfaceType;
929                         GetInterfaceMapData (this, interfaceType, out res.TargetMethods, out res.InterfaceMethods);
930                         if (res.TargetMethods == null)
931                                 throw new ArgumentException (Locale.GetText ("Interface not found"), "interfaceType");
932
933                         return res;
934                 }
935
936                 public abstract Type[] GetInterfaces ();
937
938                 public virtual bool IsAssignableFrom (Type c)
939                 {
940                         if (c == null)
941                                 return false;
942
943                         if (Equals (c))
944                                 return true;
945
946 #if !FULL_AOT_RUNTIME
947                         if (c is TypeBuilder)
948                                 return ((TypeBuilder)c).IsAssignableTo (this);
949 #endif
950
951                         /* Handle user defined type classes */
952                         if (!IsSystemType) {
953                                 Type systemType = UnderlyingSystemType;
954                                 if (!systemType.IsSystemType)
955                                         return false;
956
957                                 Type other = c.UnderlyingSystemType;
958                                 if (!other.IsSystemType)
959                                         return false;
960
961                                 return systemType.IsAssignableFrom (other);
962                         }
963
964                         if (!c.IsSystemType) {
965                                 Type underlyingType = c.UnderlyingSystemType;
966                                 if (!underlyingType.IsSystemType)
967                                         return false;
968                                 return IsAssignableFrom (underlyingType);
969                         }
970
971                         return type_is_assignable_from (this, c);
972                 }
973
974                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
975                 extern static bool IsInstanceOfType (Type type, object o);
976
977                 public virtual bool IsInstanceOfType (object o)
978                 {
979                         Type type = UnderlyingSystemType;
980                         if (!type.IsSystemType)
981                                 return false;
982                         return IsInstanceOfType (type, o);
983                 }
984
985                 public virtual int GetArrayRank ()
986                 {
987                         throw new NotSupportedException ();     // according to MSDN
988                 }
989
990                 public abstract Type GetElementType ();
991
992                 public EventInfo GetEvent (string name)
993                 {
994                         return GetEvent (name, DefaultBindingFlags);
995                 }
996
997                 public abstract EventInfo GetEvent (string name, BindingFlags bindingAttr);
998
999                 public virtual EventInfo[] GetEvents ()
1000                 {
1001                         return GetEvents (DefaultBindingFlags);
1002                 }
1003
1004                 public abstract EventInfo[] GetEvents (BindingFlags bindingAttr);
1005
1006                 public FieldInfo GetField( string name)
1007                 {
1008                         return GetField (name, DefaultBindingFlags);
1009                 }
1010
1011                 public abstract FieldInfo GetField( string name, BindingFlags bindingAttr);
1012
1013                 public FieldInfo[] GetFields ()
1014                 {
1015                         return GetFields (DefaultBindingFlags);
1016                 }
1017
1018                 public abstract FieldInfo[] GetFields (BindingFlags bindingAttr);
1019                 
1020                 public override int GetHashCode()
1021                 {
1022                         Type t = UnderlyingSystemType;
1023                         if (t != null && t != this)
1024                                 return t.GetHashCode ();
1025                         return (int)_impl.Value;
1026                 }
1027
1028                 public MemberInfo[] GetMember (string name)
1029                 {
1030                         return GetMember (name, MemberTypes.All, DefaultBindingFlags);
1031                 }
1032                 
1033                 public virtual MemberInfo[] GetMember (string name, BindingFlags bindingAttr)
1034                 {
1035                         return GetMember (name, MemberTypes.All, bindingAttr);
1036                 }
1037
1038                 public virtual MemberInfo[] GetMember (string name, MemberTypes type, BindingFlags bindingAttr)
1039                 {
1040                         if (name == null)
1041                                 throw new ArgumentNullException ("name");
1042                         if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
1043                                 return FindMembers (type, bindingAttr, FilterNameIgnoreCase, name);
1044                         else
1045                                 return FindMembers (type, bindingAttr, FilterName, name);
1046                 }
1047
1048                 public MemberInfo[] GetMembers ()
1049                 {
1050                         return GetMembers (DefaultBindingFlags);
1051                 }
1052
1053                 public abstract MemberInfo[] GetMembers (BindingFlags bindingAttr);
1054
1055                 public MethodInfo GetMethod (string name)
1056                 {
1057                         if (name == null)
1058                                 throw new ArgumentNullException ("name");
1059                         return GetMethodImpl (name, DefaultBindingFlags, null, CallingConventions.Any, null, null);
1060                 }
1061
1062                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr)
1063                 {
1064                         if (name == null)
1065                                 throw new ArgumentNullException ("name");
1066                         
1067                         return GetMethodImpl (name, bindingAttr, null, CallingConventions.Any, null, null);
1068                 }
1069                 
1070                 public MethodInfo GetMethod (string name, Type[] types)
1071                 {
1072                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, null);
1073                 }
1074
1075                 public MethodInfo GetMethod (string name, Type[] types, ParameterModifier[] modifiers)
1076                 {
1077                         return GetMethod (name, DefaultBindingFlags, null, CallingConventions.Any, types, modifiers);
1078                 }
1079
1080                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1081                                              Type[] types, ParameterModifier[] modifiers)
1082                 {
1083                         return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
1084                 }
1085
1086                 public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
1087                                              CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
1088                 {
1089                         if (name == null)
1090                                 throw new ArgumentNullException ("name");
1091                         if (types == null)
1092                                 throw new ArgumentNullException ("types");
1093
1094                         for (int i = 0; i < types.Length; i++) 
1095                                 if (types[i] == null)
1096                                         throw new ArgumentNullException ("types");
1097
1098                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1099                 }
1100
1101                 protected abstract MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr, Binder binder,
1102                                                              CallingConventions callConvention, Type[] types,
1103                                                              ParameterModifier[] modifiers);
1104
1105                 internal MethodInfo GetMethodImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1106                                                                                                                         CallingConventions callConvention, Type[] types,
1107                                                                                                                         ParameterModifier[] modifiers)
1108                 {
1109                         return GetMethodImpl (name, bindingAttr, binder, callConvention, types, modifiers);
1110                 }
1111
1112                 internal virtual MethodInfo GetMethod (MethodInfo fromNoninstanciated)
1113                 {
1114                         throw new System.InvalidOperationException ("can only be called in generic type");
1115                 }
1116
1117                 internal virtual ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
1118                 {
1119                         throw new System.InvalidOperationException ("can only be called in generic type");
1120                 }
1121
1122                 internal virtual FieldInfo GetField (FieldInfo fromNoninstanciated)
1123                 {
1124                         throw new System.InvalidOperationException ("can only be called in generic type");
1125                 }
1126
1127                 
1128                 public MethodInfo[] GetMethods ()
1129                 {
1130                         return GetMethods (DefaultBindingFlags);
1131                 }
1132
1133                 public abstract MethodInfo[] GetMethods (BindingFlags bindingAttr);
1134
1135                 public Type GetNestedType (string name)
1136                 {
1137                         return GetNestedType (name, DefaultBindingFlags);
1138                 }
1139
1140                 public abstract Type GetNestedType (string name, BindingFlags bindingAttr);
1141
1142                 public Type[] GetNestedTypes ()
1143                 {
1144                         return GetNestedTypes (DefaultBindingFlags);
1145                 }
1146
1147                 public abstract Type[] GetNestedTypes (BindingFlags bindingAttr);
1148
1149
1150                 public PropertyInfo[] GetProperties ()
1151                 {
1152                         return GetProperties (DefaultBindingFlags);
1153                 }
1154
1155                 public abstract PropertyInfo[] GetProperties (BindingFlags bindingAttr);
1156
1157
1158                 public PropertyInfo GetProperty (string name)
1159                 {
1160                         if (name == null)
1161                                 throw new ArgumentNullException ("name");
1162
1163                         return GetPropertyImpl (name, DefaultBindingFlags, null, null, null, null);
1164                 }
1165
1166                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr)
1167                 {
1168                         if (name == null)
1169                                 throw new ArgumentNullException ("name");
1170                         return GetPropertyImpl (name, bindingAttr, null, null, null, null);
1171                 }
1172
1173                 public PropertyInfo GetProperty (string name, Type returnType)
1174                 {
1175                         if (name == null)
1176                                 throw new ArgumentNullException ("name");
1177                         return GetPropertyImpl (name, DefaultBindingFlags, null, returnType, null, null);
1178                 }
1179
1180                 public PropertyInfo GetProperty (string name, Type[] types)
1181                 {
1182                         return GetProperty (name, DefaultBindingFlags, null, null, types, null);
1183                 }
1184
1185                 public PropertyInfo GetProperty (string name, Type returnType, Type[] types)
1186                 {
1187                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, null);
1188                 }
1189
1190                 public PropertyInfo GetProperty( string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1191                 {
1192                         return GetProperty (name, DefaultBindingFlags, null, returnType, types, modifiers);
1193                 }
1194
1195                 public PropertyInfo GetProperty (string name, BindingFlags bindingAttr, Binder binder, Type returnType,
1196                                                  Type[] types, ParameterModifier[] modifiers)
1197                 {
1198                         if (name == null)
1199                                 throw new ArgumentNullException ("name");
1200                         if (types == null)
1201                                 throw new ArgumentNullException ("types");
1202
1203                         foreach (Type t in types) {
1204                                 if (t == null)
1205                                         throw new ArgumentNullException ("types");
1206                         }
1207
1208                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1209                 }
1210
1211                 protected abstract PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr, Binder binder,
1212                                                                  Type returnType, Type[] types, ParameterModifier[] modifiers);
1213
1214                 internal PropertyInfo GetPropertyImplInternal (string name, BindingFlags bindingAttr, Binder binder,
1215                                                                                                            Type returnType, Type[] types, ParameterModifier[] modifiers)
1216                 {
1217                         return GetPropertyImpl (name, bindingAttr, binder, returnType, types, modifiers);
1218                 }
1219
1220                 protected abstract ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
1221                                                                        Binder binder,
1222                                                                        CallingConventions callConvention,
1223                                                                        Type[] types,
1224                                                                        ParameterModifier[] modifiers);
1225
1226                 protected abstract TypeAttributes GetAttributeFlagsImpl ();
1227                 protected abstract bool HasElementTypeImpl ();
1228                 protected abstract bool IsArrayImpl ();
1229                 protected abstract bool IsByRefImpl ();
1230                 protected abstract bool IsCOMObjectImpl ();
1231                 protected abstract bool IsPointerImpl ();
1232                 protected abstract bool IsPrimitiveImpl ();
1233                 
1234                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1235                 internal static extern bool IsArrayImpl (Type type);
1236
1237                 protected virtual bool IsValueTypeImpl ()
1238                 {
1239                         if (this == typeof (ValueType) || this == typeof (Enum))
1240                                 return false;
1241
1242                         return IsSubclassOf (typeof (ValueType));
1243                 }
1244                 
1245                 protected virtual bool IsContextfulImpl ()
1246                 {
1247                         return typeof (ContextBoundObject).IsAssignableFrom (this);
1248                 }
1249
1250                 protected virtual bool IsMarshalByRefImpl ()
1251                 {
1252                         return typeof (MarshalByRefObject).IsAssignableFrom (this);
1253                 }
1254
1255                 [ComVisible (true)]
1256                 public ConstructorInfo GetConstructor (Type[] types)
1257                 {
1258                         return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
1259                 }
1260
1261                 [ComVisible (true)]
1262                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1263                                                        Type[] types, ParameterModifier[] modifiers)
1264                 {
1265                         return GetConstructor (bindingAttr, binder, CallingConventions.Any, types, modifiers);
1266                 }
1267
1268                 [ComVisible (true)]
1269                 public ConstructorInfo GetConstructor (BindingFlags bindingAttr, Binder binder,
1270                                                        CallingConventions callConvention,
1271                                                        Type[] types, ParameterModifier[] modifiers)
1272                 {
1273                         if (types == null)
1274                                 throw new ArgumentNullException ("types");
1275
1276                         foreach (Type t in types) {
1277                                 if (t == null)
1278                                         throw new ArgumentNullException ("types");
1279                         }
1280
1281                         return GetConstructorImpl (bindingAttr, binder, callConvention, types, modifiers);
1282                 }
1283
1284                 [ComVisible (true)]
1285                 public ConstructorInfo[] GetConstructors ()
1286                 {
1287                         return GetConstructors (BindingFlags.Public | BindingFlags.Instance);
1288                 }
1289
1290                 [ComVisible (true)]
1291                 public abstract ConstructorInfo[] GetConstructors (BindingFlags bindingAttr);
1292
1293                 public virtual MemberInfo[] GetDefaultMembers ()
1294                 {
1295                         object [] att = GetCustomAttributes (typeof (DefaultMemberAttribute), true);
1296                         if (att.Length == 0)
1297                                 return EmptyArray<MemberInfo>.Value;
1298
1299                         MemberInfo [] member = GetMember (((DefaultMemberAttribute) att [0]).MemberName);
1300                         return (member != null) ? member : EmptyArray<MemberInfo>.Value;
1301                 }
1302
1303                 public virtual MemberInfo[] FindMembers (MemberTypes memberType, BindingFlags bindingAttr,
1304                                                          MemberFilter filter, object filterCriteria)
1305                 {
1306                         MemberInfo[] result;
1307                         ArrayList l = new ArrayList ();
1308
1309                         // Console.WriteLine ("FindMembers for {0} (Type: {1}): {2}",
1310                         // this.FullName, this.GetType().FullName, this.obj_address());
1311                         if ((memberType & MemberTypes.Method) != 0) {
1312                                 MethodInfo[] c = GetMethods (bindingAttr);
1313                                 if (filter != null) {
1314                                         foreach (MemberInfo m in c) {
1315                                                 if (filter (m, filterCriteria))
1316                                                         l.Add (m);
1317                                         }
1318                                 } else {
1319                                         l.AddRange (c);
1320                                 }
1321                         }
1322                         if ((memberType & MemberTypes.Constructor) != 0) {
1323                                 ConstructorInfo[] c = GetConstructors (bindingAttr);
1324                                 if (filter != null) {
1325                                         foreach (MemberInfo m in c) {
1326                                                 if (filter (m, filterCriteria))
1327                                                         l.Add (m);
1328                                         }
1329                                 } else {
1330                                         l.AddRange (c);
1331                                 }
1332                         }
1333                         if ((memberType & MemberTypes.Property) != 0) {
1334                                 PropertyInfo[] c = GetProperties (bindingAttr);
1335
1336
1337                                 if (filter != null) {
1338                                         foreach (MemberInfo m in c) {
1339                                                 if (filter (m, filterCriteria))
1340                                                         l.Add (m);
1341                                         }
1342                                 } else {
1343                                         l.AddRange (c);
1344                                 }
1345
1346                         }
1347                         if ((memberType & MemberTypes.Event) != 0) {
1348                                 EventInfo[] c = GetEvents (bindingAttr);
1349                                 if (filter != null) {
1350                                         foreach (MemberInfo m in c) {
1351                                                 if (filter (m, filterCriteria))
1352                                                         l.Add (m);
1353                                         }
1354                                 } else {
1355                                         l.AddRange (c);
1356                                 }
1357                         }
1358                         if ((memberType & MemberTypes.Field) != 0) {
1359                                 FieldInfo[] c = GetFields (bindingAttr);
1360                                 if (filter != null) {
1361                                         foreach (MemberInfo m in c) {
1362                                                 if (filter (m, filterCriteria))
1363                                                         l.Add (m);
1364                                         }
1365                                 } else {
1366                                         l.AddRange (c);
1367                                 }
1368                         }
1369                         if ((memberType & MemberTypes.NestedType) != 0) {
1370                                 Type[] c = GetNestedTypes (bindingAttr);
1371                                 if (filter != null) {
1372                                         foreach (MemberInfo m in c) {
1373                                                 if (filter (m, filterCriteria)) {
1374                                                         l.Add (m);
1375                                                 }
1376                                         }
1377                                 } else {
1378                                         l.AddRange (c);
1379                                 }
1380                         }
1381
1382                         switch (memberType) {
1383                         case MemberTypes.Constructor :
1384                                 result = new ConstructorInfo [l.Count];
1385                                 break;
1386                         case MemberTypes.Event :
1387                                 result = new EventInfo [l.Count];
1388                                 break;
1389                         case MemberTypes.Field :
1390                                 result = new FieldInfo [l.Count];
1391                                 break;
1392                         case MemberTypes.Method :
1393                                 result = new MethodInfo [l.Count];
1394                                 break;
1395                         case MemberTypes.NestedType :
1396                         case MemberTypes.TypeInfo :
1397                                 result = new Type [l.Count];
1398                                 break;
1399                         case MemberTypes.Property :
1400                                 result = new PropertyInfo [l.Count];
1401                                 break;
1402                         default :
1403                                 result = new MemberInfo [l.Count];
1404                                 break;
1405                         }
1406                         l.CopyTo (result);
1407                         return result;
1408                 }
1409
1410                 [DebuggerHidden]
1411                 [DebuggerStepThrough] 
1412                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder, object target, object[] args)
1413                 {
1414                         return InvokeMember (name, invokeAttr, binder, target, args, null, null, null);
1415                 }
1416
1417                 [DebuggerHidden]
1418                 [DebuggerStepThrough] 
1419                 public object InvokeMember (string name, BindingFlags invokeAttr, Binder binder,
1420                                             object target, object[] args, CultureInfo culture)
1421                 {
1422                         return InvokeMember (name, invokeAttr, binder, target, args, null, culture, null);
1423                 }
1424
1425                 public abstract object InvokeMember (string name, BindingFlags invokeAttr,
1426                                                      Binder binder, object target, object[] args,
1427                                                      ParameterModifier[] modifiers,
1428                                                      CultureInfo culture, string[] namedParameters);
1429
1430                 public override string ToString()
1431                 {
1432                         return FullName;
1433                 }
1434
1435                 internal virtual Type InternalResolve ()
1436                 {
1437                         return UnderlyingSystemType;
1438                 }
1439
1440                 internal bool IsSystemType {
1441                         get {
1442                                 return _impl.Value != IntPtr.Zero;
1443                         }
1444                 }
1445                 
1446 #if NET_4_5
1447                 public virtual Type[] GenericTypeArguments {
1448                         get {
1449                                 return IsGenericType ? GetGenericArguments () : EmptyTypes;
1450                         }
1451                 }
1452 #endif
1453
1454                 public virtual Type[] GetGenericArguments ()
1455                 {
1456                         throw new NotSupportedException ();
1457                 }
1458
1459                 public virtual bool ContainsGenericParameters {
1460                         get { return false; }
1461                 }
1462
1463                 public virtual extern bool IsGenericTypeDefinition {
1464                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1465                         get;
1466                 }
1467
1468                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1469                 internal extern Type GetGenericTypeDefinition_impl ();
1470
1471                 public virtual Type GetGenericTypeDefinition ()
1472                 {
1473                         throw new NotSupportedException ("Derived classes must provide an implementation.");
1474                 }
1475
1476                 public virtual extern bool IsGenericType {
1477                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
1478                         get;
1479                 }
1480                 
1481                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1482                 static extern Type MakeGenericType (Type gt, Type [] types);
1483
1484                 public virtual Type MakeGenericType (params Type[] typeArguments)
1485                 {
1486                         if (IsUserType)
1487                                 throw new NotSupportedException ();
1488                         if (!IsGenericTypeDefinition)
1489                                 throw new InvalidOperationException ("not a generic type definition");
1490                         if (typeArguments == null)
1491                                 throw new ArgumentNullException ("typeArguments");
1492                         if (GetGenericArguments().Length != typeArguments.Length)
1493                                 throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", GetGenericArguments ().Length, typeArguments.Length), "typeArguments");
1494
1495                         bool hasUserType = false;
1496
1497                         Type[] systemTypes = new Type[typeArguments.Length];
1498                         for (int i = 0; i < typeArguments.Length; ++i) {
1499                                 Type t = typeArguments [i];
1500                                 if (t == null)
1501                                         throw new ArgumentNullException ("typeArguments");
1502
1503                                 if (!(t is MonoType))
1504                                         hasUserType = true;
1505                                 systemTypes [i] = t;
1506                         }
1507
1508                         if (hasUserType) {
1509 #if FULL_AOT_RUNTIME
1510                                 throw new NotSupportedException ("User types are not supported under full aot");
1511 #else
1512                                 return new MonoGenericClass (this, typeArguments);
1513 #endif
1514                         }
1515
1516                         Type res = MakeGenericType (this, systemTypes);
1517                         if (res == null)
1518                                 throw new TypeLoadException ();
1519                         return res;
1520                 }
1521
1522                 public virtual bool IsGenericParameter {
1523                         get {
1524                                 return false;
1525                         }
1526                 }
1527
1528                 public bool IsNested {
1529                         get {
1530                                 return DeclaringType != null;
1531                         }
1532                 }
1533
1534                 public bool IsVisible {
1535                         get {
1536                                 if (IsNestedPublic)
1537                                         return DeclaringType.IsVisible;
1538
1539                                 return IsPublic;
1540                         }
1541                 }
1542
1543                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1544                 extern int GetGenericParameterPosition ();
1545                 
1546                 public virtual int GenericParameterPosition {
1547                         get {
1548                                 int res = GetGenericParameterPosition ();
1549                                 if (res < 0)
1550                                         throw new InvalidOperationException ();
1551                                 return res;
1552                         }
1553                 }
1554
1555                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1556                 extern GenericParameterAttributes GetGenericParameterAttributes ();
1557
1558                 public virtual GenericParameterAttributes GenericParameterAttributes {
1559                         get {
1560                                 if (!IsSystemType)
1561                                         throw new NotSupportedException ("Derived classes must provide an implementation.");
1562
1563                                 if (!IsGenericParameter)
1564                                         throw new InvalidOperationException ();
1565
1566                                 return GetGenericParameterAttributes ();
1567                         }
1568                 }
1569
1570                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1571                 extern Type[] GetGenericParameterConstraints_impl ();
1572
1573                 public virtual Type[] GetGenericParameterConstraints ()
1574                 {
1575                         if (!IsSystemType)
1576                                 throw new InvalidOperationException ();
1577
1578                         if (!IsGenericParameter)
1579                                 throw new InvalidOperationException ();
1580
1581                         return GetGenericParameterConstraints_impl ();
1582                 }
1583
1584                 public virtual MethodBase DeclaringMethod {
1585                         get {
1586                                 return null;
1587                         }
1588                 }
1589
1590                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1591                 extern Type make_array_type (int rank);
1592
1593                 public virtual Type MakeArrayType ()
1594                 {
1595                         if (!IsSystemType)
1596                                 throw new NotSupportedException ("Derived classes must provide an implementation.");
1597                         return make_array_type (0);
1598                 }
1599
1600                 public virtual Type MakeArrayType (int rank)
1601                 {
1602                         if (!IsSystemType)
1603                                 throw new NotSupportedException ("Derived classes must provide an implementation.");
1604                         if (rank < 1 || rank > 255)
1605                                 throw new IndexOutOfRangeException ();
1606                         return make_array_type (rank);
1607                 }
1608
1609                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1610                 extern Type make_byref_type ();
1611
1612                 public virtual Type MakeByRefType ()
1613                 {
1614                         if (!IsSystemType)
1615                                 throw new NotSupportedException ("Derived classes must provide an implementation.");
1616                         if (IsByRef)
1617                                 throw new TypeLoadException ("Can not call MakeByRefType on a ByRef type");
1618                         return make_byref_type ();
1619                 }
1620
1621                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1622                 static extern Type MakePointerType (Type type);
1623
1624                 public virtual Type MakePointerType ()
1625                 {
1626                         if (!IsSystemType)
1627                                 throw new NotSupportedException ("Derived classes must provide an implementation.");
1628                         return MakePointerType (this);
1629                 }
1630
1631                 public static Type ReflectionOnlyGetType (string typeName, 
1632                                                           bool throwIfNotFound, 
1633                                                           bool ignoreCase)
1634                 {
1635                         if (typeName == null)
1636                                 throw new ArgumentNullException ("typeName");
1637                         int idx = typeName.IndexOf (',');
1638                         if (idx < 0 || idx == 0 || idx == typeName.Length - 1)
1639                                 throw new ArgumentException ("Assembly qualifed type name is required", "typeName");
1640                         string an = typeName.Substring (idx + 1);
1641                         Assembly a;
1642                         try {
1643                                 a = Assembly.ReflectionOnlyLoad (an);
1644                         } catch {
1645                                 if (throwIfNotFound)
1646                                         throw;
1647                                 return null;
1648                         }
1649                         return a.GetType (typeName.Substring (0, idx), throwIfNotFound, ignoreCase);
1650                 }
1651
1652                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1653                 extern void GetPacking (out int packing, out int size);         
1654
1655                 public virtual StructLayoutAttribute StructLayoutAttribute {
1656                         get {
1657 #if NET_4_0
1658                                 throw new NotSupportedException ();
1659 #else
1660                                 return GetStructLayoutAttribute ();
1661 #endif
1662                         }
1663                 }
1664                 
1665                 internal StructLayoutAttribute GetStructLayoutAttribute ()
1666                 {
1667                         LayoutKind kind;
1668
1669                         if (IsLayoutSequential)
1670                                 kind = LayoutKind.Sequential;
1671                         else if (IsExplicitLayout)
1672                                 kind = LayoutKind.Explicit;
1673                         else
1674                                 kind = LayoutKind.Auto;
1675
1676                         StructLayoutAttribute attr = new StructLayoutAttribute (kind);
1677
1678                         if (IsUnicodeClass)
1679                                 attr.CharSet = CharSet.Unicode;
1680                         else if (IsAnsiClass)
1681                                 attr.CharSet = CharSet.Ansi;
1682                         else
1683                                 attr.CharSet = CharSet.Auto;
1684
1685                         if (kind != LayoutKind.Auto) {
1686                                 int packing;
1687                                 GetPacking (out packing, out attr.Size);
1688                                 // 0 means no data provided, we end up with default value
1689                                 if (packing != 0)
1690                                         attr.Pack = packing;
1691                         }
1692
1693                         return attr;
1694                 }
1695
1696                 internal object[] GetPseudoCustomAttributes ()
1697                 {
1698                         int count = 0;
1699
1700                         /* IsSerializable returns true for delegates/enums as well */
1701                         if ((Attributes & TypeAttributes.Serializable) != 0)
1702                                 count ++;
1703                         if ((Attributes & TypeAttributes.Import) != 0)
1704                                 count ++;
1705
1706                         if (count == 0)
1707                                 return null;
1708                         object[] attrs = new object [count];
1709                         count = 0;
1710
1711                         if ((Attributes & TypeAttributes.Serializable) != 0)
1712                                 attrs [count ++] = new SerializableAttribute ();
1713                         if ((Attributes & TypeAttributes.Import) != 0)
1714                                 attrs [count ++] = new ComImportAttribute ();
1715
1716                         return attrs;
1717                 }                       
1718
1719
1720 #if NET_4_0
1721                 public virtual bool IsEquivalentTo (Type other)
1722                 {
1723                         return this == other;
1724                 }
1725 #endif
1726
1727                 /* 
1728                  * Return whenever this object is an instance of a user defined subclass
1729                  * of System.Type or an instance of TypeDelegator.
1730                  * A user defined type is not simply the opposite of a system type.
1731                  * It's any class that's neither a SRE or runtime baked type.
1732                  */
1733                 internal virtual bool IsUserType {
1734                         get {
1735                                 return true;
1736                         }
1737                 }
1738
1739 #if !MOBILE
1740                 void _Type.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1741                 {
1742                         throw new NotImplementedException ();
1743                 }
1744
1745                 void _Type.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1746                 {
1747                         throw new NotImplementedException ();
1748                 }
1749
1750                 void _Type.GetTypeInfoCount (out uint pcTInfo)
1751                 {
1752                         throw new NotImplementedException ();
1753                 }
1754
1755                 void _Type.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1756                 {
1757                         throw new NotImplementedException ();
1758                 }
1759 #endif
1760         }
1761 }