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