Wrap always_inline and noinline attributes in compiler checks and use MSVC equivalent.
[mono.git] / mcs / class / IKVM.Reflection / Type.cs
1 /*
2   Copyright (C) 2009-2012 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Runtime.InteropServices;
26 using System.Text;
27 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
29
30 namespace IKVM.Reflection
31 {
32         interface IGenericContext
33         {
34                 Type GetGenericTypeArgument(int index);
35                 Type GetGenericMethodArgument(int index);
36         }
37
38         interface IGenericBinder
39         {
40                 Type BindTypeParameter(Type type);
41                 Type BindMethodParameter(Type type);
42         }
43
44         public abstract class Type : MemberInfo, IGenericContext, IGenericBinder
45         {
46                 public static readonly Type[] EmptyTypes = Empty<Type>.Array;
47                 protected readonly Type underlyingType;
48                 protected TypeFlags typeFlags;
49
50                 [Flags]
51                 protected enum TypeFlags
52                 {
53                         // for use by TypeBuilder
54                         IsGenericTypeDefinition = 1,
55                         HasNestedTypes = 2,
56                         Baked = 4,
57
58                         // for use by MissingType
59                         ValueType = 8,
60                         NotValueType = 16,
61
62                         // for use by TypeDef, TypeBuilder or MissingType
63                         PotentialEnumOrValueType = 32,
64                         EnumOrValueType = 64,
65                 }
66
67                 // prevent subclassing by outsiders
68                 internal Type()
69                 {
70                         this.underlyingType = this;
71                 }
72
73                 internal Type(Type underlyingType)
74                 {
75                         System.Diagnostics.Debug.Assert(underlyingType.underlyingType == underlyingType);
76                         this.underlyingType = underlyingType;
77                         this.typeFlags = underlyingType.typeFlags;
78                 }
79
80                 public static Binder DefaultBinder
81                 {
82                         get { return new DefaultBinder(); }
83                 }
84
85                 public sealed override MemberTypes MemberType
86                 {
87                         get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
88                 }
89
90                 public virtual string AssemblyQualifiedName
91                 {
92                         // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
93                         get { return this.FullName + ", " + this.Assembly.FullName; }
94                 }
95
96                 public abstract Type BaseType
97                 {
98                         get;
99                 }
100
101                 public abstract TypeAttributes Attributes
102                 {
103                         get;
104                 }
105
106                 public virtual Type GetElementType()
107                 {
108                         return null;
109                 }
110
111                 internal virtual void CheckBaked()
112                 {
113                 }
114
115                 public virtual Type[] __GetDeclaredTypes()
116                 {
117                         return Type.EmptyTypes;
118                 }
119
120                 public virtual Type[] __GetDeclaredInterfaces()
121                 {
122                         return Type.EmptyTypes;
123                 }
124
125                 public virtual MethodBase[] __GetDeclaredMethods()
126                 {
127                         return Empty<MethodBase>.Array;
128                 }
129
130                 public virtual __MethodImplMap __GetMethodImplMap()
131                 {
132                         throw new NotSupportedException();
133                 }
134
135                 public virtual FieldInfo[] __GetDeclaredFields()
136                 {
137                         return Empty<FieldInfo>.Array;
138                 }
139
140                 public virtual EventInfo[] __GetDeclaredEvents()
141                 {
142                         return Empty<EventInfo>.Array;
143                 }
144
145                 public virtual PropertyInfo[] __GetDeclaredProperties()
146                 {
147                         return Empty<PropertyInfo>.Array;
148                 }
149
150                 public virtual CustomModifiers __GetCustomModifiers()
151                 {
152                         return new CustomModifiers();
153                 }
154
155                 [Obsolete("Please use __GetCustomModifiers() instead.")]
156                 public Type[] __GetRequiredCustomModifiers()
157                 {
158                         return __GetCustomModifiers().GetRequired();
159                 }
160
161                 [Obsolete("Please use __GetCustomModifiers() instead.")]
162                 public Type[] __GetOptionalCustomModifiers()
163                 {
164                         return __GetCustomModifiers().GetOptional();
165                 }
166
167                 public virtual __StandAloneMethodSig __MethodSignature
168                 {
169                         get { throw new InvalidOperationException(); }
170                 }
171
172                 public virtual bool HasElementType
173                 {
174                         get { return false; }
175                 }
176
177                 public virtual bool IsArray
178                 {
179                         get { return false; }
180                 }
181
182                 public virtual bool __IsVector
183                 {
184                         get { return false; }
185                 }
186
187                 public virtual bool IsByRef
188                 {
189                         get { return false; }
190                 }
191
192                 public virtual bool IsPointer
193                 {
194                         get { return false; }
195                 }
196
197                 public virtual bool __IsFunctionPointer
198                 {
199                         get { return false; }
200                 }
201
202                 public virtual bool IsValueType
203                 {
204                         get
205                         {
206                                 Type baseType = this.BaseType;
207                                 return baseType != null
208                                         && baseType.IsEnumOrValueType
209                                         && !this.IsEnumOrValueType;
210                         }
211                 }
212
213                 public virtual bool IsGenericParameter
214                 {
215                         get { return false; }
216                 }
217
218                 public virtual int GenericParameterPosition
219                 {
220                         get { throw new NotSupportedException(); }
221                 }
222
223                 public virtual MethodBase DeclaringMethod
224                 {
225                         get { return null; }
226                 }
227
228                 public Type UnderlyingSystemType
229                 {
230                         get { return underlyingType; }
231                 }
232
233                 public override Type DeclaringType
234                 {
235                         get { return null; }
236                 }
237
238                 public virtual string __Name
239                 {
240                         get { throw new InvalidOperationException(); }
241                 }
242
243                 public virtual string __Namespace
244                 {
245                         get { throw new InvalidOperationException(); }
246                 }
247
248                 public abstract override string Name
249                 {
250                         get;
251                 }
252
253                 public virtual string Namespace
254                 {
255                         get
256                         {
257                                 if (IsNested)
258                                 {
259                                         return DeclaringType.Namespace;
260                                 }
261                                 return __Namespace;
262                         }
263                 }
264
265                 internal virtual int GetModuleBuilderToken()
266                 {
267                         throw new InvalidOperationException();
268                 }
269
270                 public static bool operator ==(Type t1, Type t2)
271                 {
272                         // Casting to object results in smaller code than calling ReferenceEquals and makes
273                         // this method more likely to be inlined.
274                         // On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
275                         return (object)t1 == (object)t2
276                                 || ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
277                 }
278
279                 public static bool operator !=(Type t1, Type t2)
280                 {
281                         return !(t1 == t2);
282                 }
283
284                 public bool Equals(Type type)
285                 {
286                         return this == type;
287                 }
288
289                 public override bool Equals(object obj)
290                 {
291                         return Equals(obj as Type);
292                 }
293
294                 public override int GetHashCode()
295                 {
296                         Type type = this.UnderlyingSystemType;
297                         return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
298                 }
299
300                 public virtual Type[] GetGenericArguments()
301                 {
302                         return Type.EmptyTypes;
303                 }
304
305                 public virtual CustomModifiers[] __GetGenericArgumentsCustomModifiers()
306                 {
307                         return Empty<CustomModifiers>.Array;
308                 }
309
310                 [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
311                 public Type[][] __GetGenericArgumentsRequiredCustomModifiers()
312                 {
313                         CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
314                         Type[][] array = new Type[customModifiers.Length][];
315                         for (int i = 0; i < array.Length; i++)
316                         {
317                                 array[i] = customModifiers[i].GetRequired();
318                         }
319                         return array;
320                 }
321
322                 [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
323                 public Type[][] __GetGenericArgumentsOptionalCustomModifiers()
324                 {
325                         CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
326                         Type[][] array = new Type[customModifiers.Length][];
327                         for (int i = 0; i < array.Length; i++)
328                         {
329                                 array[i] = customModifiers[i].GetOptional();
330                         }
331                         return array;
332                 }
333
334                 public virtual Type GetGenericTypeDefinition()
335                 {
336                         throw new InvalidOperationException();
337                 }
338
339                 public virtual StructLayoutAttribute StructLayoutAttribute
340                 {
341                         get { return null; }
342                 }
343
344                 public virtual bool __GetLayout(out int packingSize, out int typeSize)
345                 {
346                         packingSize = 0;
347                         typeSize = 0;
348                         return false;
349                 }
350
351                 public virtual bool IsGenericType
352                 {
353                         get { return false; }
354                 }
355
356                 public virtual bool IsGenericTypeDefinition
357                 {
358                         get { return false; }
359                 }
360
361                 // .NET 4.5 API
362                 public virtual bool IsConstructedGenericType
363                 {
364                         get { return false; }
365                 }
366
367                 public virtual bool ContainsGenericParameters
368                 {
369                         get
370                         {
371                                 if (this.IsGenericParameter)
372                                 {
373                                         return true;
374                                 }
375                                 foreach (Type arg in this.GetGenericArguments())
376                                 {
377                                         if (arg.ContainsGenericParameters)
378                                         {
379                                                 return true;
380                                         }
381                                 }
382                                 return false;
383                         }
384                 }
385
386                 public virtual Type[] GetGenericParameterConstraints()
387                 {
388                         throw new InvalidOperationException();
389                 }
390
391                 public virtual GenericParameterAttributes GenericParameterAttributes
392                 {
393                         get { throw new InvalidOperationException(); }
394                 }
395
396                 public virtual int GetArrayRank()
397                 {
398                         throw new NotSupportedException();
399                 }
400
401                 public virtual int[] __GetArraySizes()
402                 {
403                         throw new NotSupportedException();
404                 }
405
406                 public virtual int[] __GetArrayLowerBounds()
407                 {
408                         throw new NotSupportedException();
409                 }
410
411                 // .NET 4.0 API
412                 public virtual Type GetEnumUnderlyingType()
413                 {
414                         if (!this.IsEnum)
415                         {
416                                 throw new ArgumentException();
417                         }
418                         CheckBaked();
419                         return GetEnumUnderlyingTypeImpl();
420                 }
421
422                 internal Type GetEnumUnderlyingTypeImpl()
423                 {
424                         foreach (FieldInfo field in __GetDeclaredFields())
425                         {
426                                 if (!field.IsStatic)
427                                 {
428                                         // the CLR assumes that an enum has only one instance field, so we can do the same
429                                         return field.FieldType;
430                                 }
431                         }
432                         throw new InvalidOperationException();
433                 }
434
435                 public override string ToString()
436                 {
437                         return FullName;
438                 }
439
440                 public abstract string FullName
441                 {
442                         get;
443                 }
444
445                 protected string GetFullName()
446                 {
447                         string ns = TypeNameParser.Escape(this.__Namespace);
448                         Type decl = this.DeclaringType;
449                         if (decl == null)
450                         {
451                                 if (ns == null)
452                                 {
453                                         return this.Name;
454                                 }
455                                 else
456                                 {
457                                         return ns + "." + this.Name;
458                                 }
459                         }
460                         else
461                         {
462                                 if (ns == null)
463                                 {
464                                         return decl.FullName + "+" + this.Name;
465                                 }
466                                 else
467                                 {
468                                         return decl.FullName + "+" + ns + "." + this.Name;
469                                 }
470                         }
471                 }
472
473                 internal virtual bool IsModulePseudoType
474                 {
475                         get { return false; }
476                 }
477
478                 internal virtual Type GetGenericTypeArgument(int index)
479                 {
480                         throw new InvalidOperationException();
481                 }
482
483                 public MemberInfo[] GetDefaultMembers()
484                 {
485                         Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
486                         foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
487                         {
488                                 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
489                                 {
490                                         return GetMember((string)cad.ConstructorArguments[0].Value);
491                                 }
492                         }
493                         return Empty<MemberInfo>.Array;
494                 }
495
496                 public MemberInfo[] GetMember(string name)
497                 {
498                         return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
499                 }
500
501                 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
502                 {
503                         return GetMember(name, MemberTypes.All, bindingAttr);
504                 }
505
506                 public MemberInfo[] GetMembers()
507                 {
508                         return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
509                 }
510
511                 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
512                 {
513                         List<MemberInfo> members = new List<MemberInfo>();
514                         members.AddRange(GetConstructors(bindingAttr));
515                         members.AddRange(GetMethods(bindingAttr));
516                         members.AddRange(GetFields(bindingAttr));
517                         members.AddRange(GetProperties(bindingAttr));
518                         members.AddRange(GetEvents(bindingAttr));
519                         members.AddRange(GetNestedTypes(bindingAttr));
520                         return members.ToArray();
521                 }
522
523                 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
524                 {
525                         MemberFilter filter;
526                         if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
527                         {
528                                 name = name.ToLowerInvariant();
529                                 filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.ToLowerInvariant().Equals(filterCriteria); };
530                         }
531                         else
532                         {
533                                 filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
534                         }
535                         return FindMembers(type, bindingAttr, filter, name);
536                 }
537
538                 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
539                 {
540                         foreach (MemberInfo member in members)
541                         {
542                                 if (filter == null || filter(member, filterCriteria))
543                                 {
544                                         list.Add(member);
545                                 }
546                         }
547                 }
548
549                 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
550                 {
551                         List<MemberInfo> members = new List<MemberInfo>();
552                         if ((memberType & MemberTypes.Constructor) != 0)
553                         {
554                                 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
555                         }
556                         if ((memberType & MemberTypes.Method) != 0)
557                         {
558                                 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
559                         }
560                         if ((memberType & MemberTypes.Field) != 0)
561                         {
562                                 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
563                         }
564                         if ((memberType & MemberTypes.Property) != 0)
565                         {
566                                 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
567                         }
568                         if ((memberType & MemberTypes.Event) != 0)
569                         {
570                                 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
571                         }
572                         if ((memberType & MemberTypes.NestedType) != 0)
573                         {
574                                 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
575                         }
576                         return members.ToArray();
577                 }
578
579                 private MemberInfo[] GetMembers<T>()
580                 {
581                         if (typeof(T) == typeof(ConstructorInfo) || typeof(T) == typeof(MethodInfo))
582                         {
583                                 return __GetDeclaredMethods();
584                         }
585                         else if (typeof(T) == typeof(FieldInfo))
586                         {
587                                 return __GetDeclaredFields();
588                         }
589                         else if (typeof(T) == typeof(PropertyInfo))
590                         {
591                                 return __GetDeclaredProperties();
592                         }
593                         else if (typeof(T) == typeof(EventInfo))
594                         {
595                                 return __GetDeclaredEvents();
596                         }
597                         else if (typeof(T) == typeof(Type))
598                         {
599                                 return __GetDeclaredTypes();
600                         }
601                         else
602                         {
603                                 throw new InvalidOperationException();
604                         }
605                 }
606
607                 private T[] GetMembers<T>(BindingFlags flags)
608                         where T : MemberInfo
609                 {
610                         CheckBaked();
611                         List<T> list = new List<T>();
612                         foreach (MemberInfo member in GetMembers<T>())
613                         {
614                                 if (member is T && member.BindingFlagsMatch(flags))
615                                 {
616                                         list.Add((T)member);
617                                 }
618                         }
619                         if ((flags & BindingFlags.DeclaredOnly) == 0)
620                         {
621                                 for (Type type = this.BaseType; type != null; type = type.BaseType)
622                                 {
623                                         type.CheckBaked();
624                                         foreach (MemberInfo member in type.GetMembers<T>())
625                                         {
626                                                 if (member is T && member.BindingFlagsMatchInherited(flags))
627                                                 {
628                                                         list.Add((T)member.SetReflectedType(this));
629                                                 }
630                                         }
631                                 }
632                         }
633                         return list.ToArray();
634                 }
635
636                 private T GetMemberByName<T>(string name, BindingFlags flags, Predicate<T> filter)
637                         where T : MemberInfo
638                 {
639                         CheckBaked();
640                         if ((flags & BindingFlags.IgnoreCase) != 0)
641                         {
642                                 name = name.ToLowerInvariant();
643                         }
644                         T found = null;
645                         foreach (MemberInfo member in GetMembers<T>())
646                         {
647                                 if (member is T && member.BindingFlagsMatch(flags))
648                                 {
649                                         string memberName = member.Name;
650                                         if ((flags & BindingFlags.IgnoreCase) != 0)
651                                         {
652                                                 memberName = memberName.ToLowerInvariant();
653                                         }
654                                         if (memberName == name && (filter == null || filter((T)member)))
655                                         {
656                                                 if (found != null)
657                                                 {
658                                                         throw new AmbiguousMatchException();
659                                                 }
660                                                 found = (T)member;
661                                         }
662                                 }
663                         }
664                         if ((flags & BindingFlags.DeclaredOnly) == 0)
665                         {
666                                 for (Type type = this.BaseType; (found == null || typeof(T) == typeof(MethodInfo)) && type != null; type = type.BaseType)
667                                 {
668                                         type.CheckBaked();
669                                         foreach (MemberInfo member in type.GetMembers<T>())
670                                         {
671                                                 if (member is T && member.BindingFlagsMatchInherited(flags))
672                                                 {
673                                                         string memberName = member.Name;
674                                                         if ((flags & BindingFlags.IgnoreCase) != 0)
675                                                         {
676                                                                 memberName = memberName.ToLowerInvariant();
677                                                         }
678                                                         if (memberName == name && (filter == null || filter((T)member)))
679                                                         {
680                                                                 if (found != null)
681                                                                 {
682                                                                         MethodInfo mi;
683                                                                         // TODO does this depend on HideBySig vs HideByName?
684                                                                         if ((mi = found as MethodInfo) != null
685                                                                                 && mi.MethodSignature.MatchParameterTypes(((MethodBase)member).MethodSignature))
686                                                                         {
687                                                                                 continue;
688                                                                         }
689                                                                         throw new AmbiguousMatchException();
690                                                                 }
691                                                                 found = (T)member.SetReflectedType(this);
692                                                         }
693                                                 }
694                                         }
695                                 }
696                         }
697                         return found;
698                 }
699
700                 private T GetMemberByName<T>(string name, BindingFlags flags)
701                         where T : MemberInfo
702                 {
703                         return GetMemberByName<T>(name, flags, null);
704                 }
705
706                 public EventInfo GetEvent(string name)
707                 {
708                         return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
709                 }
710
711                 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
712                 {
713                         return GetMemberByName<EventInfo>(name, bindingAttr);
714                 }
715
716                 public EventInfo[] GetEvents()
717                 {
718                         return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
719                 }
720
721                 public EventInfo[] GetEvents(BindingFlags bindingAttr)
722                 {
723                         return GetMembers<EventInfo>(bindingAttr);
724                 }
725
726                 public FieldInfo GetField(string name)
727                 {
728                         return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
729                 }
730
731                 public FieldInfo GetField(string name, BindingFlags bindingAttr)
732                 {
733                         return GetMemberByName<FieldInfo>(name, bindingAttr);
734                 }
735
736                 public FieldInfo[] GetFields()
737                 {
738                         return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
739                 }
740
741                 public FieldInfo[] GetFields(BindingFlags bindingAttr)
742                 {
743                         return GetMembers<FieldInfo>(bindingAttr);
744                 }
745
746                 public Type[] GetInterfaces()
747                 {
748                         List<Type> list = new List<Type>();
749                         for (Type type = this; type != null; type = type.BaseType)
750                         {
751                                 AddInterfaces(list, type);
752                         }
753                         return list.ToArray();
754                 }
755
756                 private static void AddInterfaces(List<Type> list, Type type)
757                 {
758                         foreach (Type iface in type.__GetDeclaredInterfaces())
759                         {
760                                 if (!list.Contains(iface))
761                                 {
762                                         list.Add(iface);
763                                         AddInterfaces(list, iface);
764                                 }
765                         }
766                 }
767
768                 public MethodInfo[] GetMethods(BindingFlags bindingAttr)
769                 {
770                         CheckBaked();
771                         List<MethodInfo> list = new List<MethodInfo>();
772                         foreach (MethodBase mb in __GetDeclaredMethods())
773                         {
774                                 MethodInfo mi = mb as MethodInfo;
775                                 if (mi != null && mi.BindingFlagsMatch(bindingAttr))
776                                 {
777                                         list.Add(mi);
778                                 }
779                         }
780                         if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
781                         {
782                                 List<MethodInfo> baseMethods = new List<MethodInfo>();
783                                 foreach (MethodInfo mi in list)
784                                 {
785                                         if (mi.IsVirtual)
786                                         {
787                                                 baseMethods.Add(mi.GetBaseDefinition());
788                                         }
789                                 }
790                                 for (Type type = this.BaseType; type != null; type = type.BaseType)
791                                 {
792                                         type.CheckBaked();
793                                         foreach (MethodBase mb in type.__GetDeclaredMethods())
794                                         {
795                                                 MethodInfo mi = mb as MethodInfo;
796                                                 if (mi != null && mi.BindingFlagsMatchInherited(bindingAttr))
797                                                 {
798                                                         if (mi.IsVirtual)
799                                                         {
800                                                                 if (baseMethods == null)
801                                                                 {
802                                                                         baseMethods = new List<MethodInfo>();
803                                                                 }
804                                                                 else if (baseMethods.Contains(mi.GetBaseDefinition()))
805                                                                 {
806                                                                         continue;
807                                                                 }
808                                                                 baseMethods.Add(mi.GetBaseDefinition());
809                                                         }
810                                                         list.Add((MethodInfo)mi.SetReflectedType(this));
811                                                 }
812                                         }
813                                 }
814                         }
815                         return list.ToArray();
816                 }
817
818                 public MethodInfo[] GetMethods()
819                 {
820                         return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
821                 }
822
823                 public MethodInfo GetMethod(string name)
824                 {
825                         return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
826                 }
827
828                 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
829                 {
830                         return GetMemberByName<MethodInfo>(name, bindingAttr);
831                 }
832
833                 public MethodInfo GetMethod(string name, Type[] types)
834                 {
835                         return GetMethod(name, types, null);
836                 }
837
838                 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
839                 {
840                         return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
841                 }
842
843                 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
844                 {
845                         // first we try an exact match and only if that fails we fall back to using the binder
846                         return GetMemberByName<MethodInfo>(name, bindingAttr,
847                                 delegate(MethodInfo method) { return method.MethodSignature.MatchParameterTypes(types); })
848                                 ?? GetMethodWithBinder<MethodInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
849                 }
850
851                 private T GetMethodWithBinder<T>(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
852                         where T : MethodBase
853                 {
854                         List<MethodBase> list = new List<MethodBase>();
855                         GetMemberByName<T>(name, bindingAttr, delegate(T method) {
856                                 list.Add(method);
857                                 return false;
858                         });
859                         return (T)binder.SelectMethod(bindingAttr, list.ToArray(), types, modifiers);
860                 }
861
862                 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
863                 {
864                         // FXBUG callConvention seems to be ignored
865                         return GetMethod(name, bindingAttr, binder, types, modifiers);
866                 }
867
868                 public ConstructorInfo[] GetConstructors()
869                 {
870                         return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
871                 }
872
873                 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
874                 {
875                         return GetMembers<ConstructorInfo>(bindingAttr | BindingFlags.DeclaredOnly);
876                 }
877
878                 public ConstructorInfo GetConstructor(Type[] types)
879                 {
880                         return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
881                 }
882
883                 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
884                 {
885                         ConstructorInfo ci1 = null;
886                         if ((bindingAttr & BindingFlags.Instance) != 0)
887                         {
888                                 ci1 = GetConstructorImpl(ConstructorInfo.ConstructorName, bindingAttr, binder, types, modifiers);
889                         }
890                         if ((bindingAttr & BindingFlags.Static) != 0)
891                         {
892                                 ConstructorInfo ci2 = GetConstructorImpl(ConstructorInfo.TypeConstructorName, bindingAttr, binder, types, modifiers);
893                                 if (ci2 != null)
894                                 {
895                                         if (ci1 != null)
896                                         {
897                                                 throw new AmbiguousMatchException();
898                                         }
899                                         return ci2;
900                                 }
901                         }
902                         return ci1;
903                 }
904
905                 private ConstructorInfo GetConstructorImpl(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
906                 {
907                         // first we try an exact match and only if that fails we fall back to using the binder
908                         return GetMemberByName<ConstructorInfo>(name, bindingAttr | BindingFlags.DeclaredOnly,
909                                 delegate(ConstructorInfo ctor) { return ctor.MethodSignature.MatchParameterTypes(types); })
910                                 ?? GetMethodWithBinder<ConstructorInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
911                 }
912
913                 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
914                 {
915                         // FXBUG callConvention seems to be ignored
916                         return GetConstructor(bindingAttr, binder, types, modifiers);
917                 }
918
919                 internal Type ResolveNestedType(TypeName typeName)
920                 {
921                         return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
922                 }
923
924                 // unlike the public API, this takes the namespace and name into account
925                 internal virtual Type FindNestedType(TypeName name)
926                 {
927                         foreach (Type type in __GetDeclaredTypes())
928                         {
929                                 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
930                                 {
931                                         return type;
932                                 }
933                         }
934                         return null;
935                 }
936
937                 internal virtual Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
938                 {
939                         foreach (Type type in __GetDeclaredTypes())
940                         {
941                                 if (new TypeName(type.__Namespace, type.__Name).ToLowerInvariant() == lowerCaseName)
942                                 {
943                                         return type;
944                                 }
945                         }
946                         return null;
947                 }
948
949                 public Type GetNestedType(string name)
950                 {
951                         return GetNestedType(name, BindingFlags.Public);
952                 }
953
954                 public Type GetNestedType(string name, BindingFlags bindingAttr)
955                 {
956                         // FXBUG the namespace is ignored, so we can use GetMemberByName
957                         return GetMemberByName<Type>(name, bindingAttr | BindingFlags.DeclaredOnly);
958                 }
959
960                 public Type[] GetNestedTypes()
961                 {
962                         return GetNestedTypes(BindingFlags.Public);
963                 }
964
965                 public Type[] GetNestedTypes(BindingFlags bindingAttr)
966                 {
967                         // FXBUG the namespace is ignored, so we can use GetMember
968                         return GetMembers<Type>(bindingAttr | BindingFlags.DeclaredOnly);
969                 }
970
971                 public PropertyInfo[] GetProperties()
972                 {
973                         return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
974                 }
975
976                 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
977                 {
978                         return GetMembers<PropertyInfo>(bindingAttr);
979                 }
980
981                 public PropertyInfo GetProperty(string name)
982                 {
983                         return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
984                 }
985
986                 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
987                 {
988                         return GetMemberByName<PropertyInfo>(name, bindingAttr);
989                 }
990
991                 public PropertyInfo GetProperty(string name, Type returnType)
992                 {
993                         const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
994                         return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertyType.Equals(returnType); })
995                                 ?? GetPropertyWithBinder(name, flags, DefaultBinder, returnType, null, null);
996                 }
997
998                 public PropertyInfo GetProperty(string name, Type[] types)
999                 {
1000                         const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
1001                         return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertySignature.MatchParameterTypes(types); })
1002                                 ?? GetPropertyWithBinder(name, flags, DefaultBinder, null, types, null);
1003                 }
1004
1005                 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
1006                 {
1007                         return GetProperty(name, returnType, types, null);
1008                 }
1009
1010                 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1011                 {
1012                         return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
1013                 }
1014
1015                 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1016                 {
1017                         return GetMemberByName<PropertyInfo>(name, bindingAttr,
1018                                 delegate(PropertyInfo prop) {
1019                                         return prop.PropertyType.Equals(returnType) && prop.PropertySignature.MatchParameterTypes(types);
1020                                 })
1021                                 ?? GetPropertyWithBinder(name, bindingAttr, binder ?? DefaultBinder, returnType, types, modifiers);
1022                 }
1023
1024                 private PropertyInfo GetPropertyWithBinder(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1025                 {
1026                         List<PropertyInfo> list = new List<PropertyInfo>();
1027                         GetMemberByName<PropertyInfo>(name, bindingAttr, delegate(PropertyInfo property) {
1028                                 list.Add(property);
1029                                 return false;
1030                         });
1031                         return binder.SelectProperty(bindingAttr, list.ToArray(), returnType, types, modifiers);
1032                 }
1033
1034                 public Type GetInterface(string name)
1035                 {
1036                         return GetInterface(name, false);
1037                 }
1038
1039                 public Type GetInterface(string name, bool ignoreCase)
1040                 {
1041                         if (ignoreCase)
1042                         {
1043                                 name = name.ToLowerInvariant();
1044                         }
1045                         Type found = null;
1046                         foreach (Type type in GetInterfaces())
1047                         {
1048                                 string typeName = type.FullName;
1049                                 if (ignoreCase)
1050                                 {
1051                                         typeName = typeName.ToLowerInvariant();
1052                                 }
1053                                 if (typeName == name)
1054                                 {
1055                                         if (found != null)
1056                                         {
1057                                                 throw new AmbiguousMatchException();
1058                                         }
1059                                         found = type;
1060                                 }
1061                         }
1062                         return found;
1063                 }
1064
1065                 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
1066                 {
1067                         List<Type> list = new List<Type>();
1068                         foreach (Type type in GetInterfaces())
1069                         {
1070                                 if (filter(type, filterCriteria))
1071                                 {
1072                                         list.Add(type);
1073                                 }
1074                         }
1075                         return list.ToArray();
1076                 }
1077
1078                 public ConstructorInfo TypeInitializer
1079                 {
1080                         get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
1081                 }
1082
1083                 public bool IsPrimitive
1084                 {
1085                         get
1086                         {
1087                                 Universe u = this.Universe;
1088                                 return this == u.System_Boolean
1089                                         || this == u.System_Byte
1090                                         || this == u.System_SByte
1091                                         || this == u.System_Int16
1092                                         || this == u.System_UInt16
1093                                         || this == u.System_Int32
1094                                         || this == u.System_UInt32
1095                                         || this == u.System_Int64
1096                                         || this == u.System_UInt64
1097                                         || this == u.System_IntPtr
1098                                         || this == u.System_UIntPtr
1099                                         || this == u.System_Char
1100                                         || this == u.System_Double
1101                                         || this == u.System_Single
1102                                         ;
1103                         }
1104                 }
1105
1106                 public bool IsEnum
1107                 {
1108                         get
1109                         {
1110                                 Type baseType = this.BaseType;
1111                                 return baseType != null
1112                                         && baseType.IsEnumOrValueType
1113                                         && baseType.__Name[0] == 'E';
1114                         }
1115                 }
1116
1117                 public bool IsSealed
1118                 {
1119                         get { return (Attributes & TypeAttributes.Sealed) != 0; }
1120                 }
1121
1122                 public bool IsAbstract
1123                 {
1124                         get { return (Attributes & TypeAttributes.Abstract) != 0; }
1125                 }
1126
1127                 private bool CheckVisibility(TypeAttributes access)
1128                 {
1129                         return (Attributes & TypeAttributes.VisibilityMask) == access;
1130                 }
1131
1132                 public bool IsPublic
1133                 {
1134                         get { return CheckVisibility(TypeAttributes.Public); }
1135                 }
1136
1137                 public bool IsNestedPublic
1138                 {
1139                         get { return CheckVisibility(TypeAttributes.NestedPublic); }
1140                 }
1141
1142                 public bool IsNestedPrivate
1143                 {
1144                         get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1145                 }
1146
1147                 public bool IsNestedFamily
1148                 {
1149                         get { return CheckVisibility(TypeAttributes.NestedFamily); }
1150                 }
1151
1152                 public bool IsNestedAssembly
1153                 {
1154                         get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1155                 }
1156
1157                 public bool IsNestedFamANDAssem
1158                 {
1159                         get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1160                 }
1161
1162                 public bool IsNestedFamORAssem
1163                 {
1164                         get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1165                 }
1166
1167                 public bool IsNotPublic
1168                 {
1169                         get { return CheckVisibility(TypeAttributes.NotPublic); }
1170                 }
1171
1172                 public bool IsImport
1173                 {
1174                         get { return (Attributes & TypeAttributes.Import) != 0; }
1175                 }
1176
1177                 public bool IsCOMObject
1178                 {
1179                         get { return IsClass && IsImport; }
1180                 }
1181
1182                 public bool IsContextful
1183                 {
1184                         get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1185                 }
1186
1187                 public bool IsMarshalByRef
1188                 {
1189                         get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1190                 }
1191
1192                 public virtual bool IsVisible
1193                 {
1194                         get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1195                 }
1196
1197                 public bool IsAnsiClass
1198                 {
1199                         get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1200                 }
1201
1202                 public bool IsUnicodeClass
1203                 {
1204                         get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1205                 }
1206
1207                 public bool IsAutoClass
1208                 {
1209                         get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1210                 }
1211
1212                 public bool IsAutoLayout
1213                 {
1214                         get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1215                 }
1216
1217                 public bool IsLayoutSequential
1218                 {
1219                         get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1220                 }
1221
1222                 public bool IsExplicitLayout
1223                 {
1224                         get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1225                 }
1226
1227                 public bool IsSpecialName
1228                 {
1229                         get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1230                 }
1231
1232                 public bool IsSerializable
1233                 {
1234                         get { return (Attributes & TypeAttributes.Serializable) != 0; }
1235                 }
1236
1237                 public bool IsClass
1238                 {
1239                         get { return !IsInterface && !IsValueType; }
1240                 }
1241
1242                 public bool IsInterface
1243                 {
1244                         get { return (Attributes & TypeAttributes.Interface) != 0; }
1245                 }
1246
1247                 public bool IsNested
1248                 {
1249                         // FXBUG we check the declaring type (like .NET) and this results
1250                         // in IsNested returning true for a generic type parameter
1251                         get { return this.DeclaringType != null; }
1252                 }
1253
1254                 public virtual bool __ContainsMissingType
1255                 {
1256                         get
1257                         {
1258                                 if (this.__IsMissing)
1259                                 {
1260                                         return true;
1261                                 }
1262                                 foreach (Type arg in this.GetGenericArguments())
1263                                 {
1264                                         if (arg.__ContainsMissingType)
1265                                         {
1266                                                 return true;
1267                                         }
1268                                 }
1269                                 return false;
1270                         }
1271                 }
1272
1273                 public Type MakeArrayType()
1274                 {
1275                         return ArrayType.Make(this, new CustomModifiers());
1276                 }
1277
1278                 public Type __MakeArrayType(CustomModifiers customModifiers)
1279                 {
1280                         return ArrayType.Make(this, customModifiers);
1281                 }
1282
1283                 [Obsolete("Please use __MakeArrayType(CustomModifiers) instead.")]
1284                 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1285                 {
1286                         return __MakeArrayType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1287                 }
1288
1289                 public Type MakeArrayType(int rank)
1290                 {
1291                         return __MakeArrayType(rank, new CustomModifiers());
1292                 }
1293
1294                 public Type __MakeArrayType(int rank, CustomModifiers customModifiers)
1295                 {
1296                         return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], customModifiers);
1297                 }
1298
1299                 [Obsolete("Please use __MakeArrayType(int, CustomModifiers) instead.")]
1300                 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1301                 {
1302                         return __MakeArrayType(rank, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1303                 }
1304
1305                 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, CustomModifiers customModifiers)
1306                 {
1307                         return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, customModifiers);
1308                 }
1309
1310                 [Obsolete("Please use __MakeArrayType(int, int[], int[], CustomModifiers) instead.")]
1311                 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1312                 {
1313                         return __MakeArrayType(rank, sizes, lobounds, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1314                 }
1315
1316                 public Type MakeByRefType()
1317                 {
1318                         return ByRefType.Make(this, new CustomModifiers());
1319                 }
1320
1321                 public Type __MakeByRefType(CustomModifiers customModifiers)
1322                 {
1323                         return ByRefType.Make(this, customModifiers);
1324                 }
1325
1326                 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1327                 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1328                 {
1329                         return __MakeByRefType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1330                 }
1331
1332                 public Type MakePointerType()
1333                 {
1334                         return PointerType.Make(this, new CustomModifiers());
1335                 }
1336
1337                 public Type __MakePointerType(CustomModifiers customModifiers)
1338                 {
1339                         return PointerType.Make(this, customModifiers);
1340                 }
1341
1342                 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1343                 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1344                 {
1345                         return __MakePointerType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1346                 }
1347
1348                 public Type MakeGenericType(params Type[] typeArguments)
1349                 {
1350                         return __MakeGenericType(typeArguments, null);
1351                 }
1352
1353                 public Type __MakeGenericType(Type[] typeArguments, CustomModifiers[] customModifiers)
1354                 {
1355                         if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1356                         {
1357                                 throw new InvalidOperationException();
1358                         }
1359                         return GenericTypeInstance.Make(this, Util.Copy(typeArguments), customModifiers == null ? null : (CustomModifiers[])customModifiers.Clone());
1360                 }
1361
1362                 [Obsolete("Please use __MakeGenericType(Type[], CustomModifiers[]) instead.")]
1363                 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1364                 {
1365                         if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1366                         {
1367                                 throw new InvalidOperationException();
1368                         }
1369                         CustomModifiers[] mods = null;
1370                         if (requiredCustomModifiers != null || optionalCustomModifiers != null)
1371                         {
1372                                 mods = new CustomModifiers[typeArguments.Length];
1373                                 for (int i = 0; i < mods.Length; i++)
1374                                 {
1375                                         mods[i] = CustomModifiers.FromReqOpt(Util.NullSafeElementAt(requiredCustomModifiers, i), Util.NullSafeElementAt(optionalCustomModifiers, i));
1376                                 }
1377                         }
1378                         return GenericTypeInstance.Make(this, Util.Copy(typeArguments), mods);
1379                 }
1380
1381                 public static System.Type __GetSystemType(TypeCode typeCode)
1382                 {
1383                         switch (typeCode)
1384                         {
1385                                 case TypeCode.Boolean:
1386                                         return typeof(System.Boolean);
1387                                 case TypeCode.Byte:
1388                                         return typeof(System.Byte);
1389                                 case TypeCode.Char:
1390                                         return typeof(System.Char);
1391                                 case TypeCode.DBNull:
1392                                         return typeof(System.DBNull);
1393                                 case TypeCode.DateTime:
1394                                         return typeof(System.DateTime);
1395                                 case TypeCode.Decimal:
1396                                         return typeof(System.Decimal);
1397                                 case TypeCode.Double:
1398                                         return typeof(System.Double);
1399                                 case TypeCode.Empty:
1400                                         return null;
1401                                 case TypeCode.Int16:
1402                                         return typeof(System.Int16);
1403                                 case TypeCode.Int32:
1404                                         return typeof(System.Int32);
1405                                 case TypeCode.Int64:
1406                                         return typeof(System.Int64);
1407                                 case TypeCode.Object:
1408                                         return typeof(System.Object);
1409                                 case TypeCode.SByte:
1410                                         return typeof(System.SByte);
1411                                 case TypeCode.Single:
1412                                         return typeof(System.Single);
1413                                 case TypeCode.String:
1414                                         return typeof(System.String);
1415                                 case TypeCode.UInt16:
1416                                         return typeof(System.UInt16);
1417                                 case TypeCode.UInt32:
1418                                         return typeof(System.UInt32);
1419                                 case TypeCode.UInt64:
1420                                         return typeof(System.UInt64);
1421                                 default:
1422                                         throw new ArgumentOutOfRangeException();
1423                         }
1424                 }
1425
1426                 public static TypeCode GetTypeCode(Type type)
1427                 {
1428                         if (type == null)
1429                         {
1430                                 return TypeCode.Empty;
1431                         }
1432                         if (!type.__IsMissing && type.IsEnum)
1433                         {
1434                                 type = type.GetEnumUnderlyingType();
1435                         }
1436                         Universe u = type.Module.universe;
1437                         if (type == u.System_Boolean)
1438                         {
1439                                 return TypeCode.Boolean;
1440                         }
1441                         else if (type == u.System_Char)
1442                         {
1443                                 return TypeCode.Char;
1444                         }
1445                         else if (type == u.System_SByte)
1446                         {
1447                                 return TypeCode.SByte;
1448                         }
1449                         else if (type == u.System_Byte)
1450                         {
1451                                 return TypeCode.Byte;
1452                         }
1453                         else if (type == u.System_Int16)
1454                         {
1455                                 return TypeCode.Int16;
1456                         }
1457                         else if (type == u.System_UInt16)
1458                         {
1459                                 return TypeCode.UInt16;
1460                         }
1461                         else if (type == u.System_Int32)
1462                         {
1463                                 return TypeCode.Int32;
1464                         }
1465                         else if (type == u.System_UInt32)
1466                         {
1467                                 return TypeCode.UInt32;
1468                         }
1469                         else if (type == u.System_Int64)
1470                         {
1471                                 return TypeCode.Int64;
1472                         }
1473                         else if (type == u.System_UInt64)
1474                         {
1475                                 return TypeCode.UInt64;
1476                         }
1477                         else if (type == u.System_Single)
1478                         {
1479                                 return TypeCode.Single;
1480                         }
1481                         else if (type == u.System_Double)
1482                         {
1483                                 return TypeCode.Double;
1484                         }
1485                         else if (type == u.System_DateTime)
1486                         {
1487                                 return TypeCode.DateTime;
1488                         }
1489                         else if (type == u.System_DBNull)
1490                         {
1491                                 return TypeCode.DBNull;
1492                         }
1493                         else if (type == u.System_Decimal)
1494                         {
1495                                 return TypeCode.Decimal;
1496                         }
1497                         else if (type == u.System_String)
1498                         {
1499                                 return TypeCode.String;
1500                         }
1501                         else if (type.__IsMissing)
1502                         {
1503                                 throw new MissingMemberException(type);
1504                         }
1505                         else
1506                         {
1507                                 return TypeCode.Object;
1508                         }
1509                 }
1510
1511                 public Assembly Assembly
1512                 {
1513                         get { return Module.Assembly; }
1514                 }
1515
1516                 public bool IsAssignableFrom(Type type)
1517                 {
1518                         if (this.Equals(type))
1519                         {
1520                                 return true;
1521                         }
1522                         else if (type == null)
1523                         {
1524                                 return false;
1525                         }
1526                         else if (this.IsArray && type.IsArray)
1527                         {
1528                                 if (this.GetArrayRank() != type.GetArrayRank())
1529                                 {
1530                                         return false;
1531                                 }
1532                                 else if (this.__IsVector && !type.__IsVector)
1533                                 {
1534                                         return false;
1535                                 }
1536                                 Type e1 = this.GetElementType();
1537                                 Type e2 = type.GetElementType();
1538                                 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1539                         }
1540                         else if (this.IsCovariant(type))
1541                         {
1542                                 return true;
1543                         }
1544                         else if (this.IsSealed)
1545                         {
1546                                 return false;
1547                         }
1548                         else if (this.IsInterface)
1549                         {
1550                                 foreach (Type iface in type.GetInterfaces())
1551                                 {
1552                                         if (this.Equals(iface) || this.IsCovariant(iface))
1553                                         {
1554                                                 return true;
1555                                         }
1556                                 }
1557                                 return false;
1558                         }
1559                         else if (type.IsInterface)
1560                         {
1561                                 return this == this.Module.universe.System_Object;
1562                         }
1563                         else if (type.IsPointer)
1564                         {
1565                                 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1566                         }
1567                         else
1568                         {
1569                                 return type.IsSubclassOf(this);
1570                         }
1571                 }
1572
1573                 private bool IsCovariant(Type other)
1574                 {
1575                         if (this.IsConstructedGenericType
1576                                 && other.IsConstructedGenericType
1577                                 && this.GetGenericTypeDefinition() == other.GetGenericTypeDefinition())
1578                         {
1579                                 Type[] typeParameters = GetGenericTypeDefinition().GetGenericArguments();
1580                                 for (int i = 0; i < typeParameters.Length; i++)
1581                                 {
1582                                         Type t1 = this.GetGenericTypeArgument(i);
1583                                         Type t2 = other.GetGenericTypeArgument(i);
1584                                         if (t1.IsValueType != t2.IsValueType)
1585                                         {
1586                                                 return false;
1587                                         }
1588                                         switch (typeParameters[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask)
1589                                         {
1590                                                 case GenericParameterAttributes.Covariant:
1591                                                         if (!t1.IsAssignableFrom(t2))
1592                                                         {
1593                                                                 return false;
1594                                                         }
1595                                                         break;
1596                                                 case GenericParameterAttributes.Contravariant:
1597                                                         if (!t2.IsAssignableFrom(t1))
1598                                                         {
1599                                                                 return false;
1600                                                         }
1601                                                         break;
1602                                                 case GenericParameterAttributes.None:
1603                                                         if (t1 != t2)
1604                                                         {
1605                                                                 return false;
1606                                                         }
1607                                                         break;
1608                                         }
1609                                 }
1610                                 return true;
1611                         }
1612                         return false;
1613                 }
1614
1615                 public bool IsSubclassOf(Type type)
1616                 {
1617                         Type thisType = this.BaseType;
1618                         while (thisType != null)
1619                         {
1620                                 if (thisType.Equals(type))
1621                                 {
1622                                         return true;
1623                                 }
1624                                 thisType = thisType.BaseType;
1625                         }
1626                         return false;
1627                 }
1628
1629                 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1630                 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1631                 private bool IsDirectlyImplementedInterface(Type interfaceType)
1632                 {
1633                         foreach (Type iface in __GetDeclaredInterfaces())
1634                         {
1635                                 if (interfaceType.IsAssignableFrom(iface))
1636                                 {
1637                                         return true;
1638                                 }
1639                         }
1640                         return false;
1641                 }
1642
1643                 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1644                 {
1645                         CheckBaked();
1646                         InterfaceMapping map = new InterfaceMapping();
1647                         if (!IsDirectlyImplementedInterface(interfaceType))
1648                         {
1649                                 Type baseType = this.BaseType;
1650                                 if (baseType == null)
1651                                 {
1652                                         throw new ArgumentException();
1653                                 }
1654                                 else
1655                                 {
1656                                         map = baseType.GetInterfaceMap(interfaceType);
1657                                 }
1658                         }
1659                         else
1660                         {
1661                                 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1662                                 map.InterfaceType = interfaceType;
1663                                 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1664                                 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1665                                 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1666                                 for (int i = 0; i < map.TargetMethods.Length; i++)
1667                                 {
1668                                         if (map.TargetMethods[i] == null)
1669                                         {
1670                                                 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1671                                                 for (int j = 0; j < methods.Length; j++)
1672                                                 {
1673                                                         if (methods[j].Name == map.InterfaceMethods[i].Name
1674                                                                 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1675                                                         {
1676                                                                 map.TargetMethods[i] = methods[j];
1677                                                         }
1678                                                 }
1679                                         }
1680                                 }
1681                                 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1682                                 {
1683                                         baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1684                                 }
1685                         }
1686                         map.TargetType = this;
1687                         return map;
1688                 }
1689
1690                 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1691                 {
1692                         __MethodImplMap impl = __GetMethodImplMap();
1693                         for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1694                         {
1695                                 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1696                                 {
1697                                         int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1698                                         if (index != -1 && targetMethods[index] == null)
1699                                         {
1700                                                 targetMethods[index] = impl.MethodBodies[i];
1701                                         }
1702                                 }
1703                         }
1704                 }
1705
1706                 Type IGenericContext.GetGenericTypeArgument(int index)
1707                 {
1708                         return GetGenericTypeArgument(index);
1709                 }
1710
1711                 Type IGenericContext.GetGenericMethodArgument(int index)
1712                 {
1713                         throw new BadImageFormatException();
1714                 }
1715
1716                 Type IGenericBinder.BindTypeParameter(Type type)
1717                 {
1718                         return GetGenericTypeArgument(type.GenericParameterPosition);
1719                 }
1720
1721                 Type IGenericBinder.BindMethodParameter(Type type)
1722                 {
1723                         throw new BadImageFormatException();
1724                 }
1725
1726                 internal virtual Type BindTypeParameters(IGenericBinder binder)
1727                 {
1728                         if (IsGenericTypeDefinition)
1729                         {
1730                                 Type[] args = GetGenericArguments();
1731                                 Type.InplaceBindTypeParameters(binder, args);
1732                                 return GenericTypeInstance.Make(this, args, null);
1733                         }
1734                         else
1735                         {
1736                                 return this;
1737                         }
1738                 }
1739
1740                 private static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1741                 {
1742                         for (int i = 0; i < types.Length; i++)
1743                         {
1744                                 types[i] = types[i].BindTypeParameters(binder);
1745                         }
1746                 }
1747
1748                 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1749                 {
1750                         foreach (MethodBase method in __GetDeclaredMethods())
1751                         {
1752                                 if (method.Name == name && method.MethodSignature.Equals(signature))
1753                                 {
1754                                         return method;
1755                                 }
1756                         }
1757                         return null;
1758                 }
1759
1760                 internal virtual FieldInfo FindField(string name, FieldSignature signature)
1761                 {
1762                         foreach (FieldInfo field in __GetDeclaredFields())
1763                         {
1764                                 if (field.Name == name && field.FieldSignature.Equals(signature))
1765                                 {
1766                                         return field;
1767                                 }
1768                         }
1769                         return null;
1770                 }
1771
1772                 internal bool IsAllowMultipleCustomAttribute
1773                 {
1774                         get
1775                         {
1776                                 IList<CustomAttributeData> cad = CustomAttributeData.__GetCustomAttributes(this, this.Module.universe.System_AttributeUsageAttribute, false);
1777                                 if (cad.Count == 1)
1778                                 {
1779                                         foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1780                                         {
1781                                                 if (arg.MemberInfo.Name == "AllowMultiple")
1782                                                 {
1783                                                         return (bool)arg.TypedValue.Value;
1784                                                 }
1785                                         }
1786                                 }
1787                                 return false;
1788                         }
1789                 }
1790
1791                 internal bool IsPseudoCustomAttribute
1792                 {
1793                         get
1794                         {
1795                                 Universe u = this.Module.universe;
1796                                 return this == u.System_NonSerializedAttribute
1797                                         || this == u.System_SerializableAttribute
1798                                         || this == u.System_Runtime_InteropServices_DllImportAttribute
1799                                         || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1800                                         || this == u.System_Runtime_InteropServices_InAttribute
1801                                         || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1802                                         || this == u.System_Runtime_InteropServices_OutAttribute
1803                                         || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1804                                         || this == u.System_Runtime_InteropServices_OptionalAttribute
1805                                         || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1806                                         || this == u.System_Runtime_InteropServices_ComImportAttribute
1807                                         || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1808                                         || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1809                                         ;
1810                         }
1811                 }
1812
1813                 internal Type MarkNotValueType()
1814                 {
1815                         typeFlags |= TypeFlags.NotValueType;
1816                         return this;
1817                 }
1818
1819                 internal Type MarkValueType()
1820                 {
1821                         typeFlags |= TypeFlags.ValueType;
1822                         return this;
1823                 }
1824
1825                 internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
1826                 {
1827                         Universe u = this.Module.universe;
1828                         MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, new PackedCustomModifiers(), CallingConventions.Standard | CallingConventions.HasThis, 0);
1829                         MethodBase mb =
1830                                 FindMethod(".ctor", methodSig) ??
1831                                 u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
1832                         return (ConstructorInfo)mb;
1833                 }
1834
1835                 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1836                 {
1837                         return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length));
1838                 }
1839
1840                 private MethodBase CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
1841                 {
1842                         MethodSignature sig = new MethodSignature(
1843                                 returnType ?? this.Module.universe.System_Void,
1844                                 Util.Copy(parameterTypes),
1845                                 customModifiers,
1846                                 callingConvention,
1847                                 0);
1848                         MethodInfo method = new MissingMethod(this, name, sig);
1849                         if (name == ".ctor" || name == ".cctor")
1850                         {
1851                                 return new ConstructorInfoImpl(method);
1852                         }
1853                         return method;
1854                 }
1855
1856                 [Obsolete("Please use __CreateMissingMethod(string, CallingConventions, Type, CustomModifiers, Type[], CustomModifiers[]) instead")]
1857                 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
1858                 {
1859                         return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length));
1860                 }
1861
1862                 public FieldInfo __CreateMissingField(string name, Type fieldType, CustomModifiers customModifiers)
1863                 {
1864                         return new MissingField(this, name, FieldSignature.Create(fieldType, customModifiers));
1865                 }
1866
1867                 [Obsolete("Please use __CreateMissingField(string, Type, CustomModifiers) instead")]
1868                 public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1869                 {
1870                         return __CreateMissingField(name, fieldType, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1871                 }
1872
1873                 public PropertyInfo __CreateMissingProperty(string name, CallingConventions callingConvention, Type propertyType, CustomModifiers propertyTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1874                 {
1875                         PropertySignature sig = PropertySignature.Create(callingConvention,
1876                                 propertyType,
1877                                 parameterTypes,
1878                                 PackedCustomModifiers.CreateFromExternal(propertyTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
1879                         return new MissingProperty(this, name, sig);
1880                 }
1881
1882                 internal virtual Type SetMetadataTokenForMissing(int token)
1883                 {
1884                         return this;
1885                 }
1886
1887                 protected void MarkEnumOrValueType(string typeNamespace, string typeName)
1888                 {
1889                         // we assume that mscorlib won't have nested types with these names,
1890                         // so we don't check that we're not a nested type
1891                         if (typeNamespace == "System"
1892                                 && (typeName == "Enum" || typeName == "ValueType"))
1893                         {
1894                                 typeFlags |= TypeFlags.PotentialEnumOrValueType;
1895                         }
1896                 }
1897
1898                 private bool ResolvePotentialEnumOrValueType()
1899                 {
1900                         if (this.Assembly == this.Universe.Mscorlib
1901                                 || this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)
1902                                 // check if mscorlib forwards the type (.NETCore profile reference mscorlib forwards System.Enum and System.ValueType to System.Runtime.dll)
1903                                 || this.Universe.Mscorlib.FindType(new TypeName(__Namespace, __Name)) == this)
1904                         {
1905                                 typeFlags = (typeFlags & ~TypeFlags.PotentialEnumOrValueType) | TypeFlags.EnumOrValueType;
1906                                 return true;
1907                         }
1908                         else
1909                         {
1910                                 typeFlags &= ~TypeFlags.PotentialEnumOrValueType;
1911                                 return false;
1912                         }
1913                 }
1914
1915                 private bool IsEnumOrValueType
1916                 {
1917                         get
1918                         {
1919                                 return (typeFlags & (TypeFlags.EnumOrValueType | TypeFlags.PotentialEnumOrValueType)) != 0
1920                                         && ((typeFlags & TypeFlags.EnumOrValueType) != 0 || ResolvePotentialEnumOrValueType());
1921                         }
1922                 }
1923
1924                 internal virtual Universe Universe
1925                 {
1926                         get { return Module.universe; }
1927                 }
1928
1929                 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
1930                 {
1931                         return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic);
1932                 }
1933
1934                 internal sealed override MemberInfo SetReflectedType(Type type)
1935                 {
1936                         throw new InvalidOperationException();
1937                 }
1938
1939                 internal override int GetCurrentToken()
1940                 {
1941                         return this.MetadataToken;
1942                 }
1943
1944                 internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
1945                 {
1946                         // types don't have pseudo custom attributes
1947                         return null;
1948                 }
1949         }
1950
1951         abstract class ElementHolderType : Type
1952         {
1953                 protected readonly Type elementType;
1954                 private int token;
1955                 private readonly CustomModifiers mods;
1956
1957                 protected ElementHolderType(Type elementType, CustomModifiers mods)
1958                 {
1959                         this.elementType = elementType;
1960                         this.mods = mods;
1961                 }
1962
1963                 protected bool EqualsHelper(ElementHolderType other)
1964                 {
1965                         return other != null
1966                                 && other.elementType.Equals(elementType)
1967                                 && other.mods.Equals(mods);
1968                 }
1969
1970                 public override CustomModifiers __GetCustomModifiers()
1971                 {
1972                         return mods;
1973                 }
1974
1975                 public sealed override string Name
1976                 {
1977                         get { return elementType.Name + GetSuffix(); }
1978                 }
1979
1980                 public sealed override string Namespace
1981                 {
1982                         get { return elementType.Namespace; }
1983                 }
1984
1985                 public sealed override string FullName
1986                 {
1987                         get { return elementType.FullName + GetSuffix(); }
1988                 }
1989
1990                 public sealed override string ToString()
1991                 {
1992                         return elementType.ToString() + GetSuffix();
1993                 }
1994
1995                 public sealed override Type GetElementType()
1996                 {
1997                         return elementType;
1998                 }
1999
2000                 public sealed override bool HasElementType
2001                 {
2002                         get { return true; }
2003                 }
2004
2005                 public sealed override Module Module
2006                 {
2007                         get { return elementType.Module; }
2008                 }
2009
2010                 internal sealed override int GetModuleBuilderToken()
2011                 {
2012                         if (token == 0)
2013                         {
2014                                 token = ((ModuleBuilder)elementType.Module).ImportType(this);
2015                         }
2016                         return token;
2017                 }
2018
2019                 public sealed override bool ContainsGenericParameters
2020                 {
2021                         get
2022                         {
2023                                 Type type = elementType;
2024                                 while (type.HasElementType)
2025                                 {
2026                                         type = type.GetElementType();
2027                                 }
2028                                 return type.ContainsGenericParameters;
2029                         }
2030                 }
2031
2032                 public sealed override bool __ContainsMissingType
2033                 {
2034                         get
2035                         {
2036                                 Type type = elementType;
2037                                 while (type.HasElementType)
2038                                 {
2039                                         type = type.GetElementType();
2040                                 }
2041                                 return type.__ContainsMissingType;
2042                         }
2043                 }
2044
2045                 internal sealed override Type BindTypeParameters(IGenericBinder binder)
2046                 {
2047                         Type type = elementType.BindTypeParameters(binder);
2048                         CustomModifiers mods = this.mods.Bind(binder);
2049                         if (ReferenceEquals(type, elementType)
2050                                 && mods.Equals(this.mods))
2051                         {
2052                                 return this;
2053                         }
2054                         return Wrap(type, mods);
2055                 }
2056
2057                 internal override void CheckBaked()
2058                 {
2059                         elementType.CheckBaked();
2060                 }
2061
2062                 internal sealed override Universe Universe
2063                 {
2064                         get { return elementType.Universe; }
2065                 }
2066
2067                 internal sealed override bool IsBaked
2068                 {
2069                         get { return elementType.IsBaked; }
2070                 }
2071
2072                 internal sealed override int GetCurrentToken()
2073                 {
2074                         // we don't have a token, so we return 0 (which is never a valid token)
2075                         return 0;
2076                 }
2077
2078                 internal abstract string GetSuffix();
2079
2080                 protected abstract Type Wrap(Type type, CustomModifiers mods);
2081         }
2082
2083         sealed class ArrayType : ElementHolderType
2084         {
2085                 internal static Type Make(Type type, CustomModifiers mods)
2086                 {
2087                         return type.Universe.CanonicalizeType(new ArrayType(type, mods));
2088                 }
2089
2090                 private ArrayType(Type type, CustomModifiers mods)
2091                         : base(type, mods)
2092                 {
2093                 }
2094
2095                 public override Type BaseType
2096                 {
2097                         get { return elementType.Module.universe.System_Array; }
2098                 }
2099
2100                 public override Type[] __GetDeclaredInterfaces()
2101                 {
2102                         return new Type[] {
2103                                 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
2104                                 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
2105                                 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
2106                         };
2107                 }
2108
2109                 public override MethodBase[] __GetDeclaredMethods()
2110                 {
2111                         Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
2112                         List<MethodBase> list = new List<MethodBase>();
2113                         list.Add(new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, new Type[] { this.Module.universe.System_Int32, elementType }));
2114                         list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
2115                         list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
2116                         list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2117                         for (Type type = elementType; type.__IsVector; type = type.GetElementType())
2118                         {
2119                                 Array.Resize(ref int32, int32.Length + 1);
2120                                 int32[int32.Length - 1] = int32[0];
2121                                 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2122                         }
2123                         return list.ToArray();
2124                 }
2125
2126                 public override TypeAttributes Attributes
2127                 {
2128                         get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2129                 }
2130
2131                 public override bool IsArray
2132                 {
2133                         get { return true; }
2134                 }
2135
2136                 public override bool __IsVector
2137                 {
2138                         get { return true; }
2139                 }
2140
2141                 public override int GetArrayRank()
2142                 {
2143                         return 1;
2144                 }
2145
2146                 public override bool Equals(object o)
2147                 {
2148                         return EqualsHelper(o as ArrayType);
2149                 }
2150
2151                 public override int GetHashCode()
2152                 {
2153                         return elementType.GetHashCode() * 5;
2154                 }
2155
2156                 internal override string GetSuffix()
2157                 {
2158                         return "[]";
2159                 }
2160
2161                 protected override Type Wrap(Type type, CustomModifiers mods)
2162                 {
2163                         return Make(type, mods);
2164                 }
2165         }
2166
2167         sealed class MultiArrayType : ElementHolderType
2168         {
2169                 private readonly int rank;
2170                 private readonly int[] sizes;
2171                 private readonly int[] lobounds;
2172
2173                 internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2174                 {
2175                         return type.Universe.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, mods));
2176                 }
2177
2178                 private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2179                         : base(type, mods)
2180                 {
2181                         this.rank = rank;
2182                         this.sizes = sizes;
2183                         this.lobounds = lobounds;
2184                 }
2185
2186                 public override Type BaseType
2187                 {
2188                         get { return elementType.Module.universe.System_Array; }
2189                 }
2190
2191                 public override MethodBase[] __GetDeclaredMethods()
2192                 {
2193                         Type int32 = this.Module.universe.System_Int32;
2194                         Type[] setArgs = new Type[rank + 1];
2195                         Type[] getArgs = new Type[rank];
2196                         Type[] ctorArgs = new Type[rank * 2];
2197                         for (int i = 0; i < rank; i++)
2198                         {
2199                                 setArgs[i] = int32;
2200                                 getArgs[i] = int32;
2201                                 ctorArgs[i * 2 + 0] = int32;
2202                                 ctorArgs[i * 2 + 1] = int32;
2203                         }
2204                         setArgs[rank] = elementType;
2205                         return new MethodBase[] {
2206                                 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
2207                                 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
2208                                 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
2209                                 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
2210                                 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
2211                         };
2212                 }
2213
2214                 public override TypeAttributes Attributes
2215                 {
2216                         get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2217                 }
2218
2219                 public override bool IsArray
2220                 {
2221                         get { return true; }
2222                 }
2223
2224                 public override int GetArrayRank()
2225                 {
2226                         return rank;
2227                 }
2228
2229                 public override int[] __GetArraySizes()
2230                 {
2231                         return Util.Copy(sizes);
2232                 }
2233
2234                 public override int[] __GetArrayLowerBounds()
2235                 {
2236                         return Util.Copy(lobounds);
2237                 }
2238
2239                 public override bool Equals(object o)
2240                 {
2241                         MultiArrayType at = o as MultiArrayType;
2242                         return EqualsHelper(at)
2243                                 && at.rank == rank
2244                                 && ArrayEquals(at.sizes, sizes)
2245                                 && ArrayEquals(at.lobounds, lobounds);
2246                 }
2247
2248                 private static bool ArrayEquals(int[] i1, int[] i2)
2249                 {
2250                         if (i1.Length == i2.Length)
2251                         {
2252                                 for (int i = 0; i < i1.Length; i++)
2253                                 {
2254                                         if (i1[i] != i2[i])
2255                                         {
2256                                                 return false;
2257                                         }
2258                                 }
2259                                 return true;
2260                         }
2261                         return false;
2262                 }
2263
2264                 public override int GetHashCode()
2265                 {
2266                         return elementType.GetHashCode() * 9 + rank;
2267                 }
2268
2269                 internal override string GetSuffix()
2270                 {
2271                         if (rank == 1)
2272                         {
2273                                 return "[*]";
2274                         }
2275                         else
2276                         {
2277                                 return "[" + new String(',', rank - 1) + "]";
2278                         }
2279                 }
2280
2281                 protected override Type Wrap(Type type, CustomModifiers mods)
2282                 {
2283                         return Make(type, rank, sizes, lobounds, mods);
2284                 }
2285         }
2286
2287         sealed class BuiltinArrayMethod : ArrayMethod
2288         {
2289                 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
2290                         : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
2291                 {
2292                 }
2293
2294                 public override MethodAttributes Attributes
2295                 {
2296                         get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
2297                 }
2298
2299                 public override MethodImplAttributes GetMethodImplementationFlags()
2300                 {
2301                         return MethodImplAttributes.IL;
2302                 }
2303
2304                 public override int MetadataToken
2305                 {
2306                         get { return 0x06000000; }
2307                 }
2308
2309                 public override MethodBody GetMethodBody()
2310                 {
2311                         return null;
2312                 }
2313
2314                 public override ParameterInfo[] GetParameters()
2315                 {
2316                         ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
2317                         for (int i = 0; i < parameterInfos.Length; i++)
2318                         {
2319                                 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
2320                         }
2321                         return parameterInfos;
2322                 }
2323
2324                 public override ParameterInfo ReturnParameter
2325                 {
2326                         get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
2327                 }
2328
2329                 private sealed class ParameterInfoImpl : ParameterInfo
2330                 {
2331                         private readonly MethodInfo method;
2332                         private readonly Type type;
2333                         private readonly int pos;
2334
2335                         internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
2336                         {
2337                                 this.method = method;
2338                                 this.type = type;
2339                                 this.pos = pos;
2340                         }
2341
2342                         public override Type ParameterType
2343                         {
2344                                 get { return type; }
2345                         }
2346
2347                         public override string Name
2348                         {
2349                                 get { return null; }
2350                         }
2351
2352                         public override ParameterAttributes Attributes
2353                         {
2354                                 get { return ParameterAttributes.None; }
2355                         }
2356
2357                         public override int Position
2358                         {
2359                                 get { return pos; }
2360                         }
2361
2362                         public override object RawDefaultValue
2363                         {
2364                                 get { return null; }
2365                         }
2366
2367                         public override CustomModifiers __GetCustomModifiers()
2368                         {
2369                                 return new CustomModifiers();
2370                         }
2371
2372                         public override bool __TryGetFieldMarshal(out FieldMarshal fieldMarshal)
2373                         {
2374                                 fieldMarshal = new FieldMarshal();
2375                                 return false;
2376                         }
2377
2378                         public override MemberInfo Member
2379                         {
2380                                 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2381                         }
2382
2383                         public override int MetadataToken
2384                         {
2385                                 get { return 0x08000000; }
2386                         }
2387
2388                         internal override Module Module
2389                         {
2390                                 get { return method.Module; }
2391                         }
2392                 }
2393         }
2394
2395         sealed class ByRefType : ElementHolderType
2396         {
2397                 internal static Type Make(Type type, CustomModifiers mods)
2398                 {
2399                         return type.Universe.CanonicalizeType(new ByRefType(type, mods));
2400                 }
2401
2402                 private ByRefType(Type type, CustomModifiers mods)
2403                         : base(type, mods)
2404                 {
2405                 }
2406
2407                 public override bool Equals(object o)
2408                 {
2409                         return EqualsHelper(o as ByRefType);
2410                 }
2411
2412                 public override int GetHashCode()
2413                 {
2414                         return elementType.GetHashCode() * 3;
2415                 }
2416
2417                 public override Type BaseType
2418                 {
2419                         get { return null; }
2420                 }
2421
2422                 public override TypeAttributes Attributes
2423                 {
2424                         get { return 0; }
2425                 }
2426
2427                 public override bool IsByRef
2428                 {
2429                         get { return true; }
2430                 }
2431
2432                 internal override string GetSuffix()
2433                 {
2434                         return "&";
2435                 }
2436
2437                 protected override Type Wrap(Type type, CustomModifiers mods)
2438                 {
2439                         return Make(type, mods);
2440                 }
2441         }
2442
2443         sealed class PointerType : ElementHolderType
2444         {
2445                 internal static Type Make(Type type, CustomModifiers mods)
2446                 {
2447                         return type.Universe.CanonicalizeType(new PointerType(type, mods));
2448                 }
2449
2450                 private PointerType(Type type, CustomModifiers mods)
2451                         : base(type, mods)
2452                 {
2453                 }
2454
2455                 public override bool Equals(object o)
2456                 {
2457                         return EqualsHelper(o as PointerType);
2458                 }
2459
2460                 public override int GetHashCode()
2461                 {
2462                         return elementType.GetHashCode() * 7;
2463                 }
2464
2465                 public override Type BaseType
2466                 {
2467                         get { return null; }
2468                 }
2469
2470                 public override TypeAttributes Attributes
2471                 {
2472                         get { return 0; }
2473                 }
2474
2475                 public override bool IsPointer
2476                 {
2477                         get { return true; }
2478                 }
2479
2480                 internal override string GetSuffix()
2481                 {
2482                         return "*";
2483                 }
2484
2485                 protected override Type Wrap(Type type, CustomModifiers mods)
2486                 {
2487                         return Make(type, mods);
2488                 }
2489         }
2490
2491         sealed class GenericTypeInstance : Type
2492         {
2493                 private readonly Type type;
2494                 private readonly Type[] args;
2495                 private readonly CustomModifiers[] mods;
2496                 private Type baseType;
2497                 private int token;
2498
2499                 internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
2500                 {
2501                         bool identity = true;
2502                         if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2503                         {
2504                                 // a TypeBuiler identity must be instantiated
2505                                 identity = false;
2506                         }
2507                         else
2508                         {
2509                                 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2510                                 for (int i = 0; i < typeArguments.Length; i++)
2511                                 {
2512                                         if (typeArguments[i] != type.GetGenericTypeArgument(i)
2513                                                 || !IsEmpty(mods, i))
2514                                         {
2515                                                 identity = false;
2516                                                 break;
2517                                         }
2518                                 }
2519                         }
2520                         if (identity)
2521                         {
2522                                 return type;
2523                         }
2524                         else
2525                         {
2526                                 return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
2527                         }
2528                 }
2529
2530                 private static bool IsEmpty(CustomModifiers[] mods, int i)
2531                 {
2532                         // we need to be extra careful, because mods doesn't not need to be in canonical format
2533                         // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2534                         return mods == null || mods[i].IsEmpty;
2535                 }
2536
2537                 private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
2538                 {
2539                         this.type = type;
2540                         this.args = args;
2541                         this.mods = mods;
2542                 }
2543
2544                 public override bool Equals(object o)
2545                 {
2546                         GenericTypeInstance gt = o as GenericTypeInstance;
2547                         return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2548                                 && Util.ArrayEquals(gt.mods, mods);
2549                 }
2550
2551                 public override int GetHashCode()
2552                 {
2553                         return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2554                 }
2555
2556                 public override string AssemblyQualifiedName
2557                 {
2558                         get
2559                         {
2560                                 string fn = FullName;
2561                                 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2562                         }
2563                 }
2564
2565                 public override Type BaseType
2566                 {
2567                         get
2568                         {
2569                                 if (baseType == null)
2570                                 {
2571                                         Type rawBaseType = type.BaseType;
2572                                         if (rawBaseType == null)
2573                                         {
2574                                                 baseType = rawBaseType;
2575                                         }
2576                                         else
2577                                         {
2578                                                 baseType = rawBaseType.BindTypeParameters(this);
2579                                         }
2580                                 }
2581                                 return baseType;
2582                         }
2583                 }
2584
2585                 public override bool IsValueType
2586                 {
2587                         get { return type.IsValueType; }
2588                 }
2589
2590                 public override bool IsVisible
2591                 {
2592                         get
2593                         {
2594                                 if (base.IsVisible)
2595                                 {
2596                                         foreach (Type arg in args)
2597                                         {
2598                                                 if (!arg.IsVisible)
2599                                                 {
2600                                                         return false;
2601                                                 }
2602                                         }
2603                                         return true;
2604                                 }
2605                                 return false;
2606                         }
2607                 }
2608
2609                 public override Type DeclaringType
2610                 {
2611                         get { return type.DeclaringType; }
2612                 }
2613
2614                 public override TypeAttributes Attributes
2615                 {
2616                         get { return type.Attributes; }
2617                 }
2618
2619                 internal override void CheckBaked()
2620                 {
2621                         type.CheckBaked();
2622                 }
2623
2624                 public override FieldInfo[] __GetDeclaredFields()
2625                 {
2626                         FieldInfo[] fields = type.__GetDeclaredFields();
2627                         for (int i = 0; i < fields.Length; i++)
2628                         {
2629                                 fields[i] = fields[i].BindTypeParameters(this);
2630                         }
2631                         return fields;
2632                 }
2633
2634                 public override Type[] __GetDeclaredInterfaces()
2635                 {
2636                         Type[] interfaces = type.__GetDeclaredInterfaces();
2637                         for (int i = 0; i < interfaces.Length; i++)
2638                         {
2639                                 interfaces[i] = interfaces[i].BindTypeParameters(this);
2640                         }
2641                         return interfaces;
2642                 }
2643
2644                 public override MethodBase[] __GetDeclaredMethods()
2645                 {
2646                         MethodBase[] methods = type.__GetDeclaredMethods();
2647                         for (int i = 0; i < methods.Length; i++)
2648                         {
2649                                 methods[i] = methods[i].BindTypeParameters(this);
2650                         }
2651                         return methods;
2652                 }
2653
2654                 public override Type[] __GetDeclaredTypes()
2655                 {
2656                         return type.__GetDeclaredTypes();
2657                 }
2658
2659                 public override EventInfo[] __GetDeclaredEvents()
2660                 {
2661                         EventInfo[] events = type.__GetDeclaredEvents();
2662                         for (int i = 0; i < events.Length; i++)
2663                         {
2664                                 events[i] = events[i].BindTypeParameters(this);
2665                         }
2666                         return events;
2667                 }
2668
2669                 public override PropertyInfo[] __GetDeclaredProperties()
2670                 {
2671                         PropertyInfo[] properties = type.__GetDeclaredProperties();
2672                         for (int i = 0; i < properties.Length; i++)
2673                         {
2674                                 properties[i] = properties[i].BindTypeParameters(this);
2675                         }
2676                         return properties;
2677                 }
2678
2679                 public override __MethodImplMap __GetMethodImplMap()
2680                 {
2681                         __MethodImplMap map = type.__GetMethodImplMap();
2682                         map.TargetType = this;
2683                         for (int i = 0; i < map.MethodBodies.Length; i++)
2684                         {
2685                                 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2686                                 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2687                                 {
2688                                         Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2689                                         if (interfaceType.IsGenericType)
2690                                         {
2691                                                 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2692                                         }
2693                                 }
2694                         }
2695                         return map;
2696                 }
2697
2698                 public override string Namespace
2699                 {
2700                         get { return type.Namespace; }
2701                 }
2702
2703                 public override string Name
2704                 {
2705                         get { return type.Name; }
2706                 }
2707
2708                 public override string FullName
2709                 {
2710                         get
2711                         {
2712                                 if (!this.__ContainsMissingType && this.ContainsGenericParameters)
2713                                 {
2714                                         return null;
2715                                 }
2716                                 StringBuilder sb = new StringBuilder(this.type.FullName);
2717                                 sb.Append('[');
2718                                 string sep = "";
2719                                 foreach (Type type in args)
2720                                 {
2721                                         sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
2722                                         sep = ",";
2723                                 }
2724                                 sb.Append(']');
2725                                 return sb.ToString();
2726                         }
2727                 }
2728
2729                 public override string ToString()
2730                 {
2731                         StringBuilder sb = new StringBuilder(type.FullName);
2732                         sb.Append('[');
2733                         string sep = "";
2734                         foreach (Type arg in args)
2735                         {
2736                                 sb.Append(sep);
2737                                 sb.Append(arg);
2738                                 sep = ",";
2739                         }
2740                         sb.Append(']');
2741                         return sb.ToString();
2742                 }
2743
2744                 public override Module Module
2745                 {
2746                         get { return type.Module; }
2747                 }
2748
2749                 public override bool IsGenericType
2750                 {
2751                         get { return true; }
2752                 }
2753
2754                 public override bool IsConstructedGenericType
2755                 {
2756                         get { return true; }
2757                 }
2758
2759                 public override Type GetGenericTypeDefinition()
2760                 {
2761                         return type;
2762                 }
2763
2764                 public override Type[] GetGenericArguments()
2765                 {
2766                         return Util.Copy(args);
2767                 }
2768
2769                 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
2770                 {
2771                         return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
2772                 }
2773
2774                 internal override Type GetGenericTypeArgument(int index)
2775                 {
2776                         return args[index];
2777                 }
2778
2779                 public override bool ContainsGenericParameters
2780                 {
2781                         get
2782                         {
2783                                 foreach (Type type in args)
2784                                 {
2785                                         if (type.ContainsGenericParameters)
2786                                         {
2787                                                 return true;
2788                                         }
2789                                 }
2790                                 return false;
2791                         }
2792                 }
2793
2794                 public override bool __ContainsMissingType
2795                 {
2796                         get
2797                         {
2798                                 foreach (Type type in args)
2799                                 {
2800                                         if (type.__ContainsMissingType)
2801                                         {
2802                                                 return true;
2803                                         }
2804                                 }
2805                                 return this.type.__IsMissing;
2806                         }
2807                 }
2808
2809                 public override StructLayoutAttribute StructLayoutAttribute
2810                 {
2811                         get { return type.StructLayoutAttribute; }
2812                 }
2813
2814                 internal override int GetModuleBuilderToken()
2815                 {
2816                         if (token == 0)
2817                         {
2818                                 token = ((ModuleBuilder)type.Module).ImportType(this);
2819                         }
2820                         return token;
2821                 }
2822
2823                 internal override Type BindTypeParameters(IGenericBinder binder)
2824                 {
2825                         for (int i = 0; i < args.Length; i++)
2826                         {
2827                                 Type xarg = args[i].BindTypeParameters(binder);
2828                                 if (!ReferenceEquals(xarg, args[i]))
2829                                 {
2830                                         Type[] xargs = new Type[args.Length];
2831                                         Array.Copy(args, xargs, i);
2832                                         xargs[i++] = xarg;
2833                                         for (; i < args.Length; i++)
2834                                         {
2835                                                 xargs[i] = args[i].BindTypeParameters(binder);
2836                                         }
2837                                         return Make(type, xargs, null);
2838                                 }
2839                         }
2840                         return this;
2841                 }
2842
2843                 internal override int GetCurrentToken()
2844                 {
2845                         return type.GetCurrentToken();
2846                 }
2847
2848                 internal override bool IsBaked
2849                 {
2850                         get { return type.IsBaked; }
2851                 }
2852         }
2853
2854         sealed class FunctionPointerType : Type
2855         {
2856                 private readonly Universe universe;
2857                 private readonly __StandAloneMethodSig sig;
2858
2859                 internal static Type Make(Universe universe, __StandAloneMethodSig sig)
2860                 {
2861                         return universe.CanonicalizeType(new FunctionPointerType(universe, sig));
2862                 }
2863
2864                 private FunctionPointerType(Universe universe, __StandAloneMethodSig sig)
2865                 {
2866                         this.universe = universe;
2867                         this.sig = sig;
2868                 }
2869
2870                 public override bool Equals(object obj)
2871                 {
2872                         FunctionPointerType other = obj as FunctionPointerType;
2873                         return other != null
2874                                 && other.universe == universe
2875                                 && other.sig.Equals(sig);
2876                 }
2877
2878                 public override int GetHashCode()
2879                 {
2880                         return sig.GetHashCode();
2881                 }
2882
2883                 public override bool __IsFunctionPointer
2884                 {
2885                         get { return true; }
2886                 }
2887
2888                 public override __StandAloneMethodSig __MethodSignature
2889                 {
2890                         get { return sig; }
2891                 }
2892
2893                 public override Type BaseType
2894                 {
2895                         get { return null; }
2896                 }
2897
2898                 public override TypeAttributes Attributes
2899                 {
2900                         get { return 0; }
2901                 }
2902
2903                 public override string Name
2904                 {
2905                         get { throw new InvalidOperationException(); }
2906                 }
2907
2908                 public override string FullName
2909                 {
2910                         get { throw new InvalidOperationException(); }
2911                 }
2912
2913                 public override Module Module
2914                 {
2915                         get { throw new InvalidOperationException(); }
2916                 }
2917
2918                 internal override Universe Universe
2919                 {
2920                         get { return universe; }
2921                 }
2922
2923                 public override string ToString()
2924                 {
2925                         return "<FunctionPtr>";
2926                 }
2927
2928                 internal override bool IsBaked
2929                 {
2930                         get { return true; }
2931                 }
2932         }
2933
2934         sealed class MarkerType : Type
2935         {
2936                 // used by ILGenerator
2937                 internal static readonly Type Fault = new MarkerType();
2938                 internal static readonly Type Finally = new MarkerType();
2939                 internal static readonly Type Filter = new MarkerType();
2940                 // used by CustomModifiers and SignatureHelper
2941                 internal static readonly Type ModOpt = new MarkerType();
2942                 internal static readonly Type ModReq = new MarkerType();
2943                 // used by SignatureHelper
2944                 internal static readonly Type Sentinel = new MarkerType();
2945                 internal static readonly Type Pinned = new MarkerType();
2946
2947                 private MarkerType() { }
2948
2949                 public override Type BaseType
2950                 {
2951                         get { throw new InvalidOperationException(); }
2952                 }
2953
2954                 public override TypeAttributes Attributes
2955                 {
2956                         get { throw new InvalidOperationException(); }
2957                 }
2958
2959                 public override string Name
2960                 {
2961                         get { throw new InvalidOperationException(); }
2962                 }
2963
2964                 public override string FullName
2965                 {
2966                         get { throw new InvalidOperationException(); }
2967                 }
2968
2969                 public override Module Module
2970                 {
2971                         get { throw new InvalidOperationException(); }
2972                 }
2973
2974                 internal override bool IsBaked
2975                 {
2976                         get { throw new InvalidOperationException(); }
2977                 }
2978         }
2979 }