2 Copyright (C) 2009-2011 Jeroen Frijters
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.
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:
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.
25 using System.Runtime.InteropServices;
27 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
30 namespace IKVM.Reflection
32 interface IGenericContext
34 Type GetGenericTypeArgument(int index);
35 Type GetGenericMethodArgument(int index);
38 interface IGenericBinder
40 Type BindTypeParameter(Type type);
41 Type BindMethodParameter(Type type);
44 public abstract class Type : MemberInfo, IGenericContext, IGenericBinder
46 public static readonly Type[] EmptyTypes = Empty<Type>.Array;
47 protected readonly Type underlyingType;
48 protected TypeFlags typeFlags;
51 protected enum TypeFlags
53 // for use by TypeBuilder
54 IsGenericTypeDefinition = 1,
58 // for use by MissingType
62 // for use by TypeDef, TypeBuilder or MissingType
66 // prevent subclassing by outsiders
69 this.underlyingType = this;
72 internal Type(Type underlyingType)
74 System.Diagnostics.Debug.Assert(underlyingType.underlyingType == underlyingType);
75 this.underlyingType = underlyingType;
76 this.typeFlags = underlyingType.typeFlags;
79 public static Binder DefaultBinder
84 public sealed override MemberTypes MemberType
86 get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
89 public virtual string AssemblyQualifiedName
91 // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
92 get { return this.FullName + ", " + this.Assembly.FullName; }
95 public abstract Type BaseType
100 public abstract TypeAttributes Attributes
105 public virtual Type GetElementType()
110 internal virtual void CheckBaked()
114 public virtual Type[] __GetDeclaredTypes()
116 return Type.EmptyTypes;
119 public virtual Type[] __GetDeclaredInterfaces()
121 return Type.EmptyTypes;
124 public virtual MethodBase[] __GetDeclaredMethods()
126 return Empty<MethodBase>.Array;
129 public virtual __MethodImplMap __GetMethodImplMap()
131 throw new NotSupportedException();
134 public virtual FieldInfo[] __GetDeclaredFields()
136 return Empty<FieldInfo>.Array;
139 public virtual EventInfo[] __GetDeclaredEvents()
141 return Empty<EventInfo>.Array;
144 public virtual PropertyInfo[] __GetDeclaredProperties()
146 return Empty<PropertyInfo>.Array;
149 public virtual Type[] __GetRequiredCustomModifiers()
151 return Type.EmptyTypes;
154 public virtual Type[] __GetOptionalCustomModifiers()
156 return Type.EmptyTypes;
159 public virtual bool HasElementType
161 get { return false; }
164 public virtual bool IsArray
166 get { return false; }
169 public virtual bool __IsVector
171 get { return false; }
174 public virtual bool IsByRef
176 get { return false; }
179 public virtual bool IsPointer
181 get { return false; }
184 public virtual bool IsValueType
188 Type baseType = this.BaseType;
189 return baseType != null
190 && (baseType.typeFlags & TypeFlags.EnumOrValueType) != 0
191 && (typeFlags & TypeFlags.EnumOrValueType) == 0;
195 public virtual bool IsGenericParameter
197 get { return false; }
200 public virtual int GenericParameterPosition
202 get { throw new NotSupportedException(); }
205 public virtual MethodBase DeclaringMethod
210 public Type UnderlyingSystemType
212 get { return underlyingType; }
215 public override Type DeclaringType
220 public virtual string __Name
222 get { throw new InvalidOperationException(); }
225 public virtual string __Namespace
227 get { throw new InvalidOperationException(); }
230 public abstract override string Name
235 public virtual string Namespace
241 return DeclaringType.Namespace;
247 internal virtual int GetModuleBuilderToken()
249 throw new InvalidOperationException();
252 public static bool operator ==(Type t1, Type t2)
254 // Casting to object results in smaller code than calling ReferenceEquals and makes
255 // this method more likely to be inlined.
256 // On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
257 return (object)t1 == (object)t2
258 || ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
261 public static bool operator !=(Type t1, Type t2)
266 public bool Equals(Type type)
271 public override bool Equals(object obj)
273 return Equals(obj as Type);
276 public override int GetHashCode()
278 Type type = this.UnderlyingSystemType;
279 return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
282 public virtual Type[] GetGenericArguments()
284 return Type.EmptyTypes;
287 public virtual Type[][] __GetGenericArgumentsRequiredCustomModifiers()
289 return Empty<Type[]>.Array;
292 public virtual Type[][] __GetGenericArgumentsOptionalCustomModifiers()
294 return Empty<Type[]>.Array;
297 public virtual Type GetGenericTypeDefinition()
299 throw new InvalidOperationException();
302 public virtual StructLayoutAttribute StructLayoutAttribute
307 public virtual bool __GetLayout(out int packingSize, out int typeSize)
314 public virtual bool IsGenericType
316 get { return false; }
319 public virtual bool IsGenericTypeDefinition
321 get { return false; }
324 internal virtual bool IsGenericTypeInstance
326 get { return false; }
329 public virtual bool ContainsGenericParameters
333 if (this.IsGenericParameter)
337 foreach (Type arg in this.GetGenericArguments())
339 if (arg.ContainsGenericParameters)
348 public virtual Type[] GetGenericParameterConstraints()
350 throw new InvalidOperationException();
353 public virtual GenericParameterAttributes GenericParameterAttributes
355 get { throw new InvalidOperationException(); }
358 public virtual int GetArrayRank()
360 throw new NotSupportedException();
363 public virtual int[] __GetArraySizes()
365 throw new NotSupportedException();
368 public virtual int[] __GetArrayLowerBounds()
370 throw new NotSupportedException();
374 public virtual Type GetEnumUnderlyingType()
378 throw new ArgumentException();
381 return GetEnumUnderlyingTypeImpl();
384 internal Type GetEnumUnderlyingTypeImpl()
386 foreach (FieldInfo field in __GetDeclaredFields())
390 // the CLR assumes that an enum has only one instance field, so we can do the same
391 return field.FieldType;
394 throw new InvalidOperationException();
397 public override string ToString()
402 public abstract string FullName
407 protected string GetFullName()
409 string ns = TypeNameParser.Escape(this.__Namespace);
410 Type decl = this.DeclaringType;
419 return ns + "." + this.Name;
426 return decl.FullName + "+" + this.Name;
430 return decl.FullName + "+" + ns + "." + this.Name;
435 internal virtual bool IsModulePseudoType
437 get { return false; }
440 internal virtual Type GetGenericTypeArgument(int index)
442 throw new InvalidOperationException();
445 public MemberInfo[] GetDefaultMembers()
447 Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
448 foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
450 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
452 return GetMember((string)cad.ConstructorArguments[0].Value);
455 return Empty<MemberInfo>.Array;
458 public MemberInfo[] GetMember(string name)
460 return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
463 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
465 return GetMember(name, MemberTypes.All, bindingAttr);
468 public MemberInfo[] GetMembers()
470 return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
473 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
475 List<MemberInfo> members = new List<MemberInfo>();
476 members.AddRange(GetConstructors(bindingAttr));
477 members.AddRange(GetMethods(bindingAttr));
478 members.AddRange(GetFields(bindingAttr));
479 members.AddRange(GetProperties(bindingAttr));
480 members.AddRange(GetEvents(bindingAttr));
481 members.AddRange(GetNestedTypes(bindingAttr));
482 return members.ToArray();
485 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
487 MemberFilter filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
488 return FindMembers(type, bindingAttr, filter, name);
491 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
493 foreach (MemberInfo member in members)
495 if (filter == null || filter(member, filterCriteria))
502 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
504 List<MemberInfo> members = new List<MemberInfo>();
505 if ((memberType & MemberTypes.Constructor) != 0)
507 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
509 if ((memberType & MemberTypes.Method) != 0)
511 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
513 if ((memberType & MemberTypes.Field) != 0)
515 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
517 if ((memberType & MemberTypes.Property) != 0)
519 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
521 if ((memberType & MemberTypes.Event) != 0)
523 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
525 if ((memberType & MemberTypes.NestedType) != 0)
527 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
529 return members.ToArray();
532 public EventInfo GetEvent(string name)
534 return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
537 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
539 foreach (EventInfo evt in GetEvents(bindingAttr))
541 if (evt.Name == name)
549 public EventInfo[] GetEvents()
551 return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
554 public EventInfo[] GetEvents(BindingFlags bindingAttr)
556 List<EventInfo> list = new List<EventInfo>();
561 foreach (EventInfo evt in type.__GetDeclaredEvents())
563 if (BindingFlagsMatch(evt.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
564 && BindingFlagsMatch(evt.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
569 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
571 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
573 bindingAttr &= ~BindingFlags.Static;
575 type = type.BaseType;
582 return list.ToArray();
585 public FieldInfo GetField(string name)
587 return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
590 public FieldInfo GetField(string name, BindingFlags bindingAttr)
592 foreach (FieldInfo field in GetFields(bindingAttr))
594 if (field.Name == name)
602 public FieldInfo[] GetFields()
604 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
607 public FieldInfo[] GetFields(BindingFlags bindingAttr)
609 List<FieldInfo> list = new List<FieldInfo>();
611 foreach (FieldInfo field in __GetDeclaredFields())
613 if (BindingFlagsMatch(field.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
614 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
619 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
621 for (Type type = this.BaseType; type != null; type = type.BaseType)
624 foreach (FieldInfo field in type.__GetDeclaredFields())
626 if ((field.Attributes & FieldAttributes.FieldAccessMask) > FieldAttributes.Private
627 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance))
634 return list.ToArray();
637 public Type[] GetInterfaces()
639 List<Type> list = new List<Type>();
640 for (Type type = this; type != null; type = type.BaseType)
642 AddInterfaces(list, type);
644 return list.ToArray();
647 private static void AddInterfaces(List<Type> list, Type type)
650 foreach (Type iface in type.__GetDeclaredInterfaces())
652 if (!list.Contains(iface))
655 AddInterfaces(list, iface);
660 public MethodInfo[] GetMethods(BindingFlags bindingAttr)
663 List<MethodInfo> list = new List<MethodInfo>();
664 foreach (MethodBase mb in __GetDeclaredMethods())
666 MethodInfo mi = mb as MethodInfo;
668 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
669 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
674 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
676 for (Type type = this.BaseType; type != null; type = type.BaseType)
679 foreach (MethodBase mb in type.__GetDeclaredMethods())
681 MethodInfo mi = mb as MethodInfo;
683 && (mi.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private
684 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
685 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance)
686 && !FindMethod(list, mi))
693 return list.ToArray();
696 private static bool FindMethod(List<MethodInfo> methods, MethodInfo method)
698 foreach (MethodInfo m in methods)
700 if (m.Name == method.Name && m.MethodSignature.Equals(method.MethodSignature))
708 public MethodInfo[] GetMethods()
710 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
713 public MethodInfo GetMethod(string name)
715 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
718 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
720 MethodInfo found = null;
721 foreach (MethodInfo method in GetMethods(bindingAttr))
723 if (method.Name == name)
727 throw new AmbiguousMatchException();
735 public MethodInfo GetMethod(string name, Type[] types)
737 return GetMethod(name, types, null);
740 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
742 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
745 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
747 MethodInfo found = null;
748 foreach (MethodInfo method in GetMethods(bindingAttr))
750 if (method.Name == name && method.MethodSignature.MatchParameterTypes(types))
754 throw new AmbiguousMatchException();
762 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
764 // FXBUG callConvention seems to be ignored
765 return GetMethod(name, bindingAttr, binder, types, modifiers);
768 public ConstructorInfo[] GetConstructors()
770 return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
773 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
776 List<ConstructorInfo> list = new List<ConstructorInfo>();
777 foreach (MethodBase mb in __GetDeclaredMethods())
779 ConstructorInfo constructor = mb as ConstructorInfo;
780 if (constructor != null
781 && BindingFlagsMatch(constructor.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
782 && BindingFlagsMatch(constructor.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
784 list.Add(constructor);
787 return list.ToArray();
790 public ConstructorInfo GetConstructor(Type[] types)
792 return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
795 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
797 foreach (ConstructorInfo constructor in GetConstructors(bindingAttr))
799 if (constructor.MethodSignature.MatchParameterTypes(types))
807 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
809 // FXBUG callConvention seems to be ignored
810 return GetConstructor(bindingAttr, binder, types, modifiers);
813 internal Type ResolveNestedType(TypeName typeName)
815 return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
818 // unlike the public API, this takes the namespace and name into account
819 internal virtual Type FindNestedType(TypeName name)
821 foreach (Type type in __GetDeclaredTypes())
823 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
831 public Type GetNestedType(string name)
833 return GetNestedType(name, BindingFlags.Public);
836 public Type GetNestedType(string name, BindingFlags bindingAttr)
838 foreach (Type type in GetNestedTypes(bindingAttr))
840 // FXBUG the namespace is ignored
841 if (type.__Name == name)
849 public Type[] GetNestedTypes()
851 return GetNestedTypes(BindingFlags.Public);
854 public Type[] GetNestedTypes(BindingFlags bindingAttr)
857 List<Type> list = new List<Type>();
858 foreach (Type type in __GetDeclaredTypes())
860 if (BindingFlagsMatch(type.IsNestedPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic))
865 return list.ToArray();
868 public PropertyInfo[] GetProperties()
870 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
873 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
875 List<PropertyInfo> list = new List<PropertyInfo>();
880 foreach (PropertyInfo property in type.__GetDeclaredProperties())
882 if (BindingFlagsMatch(property.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
883 && BindingFlagsMatch(property.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
888 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
890 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
892 bindingAttr &= ~BindingFlags.Static;
894 type = type.BaseType;
901 return list.ToArray();
904 public PropertyInfo GetProperty(string name)
906 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
909 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
911 foreach (PropertyInfo prop in GetProperties(bindingAttr))
913 if (prop.Name == name)
921 public PropertyInfo GetProperty(string name, Type returnType)
923 PropertyInfo found = null;
924 foreach (PropertyInfo prop in GetProperties())
926 if (prop.Name == name && prop.PropertyType.Equals(returnType))
930 throw new AmbiguousMatchException();
938 public PropertyInfo GetProperty(string name, Type[] types)
940 PropertyInfo found = null;
941 foreach (PropertyInfo prop in GetProperties())
943 if (prop.Name == name && MatchParameterTypes(prop.GetIndexParameters(), types))
947 throw new AmbiguousMatchException();
955 private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
957 if (parameters.Length == types.Length)
959 for (int i = 0; i < parameters.Length; i++)
961 if (!parameters[i].ParameterType.Equals(types[i]))
971 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
973 return GetProperty(name, returnType, types, null);
976 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
978 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
981 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
983 PropertyInfo found = null;
984 foreach (PropertyInfo prop in GetProperties(bindingAttr))
986 if (prop.Name == name && prop.PropertyType.Equals(returnType) && MatchParameterTypes(prop.GetIndexParameters(), types))
990 throw new AmbiguousMatchException();
998 public Type GetInterface(string name)
1000 return GetInterface(name, false);
1003 public Type GetInterface(string name, bool ignoreCase)
1007 throw new NotImplementedException();
1009 foreach (Type type in GetInterfaces())
1011 if (type.FullName == name)
1019 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
1021 List<Type> list = new List<Type>();
1022 foreach (Type type in GetInterfaces())
1024 if (filter(type, filterCriteria))
1029 return list.ToArray();
1032 public ConstructorInfo TypeInitializer
1034 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
1037 public bool IsPrimitive
1041 Universe u = this.Module.universe;
1042 return this == u.System_Boolean
1043 || this == u.System_Byte
1044 || this == u.System_SByte
1045 || this == u.System_Int16
1046 || this == u.System_UInt16
1047 || this == u.System_Int32
1048 || this == u.System_UInt32
1049 || this == u.System_Int64
1050 || this == u.System_UInt64
1051 || this == u.System_IntPtr
1052 || this == u.System_UIntPtr
1053 || this == u.System_Char
1054 || this == u.System_Double
1055 || this == u.System_Single
1064 Type baseType = this.BaseType;
1065 return baseType != null
1066 && (baseType.typeFlags & TypeFlags.EnumOrValueType) != 0
1067 && baseType.__Name[0] == 'E';
1071 public bool IsSealed
1073 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1076 public bool IsAbstract
1078 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1081 private bool CheckVisibility(TypeAttributes access)
1083 return (Attributes & TypeAttributes.VisibilityMask) == access;
1086 public bool IsPublic
1088 get { return CheckVisibility(TypeAttributes.Public); }
1091 public bool IsNestedPublic
1093 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1096 public bool IsNestedPrivate
1098 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1101 public bool IsNestedFamily
1103 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1106 public bool IsNestedAssembly
1108 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1111 public bool IsNestedFamANDAssem
1113 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1116 public bool IsNestedFamORAssem
1118 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1121 public bool IsNotPublic
1123 get { return CheckVisibility(TypeAttributes.NotPublic); }
1126 public bool IsImport
1128 get { return (Attributes & TypeAttributes.Import) != 0; }
1131 public bool IsCOMObject
1133 get { return IsClass && IsImport; }
1136 public bool IsContextful
1138 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1141 public bool IsMarshalByRef
1143 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1146 public virtual bool IsVisible
1148 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1151 public bool IsAnsiClass
1153 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1156 public bool IsUnicodeClass
1158 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1161 public bool IsAutoClass
1163 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1166 public bool IsAutoLayout
1168 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1171 public bool IsLayoutSequential
1173 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1176 public bool IsExplicitLayout
1178 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1181 public bool IsSpecialName
1183 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1186 public bool IsSerializable
1188 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1193 get { return !IsInterface && !IsValueType; }
1196 public bool IsInterface
1198 get { return (Attributes & TypeAttributes.Interface) != 0; }
1201 public bool IsNested
1203 // FXBUG we check the declaring type (like .NET) and this results
1204 // in IsNested returning true for a generic type parameter
1205 get { return this.DeclaringType != null; }
1208 public virtual bool __ContainsMissingType
1212 if (this.__IsMissing)
1216 foreach (Type arg in this.GetGenericArguments())
1218 if (arg.__ContainsMissingType)
1227 public Type MakeArrayType()
1229 return ArrayType.Make(this, null, null);
1232 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1234 return ArrayType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1237 public Type MakeArrayType(int rank)
1239 return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], null, null);
1242 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1244 return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1247 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1249 return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1252 public Type MakeByRefType()
1254 return ByRefType.Make(this, null, null);
1257 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1259 return ByRefType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1262 public Type MakePointerType()
1264 return PointerType.Make(this, null, null);
1267 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1269 return PointerType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1272 public Type MakeGenericType(params Type[] typeArguments)
1274 return __MakeGenericType(typeArguments, null, null);
1277 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1279 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1281 throw new InvalidOperationException();
1283 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1286 public static System.Type __GetSystemType(TypeCode typeCode)
1290 case TypeCode.Boolean:
1291 return typeof(System.Boolean);
1293 return typeof(System.Byte);
1295 return typeof(System.Char);
1296 case TypeCode.DBNull:
1297 return typeof(System.DBNull);
1298 case TypeCode.DateTime:
1299 return typeof(System.DateTime);
1300 case TypeCode.Decimal:
1301 return typeof(System.Decimal);
1302 case TypeCode.Double:
1303 return typeof(System.Double);
1304 case TypeCode.Empty:
1306 case TypeCode.Int16:
1307 return typeof(System.Int16);
1308 case TypeCode.Int32:
1309 return typeof(System.Int32);
1310 case TypeCode.Int64:
1311 return typeof(System.Int64);
1312 case TypeCode.Object:
1313 return typeof(System.Object);
1314 case TypeCode.SByte:
1315 return typeof(System.SByte);
1316 case TypeCode.Single:
1317 return typeof(System.Single);
1318 case TypeCode.String:
1319 return typeof(System.String);
1320 case TypeCode.UInt16:
1321 return typeof(System.UInt16);
1322 case TypeCode.UInt32:
1323 return typeof(System.UInt32);
1324 case TypeCode.UInt64:
1325 return typeof(System.UInt64);
1327 throw new ArgumentOutOfRangeException();
1331 public static TypeCode GetTypeCode(Type type)
1335 return TypeCode.Empty;
1337 if (!type.__IsMissing && type.IsEnum)
1339 type = type.GetEnumUnderlyingType();
1341 Universe u = type.Module.universe;
1342 if (type == u.System_Boolean)
1344 return TypeCode.Boolean;
1346 else if (type == u.System_Char)
1348 return TypeCode.Char;
1350 else if (type == u.System_SByte)
1352 return TypeCode.SByte;
1354 else if (type == u.System_Byte)
1356 return TypeCode.Byte;
1358 else if (type == u.System_Int16)
1360 return TypeCode.Int16;
1362 else if (type == u.System_UInt16)
1364 return TypeCode.UInt16;
1366 else if (type == u.System_Int32)
1368 return TypeCode.Int32;
1370 else if (type == u.System_UInt32)
1372 return TypeCode.UInt32;
1374 else if (type == u.System_Int64)
1376 return TypeCode.Int64;
1378 else if (type == u.System_UInt64)
1380 return TypeCode.UInt64;
1382 else if (type == u.System_Single)
1384 return TypeCode.Single;
1386 else if (type == u.System_Double)
1388 return TypeCode.Double;
1390 else if (type == u.System_DateTime)
1392 return TypeCode.DateTime;
1394 else if (type == u.System_DBNull)
1396 return TypeCode.DBNull;
1398 else if (type == u.System_Decimal)
1400 return TypeCode.Decimal;
1402 else if (type == u.System_String)
1404 return TypeCode.String;
1406 else if (type.__IsMissing)
1408 throw new MissingMemberException(type);
1412 return TypeCode.Object;
1416 public Assembly Assembly
1418 get { return Module.Assembly; }
1421 // note that interface/delegate co- and contravariance is not considered
1422 public bool IsAssignableFrom(Type type)
1424 if (this.Equals(type))
1428 else if (type == null)
1432 else if (this.IsArray && type.IsArray)
1434 if (this.GetArrayRank() != type.GetArrayRank())
1438 else if (this.__IsVector && !type.__IsVector)
1442 Type e1 = this.GetElementType();
1443 Type e2 = type.GetElementType();
1444 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1446 else if (this.IsSealed)
1450 else if (this.IsInterface)
1452 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1454 else if (type.IsInterface)
1456 return this == this.Module.universe.System_Object;
1458 else if (type.IsPointer)
1460 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1464 return type.IsSubclassOf(this);
1468 public bool IsSubclassOf(Type type)
1470 Type thisType = this.BaseType;
1471 while (thisType != null)
1473 if (thisType.Equals(type))
1477 thisType = thisType.BaseType;
1482 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1483 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1484 private bool IsDirectlyImplementedInterface(Type interfaceType)
1486 foreach (Type iface in __GetDeclaredInterfaces())
1488 if (interfaceType.IsAssignableFrom(iface))
1496 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1499 InterfaceMapping map = new InterfaceMapping();
1500 if (!IsDirectlyImplementedInterface(interfaceType))
1502 Type baseType = this.BaseType;
1503 if (baseType == null)
1505 throw new ArgumentException();
1509 map = baseType.GetInterfaceMap(interfaceType);
1514 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1515 map.InterfaceType = interfaceType;
1516 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1517 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1518 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1519 for (int i = 0; i < map.TargetMethods.Length; i++)
1521 if (map.TargetMethods[i] == null)
1523 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1524 for (int j = 0; j < methods.Length; j++)
1526 if (methods[j].Name == map.InterfaceMethods[i].Name
1527 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1529 map.TargetMethods[i] = methods[j];
1534 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1536 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1539 map.TargetType = this;
1543 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1545 __MethodImplMap impl = __GetMethodImplMap();
1546 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1548 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1550 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1551 if (index != -1 && targetMethods[index] == null)
1553 targetMethods[index] = impl.MethodBodies[i];
1559 Type IGenericContext.GetGenericTypeArgument(int index)
1561 return GetGenericTypeArgument(index);
1564 Type IGenericContext.GetGenericMethodArgument(int index)
1566 throw new BadImageFormatException();
1569 Type IGenericBinder.BindTypeParameter(Type type)
1571 return GetGenericTypeArgument(type.GenericParameterPosition);
1574 Type IGenericBinder.BindMethodParameter(Type type)
1576 throw new BadImageFormatException();
1579 internal virtual Type BindTypeParameters(IGenericBinder binder)
1581 if (IsGenericTypeDefinition)
1583 Type[] args = GetGenericArguments();
1584 Type.InplaceBindTypeParameters(binder, args);
1585 return GenericTypeInstance.Make(this, args, null, null);
1593 internal static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1595 for (int i = 0; i < types.Length; i++)
1597 types[i] = types[i].BindTypeParameters(binder);
1601 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1603 foreach (MethodBase method in __GetDeclaredMethods())
1605 if (method.Name == name && method.MethodSignature.Equals(signature))
1613 internal virtual FieldInfo FindField(string name, FieldSignature signature)
1615 foreach (FieldInfo field in __GetDeclaredFields())
1617 if (field.Name == name && field.FieldSignature.Equals(signature))
1625 internal bool IsAllowMultipleCustomAttribute
1629 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1632 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1634 if (arg.MemberInfo.Name == "AllowMultiple")
1636 return (bool)arg.TypedValue.Value;
1644 internal bool IsPseudoCustomAttribute
1648 Universe u = this.Module.universe;
1649 return this == u.System_NonSerializedAttribute
1650 || this == u.System_SerializableAttribute
1651 || this == u.System_Runtime_InteropServices_DllImportAttribute
1652 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1653 || this == u.System_Runtime_InteropServices_InAttribute
1654 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1655 || this == u.System_Runtime_InteropServices_OutAttribute
1656 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1657 || this == u.System_Runtime_InteropServices_OptionalAttribute
1658 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1659 || this == u.System_Runtime_InteropServices_ComImportAttribute
1660 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1661 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1666 internal Type MarkNotValueType()
1668 typeFlags |= TypeFlags.NotValueType;
1672 internal Type MarkValueType()
1674 typeFlags |= TypeFlags.ValueType;
1678 internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
1680 Universe u = this.Module.universe;
1681 MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, null, CallingConventions.Standard | CallingConventions.HasThis, 0);
1683 FindMethod(".ctor", methodSig) ??
1684 u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
1685 return (ConstructorInfo)mb;
1688 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
1690 MethodSignature sig = new MethodSignature(
1691 returnType ?? this.Module.universe.System_Void,
1692 Util.Copy(parameterTypes),
1693 PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length),
1696 MethodInfo method = new MissingMethod(this, name, sig);
1697 if (name == ".ctor" || name == ".cctor")
1699 return new ConstructorInfoImpl(method);
1704 public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1706 return new MissingField(this, name, FieldSignature.Create(fieldType, optionalCustomModifiers, requiredCustomModifiers));
1709 internal virtual Type SetMetadataTokenForMissing(int token)
1714 protected void MarkEnumOrValueType(string typeNamespace, string typeName)
1716 // we don't assume that mscorlib won't have nested types with these names,
1717 // so we don't check that we're not a nested type
1718 if (typeNamespace == "System"
1719 && (typeName == "Enum" || typeName == "ValueType")
1720 && this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase))
1722 typeFlags |= TypeFlags.EnumOrValueType;
1726 internal virtual IList<CustomAttributeData> GetInterfaceImplCustomAttributes(Type interfaceType, Type attributeType)
1728 throw new NotSupportedException();
1732 abstract class ElementHolderType : Type
1734 protected readonly Type elementType;
1736 private readonly Type[] requiredCustomModifiers;
1737 private readonly Type[] optionalCustomModifiers;
1739 protected ElementHolderType(Type elementType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1741 this.elementType = elementType;
1742 this.requiredCustomModifiers = requiredCustomModifiers;
1743 this.optionalCustomModifiers = optionalCustomModifiers;
1746 protected bool EqualsHelper(ElementHolderType other)
1748 return other != null
1749 && other.elementType.Equals(elementType)
1750 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers)
1751 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers);
1754 public override Type[] __GetRequiredCustomModifiers()
1756 return Util.Copy(requiredCustomModifiers);
1759 public override Type[] __GetOptionalCustomModifiers()
1761 return Util.Copy(optionalCustomModifiers);
1764 public sealed override string Name
1766 get { return elementType.Name + GetSuffix(); }
1769 public sealed override string Namespace
1771 get { return elementType.Namespace; }
1774 public sealed override string FullName
1776 get { return elementType.FullName + GetSuffix(); }
1779 public sealed override string ToString()
1781 return elementType.ToString() + GetSuffix();
1784 public sealed override Type GetElementType()
1789 public sealed override bool HasElementType
1791 get { return true; }
1794 public sealed override Module Module
1796 get { return elementType.Module; }
1799 internal sealed override int GetModuleBuilderToken()
1803 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1808 public sealed override bool ContainsGenericParameters
1812 Type type = elementType;
1813 while (type.HasElementType)
1815 type = type.GetElementType();
1817 return type.ContainsGenericParameters;
1821 public sealed override bool __ContainsMissingType
1825 Type type = elementType;
1826 while (type.HasElementType)
1828 type = type.GetElementType();
1830 return type.__ContainsMissingType;
1834 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1836 Type type = elementType.BindTypeParameters(binder);
1837 Type[] req = BindArray(requiredCustomModifiers, binder);
1838 Type[] opt = BindArray(optionalCustomModifiers, binder);
1839 if (ReferenceEquals(type, elementType)
1840 && ReferenceEquals(req, requiredCustomModifiers)
1841 && ReferenceEquals(opt, optionalCustomModifiers))
1845 return Wrap(type, req, opt);
1848 internal override void CheckBaked()
1850 elementType.CheckBaked();
1853 private static Type[] BindArray(Type[] array, IGenericBinder binder)
1855 if (array ==null || array.Length == 0)
1859 Type[] result = array;
1860 for (int i = 0; i < array.Length; i++)
1862 Type type = array[i].BindTypeParameters(binder);
1863 if (!ReferenceEquals(type, array[i]))
1865 if (result == array)
1867 result = (Type[])array.Clone();
1875 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1877 return CustomAttributeData.EmptyList;
1880 internal abstract string GetSuffix();
1882 protected abstract Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers);
1885 sealed class ArrayType : ElementHolderType
1887 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1889 return type.Module.CanonicalizeType(new ArrayType(type, requiredCustomModifiers, optionalCustomModifiers));
1892 private ArrayType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1893 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1897 public override Type BaseType
1899 get { return elementType.Module.universe.System_Array; }
1902 public override Type[] __GetDeclaredInterfaces()
1905 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
1906 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
1907 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
1911 public override MethodBase[] __GetDeclaredMethods()
1913 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
1914 List<MethodBase> list = new List<MethodBase>();
1915 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 }));
1916 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
1917 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
1918 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1919 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
1921 Array.Resize(ref int32, int32.Length + 1);
1922 int32[int32.Length - 1] = int32[0];
1923 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1925 return list.ToArray();
1928 public override TypeAttributes Attributes
1930 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1933 public override bool IsArray
1935 get { return true; }
1938 public override bool __IsVector
1940 get { return true; }
1943 public override int GetArrayRank()
1948 public override bool Equals(object o)
1950 return EqualsHelper(o as ArrayType);
1953 public override int GetHashCode()
1955 return elementType.GetHashCode() * 5;
1958 internal override string GetSuffix()
1963 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1965 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
1969 sealed class MultiArrayType : ElementHolderType
1971 private readonly int rank;
1972 private readonly int[] sizes;
1973 private readonly int[] lobounds;
1975 internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1977 return type.Module.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, requiredCustomModifiers, optionalCustomModifiers));
1980 private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1981 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1985 this.lobounds = lobounds;
1988 public override Type BaseType
1990 get { return elementType.Module.universe.System_Array; }
1993 public override MethodBase[] __GetDeclaredMethods()
1995 Type int32 = this.Module.universe.System_Int32;
1996 Type[] setArgs = new Type[rank + 1];
1997 Type[] getArgs = new Type[rank];
1998 Type[] ctorArgs = new Type[rank * 2];
1999 for (int i = 0; i < rank; i++)
2003 ctorArgs[i * 2 + 0] = int32;
2004 ctorArgs[i * 2 + 1] = int32;
2006 setArgs[rank] = elementType;
2007 return new MethodBase[] {
2008 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
2009 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
2010 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
2011 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
2012 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
2016 public override TypeAttributes Attributes
2018 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2021 public override bool IsArray
2023 get { return true; }
2026 public override int GetArrayRank()
2031 public override int[] __GetArraySizes()
2033 return Util.Copy(sizes);
2036 public override int[] __GetArrayLowerBounds()
2038 return Util.Copy(lobounds);
2041 public override bool Equals(object o)
2043 MultiArrayType at = o as MultiArrayType;
2044 return EqualsHelper(at)
2046 && ArrayEquals(at.sizes, sizes)
2047 && ArrayEquals(at.lobounds, lobounds);
2050 private static bool ArrayEquals(int[] i1, int[] i2)
2052 if (i1.Length == i2.Length)
2054 for (int i = 0; i < i1.Length; i++)
2066 public override int GetHashCode()
2068 return elementType.GetHashCode() * 9 + rank;
2071 internal override string GetSuffix()
2079 return "[" + new String(',', rank - 1) + "]";
2083 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2085 return Make(type, rank, sizes, lobounds, requiredCustomModifiers, optionalCustomModifiers);
2089 sealed class BuiltinArrayMethod : ArrayMethod
2091 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
2092 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
2096 public override MethodAttributes Attributes
2098 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
2101 public override MethodImplAttributes GetMethodImplementationFlags()
2103 return MethodImplAttributes.IL;
2106 public override int MetadataToken
2108 get { return 0x06000000; }
2111 public override MethodBody GetMethodBody()
2116 public override ParameterInfo[] GetParameters()
2118 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
2119 for (int i = 0; i < parameterInfos.Length; i++)
2121 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
2123 return parameterInfos;
2126 public override ParameterInfo ReturnParameter
2128 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
2131 private sealed class ParameterInfoImpl : ParameterInfo
2133 private readonly MethodInfo method;
2134 private readonly Type type;
2135 private readonly int pos;
2137 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
2139 this.method = method;
2144 public override Type ParameterType
2146 get { return type; }
2149 public override string Name
2151 get { return null; }
2154 public override ParameterAttributes Attributes
2156 get { return ParameterAttributes.None; }
2159 public override int Position
2164 public override object RawDefaultValue
2166 get { return null; }
2169 public override Type[] GetOptionalCustomModifiers()
2171 return Empty<Type>.Array;
2174 public override Type[] GetRequiredCustomModifiers()
2176 return Empty<Type>.Array;
2179 public override MemberInfo Member
2181 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2184 public override int MetadataToken
2186 get { return 0x8000000; }
2189 internal override Module Module
2191 get { return method.Module; }
2196 sealed class ByRefType : ElementHolderType
2198 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2200 return type.Module.CanonicalizeType(new ByRefType(type, requiredCustomModifiers, optionalCustomModifiers));
2203 private ByRefType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2204 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2208 public override bool Equals(object o)
2210 return EqualsHelper(o as ByRefType);
2213 public override int GetHashCode()
2215 return elementType.GetHashCode() * 3;
2218 public override Type BaseType
2220 get { return null; }
2223 public override TypeAttributes Attributes
2228 public override bool IsByRef
2230 get { return true; }
2233 internal override string GetSuffix()
2238 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2240 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2244 sealed class PointerType : ElementHolderType
2246 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2248 return type.Module.CanonicalizeType(new PointerType(type, requiredCustomModifiers, optionalCustomModifiers));
2251 private PointerType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2252 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2256 public override bool Equals(object o)
2258 return EqualsHelper(o as PointerType);
2261 public override int GetHashCode()
2263 return elementType.GetHashCode() * 7;
2266 public override Type BaseType
2268 get { return null; }
2271 public override TypeAttributes Attributes
2276 public override bool IsPointer
2278 get { return true; }
2281 internal override string GetSuffix()
2286 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2288 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2292 sealed class GenericTypeInstance : Type
2294 private readonly Type type;
2295 private readonly Type[] args;
2296 private readonly Type[][] requiredCustomModifiers;
2297 private readonly Type[][] optionalCustomModifiers;
2298 private Type baseType;
2301 internal static Type Make(Type type, Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2303 bool identity = true;
2304 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2306 // a TypeBuiler identity must be instantiated
2311 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2312 for (int i = 0; i < typeArguments.Length; i++)
2314 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2315 || !IsEmpty(requiredCustomModifiers, i)
2316 || !IsEmpty(optionalCustomModifiers, i))
2329 return type.Module.CanonicalizeType(new GenericTypeInstance(type, typeArguments, requiredCustomModifiers, optionalCustomModifiers));
2333 private static bool IsEmpty(Type[][] mods, int i)
2335 // we need to be extra careful, because mods doesn't not need to be in canonical format
2336 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2337 return mods == null || mods[i] == null || mods[i].Length == 0;
2340 private GenericTypeInstance(Type type, Type[] args, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2344 this.requiredCustomModifiers = requiredCustomModifiers;
2345 this.optionalCustomModifiers = optionalCustomModifiers;
2348 public override bool Equals(object o)
2350 GenericTypeInstance gt = o as GenericTypeInstance;
2351 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2352 && Util.ArrayEquals(gt.requiredCustomModifiers, requiredCustomModifiers)
2353 && Util.ArrayEquals(gt.optionalCustomModifiers, optionalCustomModifiers);
2356 public override int GetHashCode()
2358 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2361 public override string AssemblyQualifiedName
2365 string fn = FullName;
2366 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2370 public override Type BaseType
2374 if (baseType == null)
2376 Type rawBaseType = type.BaseType;
2377 if (rawBaseType == null)
2379 baseType = rawBaseType;
2383 baseType = rawBaseType.BindTypeParameters(this);
2390 public override bool IsValueType
2392 get { return type.IsValueType; }
2395 public override bool IsVisible
2401 foreach (Type arg in args)
2414 public override Type DeclaringType
2416 get { return type.DeclaringType; }
2419 public override TypeAttributes Attributes
2421 get { return type.Attributes; }
2424 internal override void CheckBaked()
2429 public override FieldInfo[] __GetDeclaredFields()
2431 FieldInfo[] fields = type.__GetDeclaredFields();
2432 for (int i = 0; i < fields.Length; i++)
2434 fields[i] = fields[i].BindTypeParameters(this);
2439 public override Type[] __GetDeclaredInterfaces()
2441 Type[] interfaces = type.__GetDeclaredInterfaces();
2442 for (int i = 0; i < interfaces.Length; i++)
2444 interfaces[i] = interfaces[i].BindTypeParameters(this);
2449 public override MethodBase[] __GetDeclaredMethods()
2451 MethodBase[] methods = type.__GetDeclaredMethods();
2452 for (int i = 0; i < methods.Length; i++)
2454 methods[i] = methods[i].BindTypeParameters(this);
2459 public override Type[] __GetDeclaredTypes()
2461 return type.__GetDeclaredTypes();
2464 public override EventInfo[] __GetDeclaredEvents()
2466 EventInfo[] events = type.__GetDeclaredEvents();
2467 for (int i = 0; i < events.Length; i++)
2469 events[i] = events[i].BindTypeParameters(this);
2474 public override PropertyInfo[] __GetDeclaredProperties()
2476 PropertyInfo[] properties = type.__GetDeclaredProperties();
2477 for (int i = 0; i < properties.Length; i++)
2479 properties[i] = properties[i].BindTypeParameters(this);
2484 public override __MethodImplMap __GetMethodImplMap()
2486 __MethodImplMap map = type.__GetMethodImplMap();
2487 map.TargetType = this;
2488 for (int i = 0; i < map.MethodBodies.Length; i++)
2490 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2491 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2493 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2494 if (interfaceType.IsGenericType)
2496 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2503 public override string Namespace
2505 get { return type.Namespace; }
2508 public override string Name
2510 get { return type.Name; }
2513 public override string FullName
2517 if (!this.__ContainsMissingType && this.ContainsGenericParameters)
2521 StringBuilder sb = new StringBuilder(this.type.FullName);
2524 foreach (Type type in args)
2526 sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
2530 return sb.ToString();
2534 public override string ToString()
2536 StringBuilder sb = new StringBuilder(type.FullName);
2539 foreach (Type arg in args)
2546 return sb.ToString();
2549 public override Module Module
2551 get { return type.Module; }
2554 public override bool IsGenericType
2556 get { return true; }
2559 internal override bool IsGenericTypeInstance
2561 get { return true; }
2564 public override Type GetGenericTypeDefinition()
2569 public override Type[] GetGenericArguments()
2571 return Util.Copy(args);
2574 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
2576 return Util.Copy(requiredCustomModifiers ?? new Type[args.Length][]);
2579 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
2581 return Util.Copy(optionalCustomModifiers ?? new Type[args.Length][]);
2584 internal override Type GetGenericTypeArgument(int index)
2589 public override bool ContainsGenericParameters
2593 foreach (Type type in args)
2595 if (type.ContainsGenericParameters)
2604 public override bool __ContainsMissingType
2608 foreach (Type type in args)
2610 if (type.__ContainsMissingType)
2615 return this.type.__IsMissing;
2619 public override StructLayoutAttribute StructLayoutAttribute
2621 get { return type.StructLayoutAttribute; }
2624 internal override int GetModuleBuilderToken()
2628 token = ((ModuleBuilder)type.Module).ImportType(this);
2633 internal override Type BindTypeParameters(IGenericBinder binder)
2635 for (int i = 0; i < args.Length; i++)
2637 Type xarg = args[i].BindTypeParameters(binder);
2638 if (!ReferenceEquals(xarg, args[i]))
2640 Type[] xargs = new Type[args.Length];
2641 Array.Copy(args, xargs, i);
2643 for (; i < args.Length; i++)
2645 xargs[i] = args[i].BindTypeParameters(binder);
2647 return Make(type, xargs, null, null);
2653 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2655 return type.GetCustomAttributesData(attributeType);