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