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