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;
48 // prevent subclassing by outsiders
53 public static Binder DefaultBinder
58 public sealed override MemberTypes MemberType
60 get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
63 public virtual string AssemblyQualifiedName
65 // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
66 get { return this.FullName + ", " + this.Assembly.FullName; }
69 public abstract Type BaseType
74 public abstract TypeAttributes Attributes
79 public virtual Type GetElementType()
84 internal virtual void CheckBaked()
88 public virtual Type[] __GetDeclaredTypes()
90 return Type.EmptyTypes;
93 public virtual Type[] __GetDeclaredInterfaces()
95 return Type.EmptyTypes;
98 public virtual MethodBase[] __GetDeclaredMethods()
100 return Empty<MethodBase>.Array;
103 public virtual __MethodImplMap __GetMethodImplMap()
105 throw new NotSupportedException();
108 public virtual FieldInfo[] __GetDeclaredFields()
110 return Empty<FieldInfo>.Array;
113 public virtual EventInfo[] __GetDeclaredEvents()
115 return Empty<EventInfo>.Array;
118 public virtual PropertyInfo[] __GetDeclaredProperties()
120 return Empty<PropertyInfo>.Array;
123 public virtual Type[] __GetRequiredCustomModifiers()
125 return Type.EmptyTypes;
128 public virtual Type[] __GetOptionalCustomModifiers()
130 return Type.EmptyTypes;
133 public virtual bool HasElementType
135 get { return false; }
138 public virtual bool IsArray
140 get { return false; }
143 public virtual bool __IsVector
145 get { return false; }
148 public virtual bool IsByRef
150 get { return false; }
153 public virtual bool IsPointer
155 get { return false; }
158 public virtual bool IsValueType
162 Type baseType = this.BaseType;
163 return baseType == this.Module.universe.System_Enum
164 || (baseType == this.Module.universe.System_ValueType && this != this.Module.universe.System_Enum);
168 public virtual bool IsGenericParameter
170 get { return false; }
173 public virtual int GenericParameterPosition
175 get { throw new NotSupportedException(); }
178 public virtual MethodBase DeclaringMethod
183 public virtual Type UnderlyingSystemType
188 public override Type DeclaringType
193 public virtual string __Name
195 get { throw new InvalidOperationException(); }
198 public virtual string __Namespace
200 get { throw new InvalidOperationException(); }
203 public abstract override string Name
208 public virtual string Namespace
214 return DeclaringType.Namespace;
220 internal virtual int GetModuleBuilderToken()
222 throw new InvalidOperationException();
225 public bool Equals(Type type)
227 return !ReferenceEquals(type, null) && ReferenceEquals(type.UnderlyingSystemType, this.UnderlyingSystemType);
230 public override bool Equals(object obj)
232 return Equals(obj as Type);
235 public override int GetHashCode()
237 Type type = this.UnderlyingSystemType;
238 return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
241 public virtual Type[] GetGenericArguments()
243 return Type.EmptyTypes;
246 public virtual Type[][] __GetGenericArgumentsRequiredCustomModifiers()
248 return Empty<Type[]>.Array;
251 public virtual Type[][] __GetGenericArgumentsOptionalCustomModifiers()
253 return Empty<Type[]>.Array;
256 public virtual Type GetGenericTypeDefinition()
258 throw new InvalidOperationException();
261 public virtual StructLayoutAttribute StructLayoutAttribute
266 public virtual bool IsGenericType
268 get { return false; }
271 public virtual bool IsGenericTypeDefinition
273 get { return false; }
276 public virtual bool ContainsGenericParameters
280 if (this.IsGenericParameter)
284 foreach (Type arg in this.GetGenericArguments())
286 if (arg.ContainsGenericParameters)
295 public virtual Type[] GetGenericParameterConstraints()
297 throw new InvalidOperationException();
300 public virtual GenericParameterAttributes GenericParameterAttributes
302 get { throw new InvalidOperationException(); }
305 public virtual int GetArrayRank()
307 throw new NotSupportedException();
311 public virtual Type GetEnumUnderlyingType()
315 throw new ArgumentException();
318 return GetEnumUnderlyingTypeImpl();
321 internal Type GetEnumUnderlyingTypeImpl()
323 foreach (FieldInfo field in __GetDeclaredFields())
327 // the CLR assumes that an enum has only one instance field, so we can do the same
328 return field.FieldType;
331 throw new InvalidOperationException();
334 public override string ToString()
339 public abstract string FullName
344 protected string GetFullName()
346 string ns = TypeNameParser.Escape(this.__Namespace);
347 Type decl = this.DeclaringType;
356 return ns + "." + this.Name;
363 return decl.FullName + "+" + this.Name;
367 return decl.FullName + "+" + ns + "." + this.Name;
372 internal virtual bool IsModulePseudoType
374 get { return false; }
377 internal virtual Type GetGenericTypeArgument(int index)
379 throw new InvalidOperationException();
382 public MemberInfo[] GetDefaultMembers()
384 Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
385 foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
387 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
389 return GetMember((string)cad.ConstructorArguments[0].Value);
392 return Empty<MemberInfo>.Array;
395 public MemberInfo[] GetMember(string name)
397 return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
400 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
402 return GetMember(name, MemberTypes.All, bindingAttr);
405 public MemberInfo[] GetMembers()
407 return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
410 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
412 List<MemberInfo> members = new List<MemberInfo>();
413 members.AddRange(GetConstructors(bindingAttr));
414 members.AddRange(GetMethods(bindingAttr));
415 members.AddRange(GetFields(bindingAttr));
416 members.AddRange(GetProperties(bindingAttr));
417 members.AddRange(GetEvents(bindingAttr));
418 members.AddRange(GetNestedTypes(bindingAttr));
419 return members.ToArray();
422 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
424 MemberFilter filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
425 return FindMembers(type, bindingAttr, filter, name);
428 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
430 foreach (MemberInfo member in members)
432 if (filter == null || filter(member, filterCriteria))
439 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
441 List<MemberInfo> members = new List<MemberInfo>();
442 if ((memberType & MemberTypes.Constructor) != 0)
444 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
446 if ((memberType & MemberTypes.Method) != 0)
448 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
450 if ((memberType & MemberTypes.Field) != 0)
452 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
454 if ((memberType & MemberTypes.Property) != 0)
456 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
458 if ((memberType & MemberTypes.Event) != 0)
460 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
462 if ((memberType & MemberTypes.NestedType) != 0)
464 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
466 return members.ToArray();
469 public EventInfo GetEvent(string name)
471 return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
474 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
476 foreach (EventInfo evt in GetEvents(bindingAttr))
478 if (evt.Name == name)
486 public EventInfo[] GetEvents()
488 return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
491 public EventInfo[] GetEvents(BindingFlags bindingAttr)
493 List<EventInfo> list = new List<EventInfo>();
498 foreach (EventInfo evt in type.__GetDeclaredEvents())
500 if (BindingFlagsMatch(evt.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
501 && BindingFlagsMatch(evt.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
506 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
508 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
510 bindingAttr &= ~BindingFlags.Static;
512 type = type.BaseType;
519 return list.ToArray();
522 public FieldInfo GetField(string name)
524 return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
527 public FieldInfo GetField(string name, BindingFlags bindingAttr)
529 foreach (FieldInfo field in GetFields(bindingAttr))
531 if (field.Name == name)
539 public FieldInfo[] GetFields()
541 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
544 public FieldInfo[] GetFields(BindingFlags bindingAttr)
546 List<FieldInfo> list = new List<FieldInfo>();
548 foreach (FieldInfo field in __GetDeclaredFields())
550 if (BindingFlagsMatch(field.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
551 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
556 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
558 for (Type type = this.BaseType; type != null; type = type.BaseType)
561 foreach (FieldInfo field in type.__GetDeclaredFields())
563 if ((field.Attributes & FieldAttributes.FieldAccessMask) > FieldAttributes.Private
564 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance))
571 return list.ToArray();
574 public Type[] GetInterfaces()
576 List<Type> list = new List<Type>();
577 for (Type type = this; type != null; type = type.BaseType)
579 AddInterfaces(list, type);
581 return list.ToArray();
584 private static void AddInterfaces(List<Type> list, Type type)
587 foreach (Type iface in type.__GetDeclaredInterfaces())
589 if (!list.Contains(iface))
592 AddInterfaces(list, iface);
597 public MethodInfo[] GetMethods(BindingFlags bindingAttr)
600 List<MethodInfo> list = new List<MethodInfo>();
601 foreach (MethodBase mb in __GetDeclaredMethods())
603 MethodInfo mi = mb as MethodInfo;
605 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
606 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
611 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
613 for (Type type = this.BaseType; type != null; type = type.BaseType)
616 foreach (MethodBase mb in type.__GetDeclaredMethods())
618 MethodInfo mi = mb as MethodInfo;
620 && (mi.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private
621 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
622 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance)
623 && !FindMethod(list, mi))
630 return list.ToArray();
633 private static bool FindMethod(List<MethodInfo> methods, MethodInfo method)
635 foreach (MethodInfo m in methods)
637 if (m.Name == method.Name && m.MethodSignature.Equals(method.MethodSignature))
645 public MethodInfo[] GetMethods()
647 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
650 public MethodInfo GetMethod(string name)
652 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
655 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
657 MethodInfo found = null;
658 foreach (MethodInfo method in GetMethods(bindingAttr))
660 if (method.Name == name)
664 throw new AmbiguousMatchException();
672 public MethodInfo GetMethod(string name, Type[] types)
674 return GetMethod(name, types, null);
677 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
679 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
682 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
684 MethodInfo found = null;
685 foreach (MethodInfo method in GetMethods(bindingAttr))
687 if (method.Name == name && method.MethodSignature.MatchParameterTypes(types))
691 throw new AmbiguousMatchException();
699 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
701 // FXBUG callConvention seems to be ignored
702 return GetMethod(name, bindingAttr, binder, types, modifiers);
705 public ConstructorInfo[] GetConstructors()
707 return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
710 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
713 List<ConstructorInfo> list = new List<ConstructorInfo>();
714 foreach (MethodBase mb in __GetDeclaredMethods())
716 ConstructorInfo constructor = mb as ConstructorInfo;
717 if (constructor != null
718 && BindingFlagsMatch(constructor.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
719 && BindingFlagsMatch(constructor.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
721 list.Add(constructor);
724 return list.ToArray();
727 public ConstructorInfo GetConstructor(Type[] types)
729 return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
732 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
734 foreach (ConstructorInfo constructor in GetConstructors(bindingAttr))
736 if (constructor.MethodSignature.MatchParameterTypes(types))
744 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
746 // FXBUG callConvention seems to be ignored
747 return GetConstructor(bindingAttr, binder, types, modifiers);
750 internal Type ResolveNestedType(TypeName typeName)
752 return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
755 // unlike the public API, this takes the namespace and name into account
756 internal virtual Type FindNestedType(TypeName name)
758 foreach (Type type in __GetDeclaredTypes())
760 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
768 public Type GetNestedType(string name)
770 return GetNestedType(name, BindingFlags.Public);
773 public Type GetNestedType(string name, BindingFlags bindingAttr)
775 foreach (Type type in GetNestedTypes(bindingAttr))
777 // FXBUG the namespace is ignored
778 if (type.__Name == name)
786 public Type[] GetNestedTypes()
788 return GetNestedTypes(BindingFlags.Public);
791 public Type[] GetNestedTypes(BindingFlags bindingAttr)
794 List<Type> list = new List<Type>();
795 foreach (Type type in __GetDeclaredTypes())
797 if (BindingFlagsMatch(type.IsNestedPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic))
802 return list.ToArray();
805 public PropertyInfo[] GetProperties()
807 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
810 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
812 List<PropertyInfo> list = new List<PropertyInfo>();
817 foreach (PropertyInfo property in type.__GetDeclaredProperties())
819 if (BindingFlagsMatch(property.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
820 && BindingFlagsMatch(property.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
825 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
827 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
829 bindingAttr &= ~BindingFlags.Static;
831 type = type.BaseType;
838 return list.ToArray();
841 public PropertyInfo GetProperty(string name)
843 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
846 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
848 foreach (PropertyInfo prop in GetProperties(bindingAttr))
850 if (prop.Name == name)
858 public PropertyInfo GetProperty(string name, Type returnType)
860 PropertyInfo found = null;
861 foreach (PropertyInfo prop in GetProperties())
863 if (prop.Name == name && prop.PropertyType.Equals(returnType))
867 throw new AmbiguousMatchException();
875 public PropertyInfo GetProperty(string name, Type[] types)
877 PropertyInfo found = null;
878 foreach (PropertyInfo prop in GetProperties())
880 if (prop.Name == name && MatchParameterTypes(prop.GetIndexParameters(), types))
884 throw new AmbiguousMatchException();
892 private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
894 if (parameters.Length == types.Length)
896 for (int i = 0; i < parameters.Length; i++)
898 if (!parameters[i].ParameterType.Equals(types[i]))
908 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
910 return GetProperty(name, returnType, types, null);
913 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
915 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
918 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
920 PropertyInfo found = null;
921 foreach (PropertyInfo prop in GetProperties(bindingAttr))
923 if (prop.Name == name && prop.PropertyType.Equals(returnType) && MatchParameterTypes(prop.GetIndexParameters(), types))
927 throw new AmbiguousMatchException();
935 public Type GetInterface(string name)
937 return GetInterface(name, false);
940 public Type GetInterface(string name, bool ignoreCase)
944 throw new NotImplementedException();
946 foreach (Type type in GetInterfaces())
948 if (type.FullName == name)
956 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
958 List<Type> list = new List<Type>();
959 foreach (Type type in GetInterfaces())
961 if (filter(type, filterCriteria))
966 return list.ToArray();
969 public ConstructorInfo TypeInitializer
971 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
974 public bool IsPrimitive
978 Universe u = this.Module.universe;
979 return this == u.System_Boolean
980 || this == u.System_Byte
981 || this == u.System_SByte
982 || this == u.System_Int16
983 || this == u.System_UInt16
984 || this == u.System_Int32
985 || this == u.System_UInt32
986 || this == u.System_Int64
987 || this == u.System_UInt64
988 || this == u.System_IntPtr
989 || this == u.System_UIntPtr
990 || this == u.System_Char
991 || this == u.System_Double
992 || this == u.System_Single
999 get { return this.BaseType == this.Module.universe.System_Enum; }
1002 public bool IsSealed
1004 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1007 public bool IsAbstract
1009 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1012 private bool CheckVisibility(TypeAttributes access)
1014 return (Attributes & TypeAttributes.VisibilityMask) == access;
1017 public bool IsPublic
1019 get { return CheckVisibility(TypeAttributes.Public); }
1022 public bool IsNestedPublic
1024 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1027 public bool IsNestedPrivate
1029 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1032 public bool IsNestedFamily
1034 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1037 public bool IsNestedAssembly
1039 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1042 public bool IsNestedFamANDAssem
1044 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1047 public bool IsNestedFamORAssem
1049 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1052 public bool IsNotPublic
1054 get { return CheckVisibility(TypeAttributes.NotPublic); }
1057 public bool IsImport
1059 get { return (Attributes & TypeAttributes.Import) != 0; }
1062 public bool IsCOMObject
1064 get { return IsClass && IsImport; }
1067 public bool IsContextful
1069 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1072 public bool IsMarshalByRef
1074 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1077 public virtual bool IsVisible
1079 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1082 public bool IsAnsiClass
1084 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1087 public bool IsUnicodeClass
1089 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1092 public bool IsAutoClass
1094 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1097 public bool IsAutoLayout
1099 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1102 public bool IsLayoutSequential
1104 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1107 public bool IsExplicitLayout
1109 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1112 public bool IsSpecialName
1114 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1117 public bool IsSerializable
1119 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1124 get { return !IsInterface && !IsValueType; }
1127 public bool IsInterface
1129 get { return (Attributes & TypeAttributes.Interface) != 0; }
1132 public bool IsNested
1134 // FXBUG we check the declaring type (like .NET) and this results
1135 // in IsNested returning true for a generic type parameter
1136 get { return this.DeclaringType != null; }
1139 public virtual bool __ContainsMissingType
1143 if (this.__IsMissing)
1147 foreach (Type arg in this.GetGenericArguments())
1149 if (arg.__ContainsMissingType)
1158 public Type MakeArrayType()
1160 return ArrayType.Make(this, null, null);
1163 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1165 return ArrayType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1168 public Type MakeArrayType(int rank)
1170 return MultiArrayType.Make(this, rank, null, null);
1173 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1175 return MultiArrayType.Make(this, rank, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1178 public Type MakeByRefType()
1180 return ByRefType.Make(this, null, null);
1183 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1185 return ByRefType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1188 public Type MakePointerType()
1190 return PointerType.Make(this, null, null);
1193 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1195 return PointerType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1198 public Type MakeGenericType(params Type[] typeArguments)
1200 return __MakeGenericType(typeArguments, null, null);
1203 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1205 if (!this.IsGenericTypeDefinition)
1207 throw new InvalidOperationException();
1209 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1212 public static System.Type __GetSystemType(TypeCode typeCode)
1216 case TypeCode.Boolean:
1217 return typeof(System.Boolean);
1219 return typeof(System.Byte);
1221 return typeof(System.Char);
1222 case TypeCode.DBNull:
1223 return typeof(System.DBNull);
1224 case TypeCode.DateTime:
1225 return typeof(System.DateTime);
1226 case TypeCode.Decimal:
1227 return typeof(System.Decimal);
1228 case TypeCode.Double:
1229 return typeof(System.Double);
1230 case TypeCode.Empty:
1232 case TypeCode.Int16:
1233 return typeof(System.Int16);
1234 case TypeCode.Int32:
1235 return typeof(System.Int32);
1236 case TypeCode.Int64:
1237 return typeof(System.Int64);
1238 case TypeCode.Object:
1239 return typeof(System.Object);
1240 case TypeCode.SByte:
1241 return typeof(System.SByte);
1242 case TypeCode.Single:
1243 return typeof(System.Single);
1244 case TypeCode.String:
1245 return typeof(System.String);
1246 case TypeCode.UInt16:
1247 return typeof(System.UInt16);
1248 case TypeCode.UInt32:
1249 return typeof(System.UInt32);
1250 case TypeCode.UInt64:
1251 return typeof(System.UInt64);
1253 throw new ArgumentOutOfRangeException();
1257 public static TypeCode GetTypeCode(Type type)
1261 return TypeCode.Empty;
1265 type = type.GetEnumUnderlyingType();
1267 Universe u = type.Module.universe;
1268 if (type == u.System_Boolean)
1270 return TypeCode.Boolean;
1272 else if (type == u.System_Char)
1274 return TypeCode.Char;
1276 else if (type == u.System_SByte)
1278 return TypeCode.SByte;
1280 else if (type == u.System_Byte)
1282 return TypeCode.Byte;
1284 else if (type == u.System_Int16)
1286 return TypeCode.Int16;
1288 else if (type == u.System_UInt16)
1290 return TypeCode.UInt16;
1292 else if (type == u.System_Int32)
1294 return TypeCode.Int32;
1296 else if (type == u.System_UInt32)
1298 return TypeCode.UInt32;
1300 else if (type == u.System_Int64)
1302 return TypeCode.Int64;
1304 else if (type == u.System_UInt64)
1306 return TypeCode.UInt64;
1308 else if (type == u.System_Single)
1310 return TypeCode.Single;
1312 else if (type == u.System_Double)
1314 return TypeCode.Double;
1316 else if (type == u.System_DateTime)
1318 return TypeCode.DateTime;
1320 else if (type == u.System_DBNull)
1322 return TypeCode.DBNull;
1324 else if (type == u.System_Decimal)
1326 return TypeCode.Decimal;
1328 else if (type == u.System_String)
1330 return TypeCode.String;
1334 return TypeCode.Object;
1338 public Assembly Assembly
1340 get { return Module.Assembly; }
1343 // note that interface/delegate co- and contravariance is not considered
1344 public bool IsAssignableFrom(Type type)
1346 if (this.Equals(type))
1350 else if (type == null)
1354 else if (this.IsArray && type.IsArray)
1356 if (this.GetArrayRank() != type.GetArrayRank())
1360 else if (this.__IsVector && !type.__IsVector)
1364 Type e1 = this.GetElementType();
1365 Type e2 = type.GetElementType();
1366 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1368 else if (this.IsSealed)
1372 else if (this.IsInterface)
1374 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1376 else if (type.IsInterface)
1378 return this == this.Module.universe.System_Object;
1380 else if (type.IsPointer)
1382 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1386 return type.IsSubclassOf(this);
1390 public bool IsSubclassOf(Type type)
1392 Type thisType = this.BaseType;
1393 while (thisType != null)
1395 if (thisType.Equals(type))
1399 thisType = thisType.BaseType;
1404 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1405 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1406 private bool IsDirectlyImplementedInterface(Type interfaceType)
1408 foreach (Type iface in __GetDeclaredInterfaces())
1410 if (interfaceType.IsAssignableFrom(iface))
1418 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1421 InterfaceMapping map = new InterfaceMapping();
1422 if (!IsDirectlyImplementedInterface(interfaceType))
1424 Type baseType = this.BaseType;
1425 if (baseType == null)
1427 throw new ArgumentException();
1431 map = baseType.GetInterfaceMap(interfaceType);
1436 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1437 map.InterfaceType = interfaceType;
1438 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1439 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1440 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1441 for (int i = 0; i < map.TargetMethods.Length; i++)
1443 if (map.TargetMethods[i] == null)
1445 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1446 for (int j = 0; j < methods.Length; j++)
1448 if (methods[j].Name == map.InterfaceMethods[i].Name
1449 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1451 map.TargetMethods[i] = methods[j];
1456 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1458 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1461 map.TargetType = this;
1465 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1467 __MethodImplMap impl = __GetMethodImplMap();
1468 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1470 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1472 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1473 if (index != -1 && targetMethods[index] == null)
1475 targetMethods[index] = impl.MethodBodies[i];
1481 Type IGenericContext.GetGenericTypeArgument(int index)
1483 return GetGenericTypeArgument(index);
1486 Type IGenericContext.GetGenericMethodArgument(int index)
1488 throw new BadImageFormatException();
1491 Type IGenericBinder.BindTypeParameter(Type type)
1493 return GetGenericTypeArgument(type.GenericParameterPosition);
1496 Type IGenericBinder.BindMethodParameter(Type type)
1498 throw new BadImageFormatException();
1501 internal virtual Type BindTypeParameters(IGenericBinder binder)
1503 if (IsGenericTypeDefinition)
1505 Type[] args = GetGenericArguments();
1506 Type.InplaceBindTypeParameters(binder, args);
1507 return GenericTypeInstance.Make(this, args, null, null);
1515 internal static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1517 for (int i = 0; i < types.Length; i++)
1519 types[i] = types[i].BindTypeParameters(binder);
1523 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1525 foreach (MethodBase method in __GetDeclaredMethods())
1527 if (method.Name == name && method.MethodSignature.Equals(signature))
1535 internal FieldInfo FindField(string name, FieldSignature signature)
1537 foreach (FieldInfo field in __GetDeclaredFields())
1539 if (field.Name == name && field.FieldSignature.Equals(signature))
1547 internal bool IsAllowMultipleCustomAttribute
1551 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1554 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1556 if (arg.MemberInfo.Name == "AllowMultiple")
1558 return (bool)arg.TypedValue.Value;
1566 internal bool IsPseudoCustomAttribute
1570 Universe u = this.Module.universe;
1571 return this == u.System_NonSerializedAttribute
1572 || this == u.System_SerializableAttribute
1573 || this == u.System_Runtime_InteropServices_DllImportAttribute
1574 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1575 || this == u.System_Runtime_InteropServices_InAttribute
1576 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1577 || this == u.System_Runtime_InteropServices_OutAttribute
1578 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1579 || this == u.System_Runtime_InteropServices_OptionalAttribute
1580 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1581 || this == u.System_Runtime_InteropServices_ComImportAttribute
1582 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1583 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1589 abstract class ElementHolderType : Type
1591 protected readonly Type elementType;
1593 private readonly Type[] requiredCustomModifiers;
1594 private readonly Type[] optionalCustomModifiers;
1596 protected ElementHolderType(Type elementType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1598 this.elementType = elementType;
1599 this.requiredCustomModifiers = requiredCustomModifiers;
1600 this.optionalCustomModifiers = optionalCustomModifiers;
1603 protected bool EqualsHelper(ElementHolderType other)
1605 return other != null
1606 && other.elementType.Equals(elementType)
1607 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers)
1608 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers);
1611 public override Type[] __GetRequiredCustomModifiers()
1613 return Util.Copy(requiredCustomModifiers);
1616 public override Type[] __GetOptionalCustomModifiers()
1618 return Util.Copy(optionalCustomModifiers);
1621 public sealed override string Name
1623 get { return elementType.Name + GetSuffix(); }
1626 public sealed override string Namespace
1628 get { return elementType.Namespace; }
1631 public sealed override string FullName
1633 get { return elementType.FullName + GetSuffix(); }
1636 public sealed override string ToString()
1638 return elementType.ToString() + GetSuffix();
1641 public sealed override Type GetElementType()
1646 public sealed override bool HasElementType
1648 get { return true; }
1651 public sealed override Module Module
1653 get { return elementType.Module; }
1656 internal sealed override int GetModuleBuilderToken()
1660 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1665 public sealed override bool ContainsGenericParameters
1669 Type type = elementType;
1670 while (type.HasElementType)
1672 type = type.GetElementType();
1674 return type.ContainsGenericParameters;
1678 public sealed override bool __ContainsMissingType
1682 Type type = elementType;
1683 while (type.HasElementType)
1685 type = type.GetElementType();
1687 return type.__ContainsMissingType;
1691 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1693 Type type = elementType.BindTypeParameters(binder);
1694 Type[] req = BindArray(requiredCustomModifiers, binder);
1695 Type[] opt = BindArray(optionalCustomModifiers, binder);
1696 if (ReferenceEquals(type, elementType)
1697 && ReferenceEquals(req, requiredCustomModifiers)
1698 && ReferenceEquals(opt, optionalCustomModifiers))
1702 return Wrap(type, req, opt);
1705 internal override void CheckBaked()
1707 elementType.CheckBaked();
1710 private static Type[] BindArray(Type[] array, IGenericBinder binder)
1712 if (array ==null || array.Length == 0)
1716 Type[] result = array;
1717 for (int i = 0; i < array.Length; i++)
1719 Type type = array[i].BindTypeParameters(binder);
1720 if (!ReferenceEquals(type, array[i]))
1722 if (result == array)
1724 result = (Type[])array.Clone();
1732 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1734 return CustomAttributeData.EmptyList;
1737 protected abstract string GetSuffix();
1739 protected abstract Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers);
1742 sealed class ArrayType : ElementHolderType
1744 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1746 return type.Module.CanonicalizeType(new ArrayType(type, requiredCustomModifiers, optionalCustomModifiers));
1749 private ArrayType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1750 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1754 public override Type BaseType
1756 get { return elementType.Module.universe.System_Array; }
1759 public override Type[] __GetDeclaredInterfaces()
1762 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
1763 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
1764 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
1768 public override MethodBase[] __GetDeclaredMethods()
1770 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
1771 List<MethodBase> list = new List<MethodBase>();
1772 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 }));
1773 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
1774 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
1775 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1776 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
1778 Array.Resize(ref int32, int32.Length + 1);
1779 int32[int32.Length - 1] = int32[0];
1780 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1782 return list.ToArray();
1785 public override TypeAttributes Attributes
1787 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1790 public override bool IsArray
1792 get { return true; }
1795 public override bool __IsVector
1797 get { return true; }
1800 public override int GetArrayRank()
1805 public override bool Equals(object o)
1807 return EqualsHelper(o as ArrayType);
1810 public override int GetHashCode()
1812 return elementType.GetHashCode() * 5;
1815 protected override string GetSuffix()
1820 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1822 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
1826 sealed class MultiArrayType : ElementHolderType
1828 private readonly int rank;
1830 internal static Type Make(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1832 return type.Module.CanonicalizeType(new MultiArrayType(type, rank, requiredCustomModifiers, optionalCustomModifiers));
1835 private MultiArrayType(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1836 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1841 public override Type BaseType
1843 get { return elementType.Module.universe.System_Array; }
1846 public override MethodBase[] __GetDeclaredMethods()
1848 Type int32 = this.Module.universe.System_Int32;
1849 Type[] setArgs = new Type[rank + 1];
1850 Type[] getArgs = new Type[rank];
1851 Type[] ctorArgs = new Type[rank * 2];
1852 for (int i = 0; i < rank; i++)
1856 ctorArgs[i * 2 + 0] = int32;
1857 ctorArgs[i * 2 + 1] = int32;
1859 setArgs[rank] = elementType;
1860 return new MethodBase[] {
1861 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
1862 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
1863 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
1864 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
1865 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
1869 public override TypeAttributes Attributes
1871 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1874 public override bool IsArray
1876 get { return true; }
1879 public override int GetArrayRank()
1884 public override bool Equals(object o)
1886 MultiArrayType at = o as MultiArrayType;
1887 return EqualsHelper(at) && at.rank == rank;
1890 public override int GetHashCode()
1892 return elementType.GetHashCode() * 9 + rank;
1895 protected override string GetSuffix()
1903 return "[" + new String(',', rank - 1) + "]";
1907 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1909 return Make(type, rank, requiredCustomModifiers, optionalCustomModifiers);
1913 sealed class BuiltinArrayMethod : ArrayMethod
1915 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
1916 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
1920 public override MethodAttributes Attributes
1922 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
1925 public override MethodImplAttributes GetMethodImplementationFlags()
1927 return MethodImplAttributes.IL;
1930 public override int MetadataToken
1932 get { return 0x06000000; }
1935 public override MethodBody GetMethodBody()
1940 public override ParameterInfo[] GetParameters()
1942 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
1943 for (int i = 0; i < parameterInfos.Length; i++)
1945 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
1947 return parameterInfos;
1950 public override ParameterInfo ReturnParameter
1952 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
1955 private sealed class ParameterInfoImpl : ParameterInfo
1957 private readonly MethodInfo method;
1958 private readonly Type type;
1959 private readonly int pos;
1961 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
1963 this.method = method;
1968 public override Type ParameterType
1970 get { return type; }
1973 public override string Name
1975 get { return null; }
1978 public override ParameterAttributes Attributes
1980 get { return ParameterAttributes.None; }
1983 public override int Position
1988 public override object RawDefaultValue
1990 get { return null; }
1993 public override Type[] GetOptionalCustomModifiers()
1995 return Empty<Type>.Array;
1998 public override Type[] GetRequiredCustomModifiers()
2000 return Empty<Type>.Array;
2003 public override MemberInfo Member
2005 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2008 public override int MetadataToken
2010 get { return 0x8000000; }
2013 internal override Module Module
2015 get { return method.Module; }
2020 sealed class ByRefType : ElementHolderType
2022 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2024 return type.Module.CanonicalizeType(new ByRefType(type, requiredCustomModifiers, optionalCustomModifiers));
2027 private ByRefType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2028 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2032 public override bool Equals(object o)
2034 return EqualsHelper(o as ByRefType);
2037 public override int GetHashCode()
2039 return elementType.GetHashCode() * 3;
2042 public override Type BaseType
2044 get { return null; }
2047 public override TypeAttributes Attributes
2052 public override bool IsByRef
2054 get { return true; }
2057 protected override string GetSuffix()
2062 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2064 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2068 sealed class PointerType : ElementHolderType
2070 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2072 return type.Module.CanonicalizeType(new PointerType(type, requiredCustomModifiers, optionalCustomModifiers));
2075 private PointerType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2076 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2080 public override bool Equals(object o)
2082 return EqualsHelper(o as PointerType);
2085 public override int GetHashCode()
2087 return elementType.GetHashCode() * 7;
2090 public override Type BaseType
2092 get { return null; }
2095 public override TypeAttributes Attributes
2100 public override bool IsPointer
2102 get { return true; }
2105 protected override string GetSuffix()
2110 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2112 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2116 sealed class GenericTypeInstance : Type
2118 private readonly Type type;
2119 private readonly Type[] args;
2120 private readonly Type[][] requiredCustomModifiers;
2121 private readonly Type[][] optionalCustomModifiers;
2122 private Type baseType;
2125 internal static Type Make(Type type, Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2127 bool identity = true;
2128 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2130 // a TypeBuiler identity must be instantiated
2135 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2136 for (int i = 0; i < typeArguments.Length; i++)
2138 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2139 || !IsEmpty(requiredCustomModifiers, i)
2140 || !IsEmpty(optionalCustomModifiers, i))
2153 return type.Module.CanonicalizeType(new GenericTypeInstance(type, typeArguments, requiredCustomModifiers, optionalCustomModifiers));
2157 private static bool IsEmpty(Type[][] mods, int i)
2159 // we need to be extra careful, because mods doesn't not need to be in canonical format
2160 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2161 return mods == null || mods[i] == null || mods[i].Length == 0;
2164 private GenericTypeInstance(Type type, Type[] args, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2168 this.requiredCustomModifiers = requiredCustomModifiers;
2169 this.optionalCustomModifiers = optionalCustomModifiers;
2172 public override bool Equals(object o)
2174 GenericTypeInstance gt = o as GenericTypeInstance;
2175 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2176 && Util.ArrayEquals(gt.requiredCustomModifiers, requiredCustomModifiers)
2177 && Util.ArrayEquals(gt.optionalCustomModifiers, optionalCustomModifiers);
2180 public override int GetHashCode()
2182 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2185 public override string AssemblyQualifiedName
2189 string fn = FullName;
2190 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2194 public override Type BaseType
2198 if (baseType == null)
2200 Type rawBaseType = type.BaseType;
2201 if (rawBaseType == null)
2203 baseType = rawBaseType;
2207 baseType = rawBaseType.BindTypeParameters(this);
2214 public override bool IsValueType
2216 get { return type.IsValueType; }
2219 public override bool IsVisible
2225 foreach (Type arg in args)
2238 public override Type DeclaringType
2240 get { return type.DeclaringType; }
2243 public override TypeAttributes Attributes
2245 get { return type.Attributes; }
2248 internal override void CheckBaked()
2253 public override FieldInfo[] __GetDeclaredFields()
2255 FieldInfo[] fields = type.__GetDeclaredFields();
2256 for (int i = 0; i < fields.Length; i++)
2258 fields[i] = fields[i].BindTypeParameters(this);
2263 public override Type[] __GetDeclaredInterfaces()
2265 Type[] interfaces = type.__GetDeclaredInterfaces();
2266 for (int i = 0; i < interfaces.Length; i++)
2268 interfaces[i] = interfaces[i].BindTypeParameters(this);
2273 public override MethodBase[] __GetDeclaredMethods()
2275 MethodBase[] methods = type.__GetDeclaredMethods();
2276 for (int i = 0; i < methods.Length; i++)
2278 methods[i] = methods[i].BindTypeParameters(this);
2283 public override Type[] __GetDeclaredTypes()
2285 return type.__GetDeclaredTypes();
2288 public override EventInfo[] __GetDeclaredEvents()
2290 EventInfo[] events = type.__GetDeclaredEvents();
2291 for (int i = 0; i < events.Length; i++)
2293 events[i] = events[i].BindTypeParameters(this);
2298 public override PropertyInfo[] __GetDeclaredProperties()
2300 PropertyInfo[] properties = type.__GetDeclaredProperties();
2301 for (int i = 0; i < properties.Length; i++)
2303 properties[i] = properties[i].BindTypeParameters(this);
2308 public override __MethodImplMap __GetMethodImplMap()
2310 __MethodImplMap map = type.__GetMethodImplMap();
2311 map.TargetType = this;
2312 for (int i = 0; i < map.MethodBodies.Length; i++)
2314 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2315 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2317 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2318 if (interfaceType.IsGenericType)
2320 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2327 public override string Namespace
2329 get { return type.Namespace; }
2332 public override Type UnderlyingSystemType
2334 get { return this; }
2337 public override string Name
2339 get { return type.Name; }
2342 public override string FullName
2346 if (this.ContainsGenericParameters)
2350 StringBuilder sb = new StringBuilder(this.type.FullName);
2352 foreach (Type type in args)
2354 sb.Append('[').Append(type.AssemblyQualifiedName.Replace("]", "\\]")).Append(']');
2357 return sb.ToString();
2361 public override string ToString()
2363 StringBuilder sb = new StringBuilder(type.FullName);
2366 foreach (Type arg in args)
2373 return sb.ToString();
2376 public override Module Module
2378 get { return type.Module; }
2381 public override bool IsGenericType
2383 get { return true; }
2386 public override Type GetGenericTypeDefinition()
2391 public override Type[] GetGenericArguments()
2393 return Util.Copy(args);
2396 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
2398 return Util.Copy(requiredCustomModifiers ?? new Type[args.Length][]);
2401 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
2403 return Util.Copy(optionalCustomModifiers ?? new Type[args.Length][]);
2406 internal override Type GetGenericTypeArgument(int index)
2411 public override bool ContainsGenericParameters
2415 foreach (Type type in args)
2417 if (type.ContainsGenericParameters)
2426 public override bool __ContainsMissingType
2430 foreach (Type type in args)
2432 if (type.__ContainsMissingType)
2437 return this.type.__IsMissing;
2441 public override StructLayoutAttribute StructLayoutAttribute
2443 get { return type.StructLayoutAttribute; }
2446 internal override int GetModuleBuilderToken()
2450 token = ((ModuleBuilder)type.Module).ImportType(this);
2455 internal override Type BindTypeParameters(IGenericBinder binder)
2457 for (int i = 0; i < args.Length; i++)
2459 Type xarg = args[i].BindTypeParameters(binder);
2460 if (!ReferenceEquals(xarg, args[i]))
2462 Type[] xargs = new Type[args.Length];
2463 Array.Copy(args, xargs, i);
2465 for (; i < args.Length; i++)
2467 xargs[i] = args[i].BindTypeParameters(binder);
2469 return Make(type, xargs, null, null);
2475 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2477 return type.GetCustomAttributesData(attributeType);