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 private static bool MatchTypeNames(string ns, string name, string fullName)
754 return name == fullName;
756 else if (ns.Length + 1 + name.Length == fullName.Length)
758 return fullName[ns.Length] == '.'
759 && String.CompareOrdinal(ns, 0, fullName, 0, ns.Length) == 0
760 && String.CompareOrdinal(name, 0, fullName, ns.Length + 1, name.Length) == 0;
768 internal virtual Type ResolveNestedType(string ns, string name)
770 return GetNestedTypeCorrectly(ns == null ? name : ns + "." + name);
773 // unlike the public API, this takes the namespace and name into account
774 internal Type GetNestedTypeCorrectly(string name)
777 foreach (Type type in __GetDeclaredTypes())
779 if (MatchTypeNames(type.__Namespace, type.__Name, name))
787 public Type GetNestedType(string name)
789 return GetNestedType(name, BindingFlags.Public);
792 public Type GetNestedType(string name, BindingFlags bindingAttr)
794 foreach (Type type in GetNestedTypes(bindingAttr))
796 // FXBUG the namespace is ignored
797 if (type.__Name == name)
805 public Type[] GetNestedTypes()
807 return GetNestedTypes(BindingFlags.Public);
810 public Type[] GetNestedTypes(BindingFlags bindingAttr)
813 List<Type> list = new List<Type>();
814 foreach (Type type in __GetDeclaredTypes())
816 if (BindingFlagsMatch(type.IsNestedPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic))
821 return list.ToArray();
824 public PropertyInfo[] GetProperties()
826 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
829 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
831 List<PropertyInfo> list = new List<PropertyInfo>();
836 foreach (PropertyInfo property in type.__GetDeclaredProperties())
838 if (BindingFlagsMatch(property.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
839 && BindingFlagsMatch(property.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
844 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
846 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
848 bindingAttr &= ~BindingFlags.Static;
850 type = type.BaseType;
857 return list.ToArray();
860 public PropertyInfo GetProperty(string name)
862 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
865 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
867 foreach (PropertyInfo prop in GetProperties(bindingAttr))
869 if (prop.Name == name)
877 public PropertyInfo GetProperty(string name, Type returnType)
879 PropertyInfo found = null;
880 foreach (PropertyInfo prop in GetProperties())
882 if (prop.Name == name && prop.PropertyType.Equals(returnType))
886 throw new AmbiguousMatchException();
894 public PropertyInfo GetProperty(string name, Type[] types)
896 PropertyInfo found = null;
897 foreach (PropertyInfo prop in GetProperties())
899 if (prop.Name == name && MatchParameterTypes(prop.GetIndexParameters(), types))
903 throw new AmbiguousMatchException();
911 private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
913 if (parameters.Length == types.Length)
915 for (int i = 0; i < parameters.Length; i++)
917 if (!parameters[i].ParameterType.Equals(types[i]))
927 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
929 return GetProperty(name, returnType, types, null);
932 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
934 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
937 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
939 PropertyInfo found = null;
940 foreach (PropertyInfo prop in GetProperties(bindingAttr))
942 if (prop.Name == name && prop.PropertyType.Equals(returnType) && MatchParameterTypes(prop.GetIndexParameters(), types))
946 throw new AmbiguousMatchException();
954 public Type GetInterface(string name)
956 return GetInterface(name, false);
959 public Type GetInterface(string name, bool ignoreCase)
963 throw new NotImplementedException();
965 foreach (Type type in GetInterfaces())
967 if (type.FullName == name)
975 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
977 List<Type> list = new List<Type>();
978 foreach (Type type in GetInterfaces())
980 if (filter(type, filterCriteria))
985 return list.ToArray();
988 public ConstructorInfo TypeInitializer
990 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
993 public bool IsPrimitive
997 Universe u = this.Module.universe;
998 return this == u.System_Boolean
999 || this == u.System_Byte
1000 || this == u.System_SByte
1001 || this == u.System_Int16
1002 || this == u.System_UInt16
1003 || this == u.System_Int32
1004 || this == u.System_UInt32
1005 || this == u.System_Int64
1006 || this == u.System_UInt64
1007 || this == u.System_IntPtr
1008 || this == u.System_UIntPtr
1009 || this == u.System_Char
1010 || this == u.System_Double
1011 || this == u.System_Single
1018 get { return this.BaseType == this.Module.universe.System_Enum; }
1021 public bool IsSealed
1023 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1026 public bool IsAbstract
1028 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1031 private bool CheckVisibility(TypeAttributes access)
1033 return (Attributes & TypeAttributes.VisibilityMask) == access;
1036 public bool IsPublic
1038 get { return CheckVisibility(TypeAttributes.Public); }
1041 public bool IsNestedPublic
1043 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1046 public bool IsNestedPrivate
1048 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1051 public bool IsNestedFamily
1053 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1056 public bool IsNestedAssembly
1058 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1061 public bool IsNestedFamANDAssem
1063 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1066 public bool IsNestedFamORAssem
1068 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1071 public bool IsNotPublic
1073 get { return CheckVisibility(TypeAttributes.NotPublic); }
1076 public bool IsImport
1078 get { return (Attributes & TypeAttributes.Import) != 0; }
1081 public bool IsCOMObject
1083 get { return IsClass && IsImport; }
1086 public bool IsContextful
1088 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1091 public bool IsMarshalByRef
1093 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1096 public virtual bool IsVisible
1098 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1101 public bool IsAnsiClass
1103 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1106 public bool IsUnicodeClass
1108 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1111 public bool IsAutoClass
1113 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1116 public bool IsAutoLayout
1118 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1121 public bool IsLayoutSequential
1123 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1126 public bool IsExplicitLayout
1128 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1131 public bool IsSpecialName
1133 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1136 public bool IsSerializable
1138 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1143 get { return !IsInterface && !IsValueType; }
1146 public bool IsInterface
1148 get { return (Attributes & TypeAttributes.Interface) != 0; }
1151 public bool IsNested
1153 // FXBUG we check the declaring type (like .NET) and this results
1154 // in IsNested returning true for a generic type parameter
1155 get { return this.DeclaringType != null; }
1158 public bool __IsMissing
1160 get { return this is MissingType; }
1163 public virtual bool __ContainsMissingType
1167 if (this.__IsMissing)
1171 foreach (Type arg in this.GetGenericArguments())
1173 if (arg.__ContainsMissingType)
1182 public Type MakeArrayType()
1184 return ArrayType.Make(this, null, null);
1187 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1189 return ArrayType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1192 public Type MakeArrayType(int rank)
1194 return MultiArrayType.Make(this, rank, null, null);
1197 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1199 return MultiArrayType.Make(this, rank, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1202 public Type MakeByRefType()
1204 return ByRefType.Make(this, null, null);
1207 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1209 return ByRefType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1212 public Type MakePointerType()
1214 return PointerType.Make(this, null, null);
1217 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1219 return PointerType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1222 public Type MakeGenericType(params Type[] typeArguments)
1224 return __MakeGenericType(typeArguments, null, null);
1227 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1229 if (!this.IsGenericTypeDefinition)
1231 throw new InvalidOperationException();
1233 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1236 public static System.Type __GetSystemType(TypeCode typeCode)
1240 case TypeCode.Boolean:
1241 return typeof(System.Boolean);
1243 return typeof(System.Byte);
1245 return typeof(System.Char);
1246 case TypeCode.DBNull:
1247 return typeof(System.DBNull);
1248 case TypeCode.DateTime:
1249 return typeof(System.DateTime);
1250 case TypeCode.Decimal:
1251 return typeof(System.Decimal);
1252 case TypeCode.Double:
1253 return typeof(System.Double);
1254 case TypeCode.Empty:
1256 case TypeCode.Int16:
1257 return typeof(System.Int16);
1258 case TypeCode.Int32:
1259 return typeof(System.Int32);
1260 case TypeCode.Int64:
1261 return typeof(System.Int64);
1262 case TypeCode.Object:
1263 return typeof(System.Object);
1264 case TypeCode.SByte:
1265 return typeof(System.SByte);
1266 case TypeCode.Single:
1267 return typeof(System.Single);
1268 case TypeCode.String:
1269 return typeof(System.String);
1270 case TypeCode.UInt16:
1271 return typeof(System.UInt16);
1272 case TypeCode.UInt32:
1273 return typeof(System.UInt32);
1274 case TypeCode.UInt64:
1275 return typeof(System.UInt64);
1277 throw new ArgumentOutOfRangeException();
1281 public static TypeCode GetTypeCode(Type type)
1285 return TypeCode.Empty;
1289 type = type.GetEnumUnderlyingType();
1291 Universe u = type.Module.universe;
1292 if (type == u.System_Boolean)
1294 return TypeCode.Boolean;
1296 else if (type == u.System_Char)
1298 return TypeCode.Char;
1300 else if (type == u.System_SByte)
1302 return TypeCode.SByte;
1304 else if (type == u.System_Byte)
1306 return TypeCode.Byte;
1308 else if (type == u.System_Int16)
1310 return TypeCode.Int16;
1312 else if (type == u.System_UInt16)
1314 return TypeCode.UInt16;
1316 else if (type == u.System_Int32)
1318 return TypeCode.Int32;
1320 else if (type == u.System_UInt32)
1322 return TypeCode.UInt32;
1324 else if (type == u.System_Int64)
1326 return TypeCode.Int64;
1328 else if (type == u.System_UInt64)
1330 return TypeCode.UInt64;
1332 else if (type == u.System_Single)
1334 return TypeCode.Single;
1336 else if (type == u.System_Double)
1338 return TypeCode.Double;
1340 else if (type == u.System_DateTime)
1342 return TypeCode.DateTime;
1344 else if (type == u.System_DBNull)
1346 return TypeCode.DBNull;
1348 else if (type == u.System_Decimal)
1350 return TypeCode.Decimal;
1352 else if (type == u.System_String)
1354 return TypeCode.String;
1358 return TypeCode.Object;
1362 public Assembly Assembly
1364 get { return Module.Assembly; }
1367 // note that interface/delegate co- and contravariance is not considered
1368 public bool IsAssignableFrom(Type type)
1370 if (this.Equals(type))
1374 else if (type == null)
1378 else if (this.IsArray && type.IsArray)
1380 if (this.GetArrayRank() != type.GetArrayRank())
1384 else if (this.__IsVector && !type.__IsVector)
1388 Type e1 = this.GetElementType();
1389 Type e2 = type.GetElementType();
1390 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1392 else if (this.IsSealed)
1396 else if (this.IsInterface)
1398 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1400 else if (type.IsInterface)
1402 return this == this.Module.universe.System_Object;
1404 else if (type.IsPointer)
1406 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1410 return type.IsSubclassOf(this);
1414 public bool IsSubclassOf(Type type)
1416 Type thisType = this.BaseType;
1417 while (thisType != null)
1419 if (thisType.Equals(type))
1423 thisType = thisType.BaseType;
1428 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1429 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1430 private bool IsDirectlyImplementedInterface(Type interfaceType)
1432 foreach (Type iface in __GetDeclaredInterfaces())
1434 if (interfaceType.IsAssignableFrom(iface))
1442 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1445 InterfaceMapping map = new InterfaceMapping();
1446 if (!IsDirectlyImplementedInterface(interfaceType))
1448 Type baseType = this.BaseType;
1449 if (baseType == null)
1451 throw new ArgumentException();
1455 map = baseType.GetInterfaceMap(interfaceType);
1460 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1461 map.InterfaceType = interfaceType;
1462 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1463 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1464 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1465 for (int i = 0; i < map.TargetMethods.Length; i++)
1467 if (map.TargetMethods[i] == null)
1469 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1470 for (int j = 0; j < methods.Length; j++)
1472 if (methods[j].Name == map.InterfaceMethods[i].Name
1473 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1475 map.TargetMethods[i] = methods[j];
1480 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1482 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1485 map.TargetType = this;
1489 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1491 __MethodImplMap impl = __GetMethodImplMap();
1492 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1494 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1496 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1497 if (index != -1 && targetMethods[index] == null)
1499 targetMethods[index] = impl.MethodBodies[i];
1505 Type IGenericContext.GetGenericTypeArgument(int index)
1507 return GetGenericTypeArgument(index);
1510 Type IGenericContext.GetGenericMethodArgument(int index)
1512 throw new BadImageFormatException();
1515 Type IGenericBinder.BindTypeParameter(Type type)
1517 return GetGenericTypeArgument(type.GenericParameterPosition);
1520 Type IGenericBinder.BindMethodParameter(Type type)
1522 throw new BadImageFormatException();
1525 internal virtual Type BindTypeParameters(IGenericBinder binder)
1527 if (IsGenericTypeDefinition)
1529 Type[] args = GetGenericArguments();
1530 Type.InplaceBindTypeParameters(binder, args);
1531 return GenericTypeInstance.Make(this, args, null, null);
1539 internal static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1541 for (int i = 0; i < types.Length; i++)
1543 types[i] = types[i].BindTypeParameters(binder);
1547 internal MethodBase FindMethod(string name, MethodSignature signature)
1549 foreach (MethodBase method in __GetDeclaredMethods())
1551 if (method.Name == name && method.MethodSignature.Equals(signature))
1559 internal FieldInfo FindField(string name, FieldSignature signature)
1561 foreach (FieldInfo field in __GetDeclaredFields())
1563 if (field.Name == name && field.FieldSignature.Equals(signature))
1571 internal bool IsAllowMultipleCustomAttribute
1575 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1578 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1580 if (arg.MemberInfo.Name == "AllowMultiple")
1582 return (bool)arg.TypedValue.Value;
1590 internal bool IsPseudoCustomAttribute
1594 Universe u = this.Module.universe;
1595 return this == u.System_NonSerializedAttribute
1596 || this == u.System_SerializableAttribute
1597 || this == u.System_Runtime_InteropServices_DllImportAttribute
1598 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1599 || this == u.System_Runtime_InteropServices_InAttribute
1600 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1601 || this == u.System_Runtime_InteropServices_OutAttribute
1602 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1603 || this == u.System_Runtime_InteropServices_OptionalAttribute
1604 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1605 || this == u.System_Runtime_InteropServices_ComImportAttribute
1606 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1607 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1613 abstract class ElementHolderType : Type
1615 protected readonly Type elementType;
1617 private readonly Type[] requiredCustomModifiers;
1618 private readonly Type[] optionalCustomModifiers;
1620 protected ElementHolderType(Type elementType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1622 this.elementType = elementType;
1623 this.requiredCustomModifiers = requiredCustomModifiers;
1624 this.optionalCustomModifiers = optionalCustomModifiers;
1627 protected bool EqualsHelper(ElementHolderType other)
1629 return other != null
1630 && other.elementType.Equals(elementType)
1631 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers)
1632 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers);
1635 public override Type[] __GetRequiredCustomModifiers()
1637 return Util.Copy(requiredCustomModifiers);
1640 public override Type[] __GetOptionalCustomModifiers()
1642 return Util.Copy(optionalCustomModifiers);
1645 public sealed override string Name
1647 get { return elementType.Name + GetSuffix(); }
1650 public sealed override string Namespace
1652 get { return elementType.Namespace; }
1655 public sealed override string FullName
1657 get { return elementType.FullName + GetSuffix(); }
1660 public sealed override string ToString()
1662 return elementType.ToString() + GetSuffix();
1665 public sealed override Type GetElementType()
1670 public sealed override bool HasElementType
1672 get { return true; }
1675 public sealed override Module Module
1677 get { return elementType.Module; }
1680 internal sealed override int GetModuleBuilderToken()
1684 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1689 public sealed override bool ContainsGenericParameters
1693 Type type = elementType;
1694 while (type.HasElementType)
1696 type = type.GetElementType();
1698 return type.ContainsGenericParameters;
1702 public sealed override bool __ContainsMissingType
1706 Type type = elementType;
1707 while (type.HasElementType)
1709 type = type.GetElementType();
1711 return type.__ContainsMissingType;
1715 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1717 Type type = elementType.BindTypeParameters(binder);
1718 Type[] req = BindArray(requiredCustomModifiers, binder);
1719 Type[] opt = BindArray(optionalCustomModifiers, binder);
1720 if (ReferenceEquals(type, elementType)
1721 && ReferenceEquals(req, requiredCustomModifiers)
1722 && ReferenceEquals(opt, optionalCustomModifiers))
1726 return Wrap(type, req, opt);
1729 internal override void CheckBaked()
1731 elementType.CheckBaked();
1734 private static Type[] BindArray(Type[] array, IGenericBinder binder)
1736 if (array ==null || array.Length == 0)
1740 Type[] result = array;
1741 for (int i = 0; i < array.Length; i++)
1743 Type type = array[i].BindTypeParameters(binder);
1744 if (!ReferenceEquals(type, array[i]))
1746 if (result == array)
1748 result = (Type[])array.Clone();
1756 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1758 return CustomAttributeData.EmptyList;
1761 protected abstract string GetSuffix();
1763 protected abstract Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers);
1766 sealed class ArrayType : ElementHolderType
1768 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1770 return type.Module.CanonicalizeType(new ArrayType(type, requiredCustomModifiers, optionalCustomModifiers));
1773 private ArrayType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1774 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1778 public override Type BaseType
1780 get { return elementType.Module.universe.System_Array; }
1783 public override Type[] __GetDeclaredInterfaces()
1786 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
1787 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
1788 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
1792 public override MethodBase[] __GetDeclaredMethods()
1794 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
1795 List<MethodBase> list = new List<MethodBase>();
1796 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 }));
1797 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
1798 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
1799 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1800 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
1802 Array.Resize(ref int32, int32.Length + 1);
1803 int32[int32.Length - 1] = int32[0];
1804 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1806 return list.ToArray();
1809 public override TypeAttributes Attributes
1811 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1814 public override bool IsArray
1816 get { return true; }
1819 public override bool __IsVector
1821 get { return true; }
1824 public override int GetArrayRank()
1829 public override bool Equals(object o)
1831 return EqualsHelper(o as ArrayType);
1834 public override int GetHashCode()
1836 return elementType.GetHashCode() * 5;
1839 protected override string GetSuffix()
1844 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1846 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
1850 sealed class MultiArrayType : ElementHolderType
1852 private readonly int rank;
1854 internal static Type Make(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1856 return type.Module.CanonicalizeType(new MultiArrayType(type, rank, requiredCustomModifiers, optionalCustomModifiers));
1859 private MultiArrayType(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1860 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1865 public override Type BaseType
1867 get { return elementType.Module.universe.System_Array; }
1870 public override MethodBase[] __GetDeclaredMethods()
1872 Type int32 = this.Module.universe.System_Int32;
1873 Type[] setArgs = new Type[rank + 1];
1874 Type[] getArgs = new Type[rank];
1875 Type[] ctorArgs = new Type[rank * 2];
1876 for (int i = 0; i < rank; i++)
1880 ctorArgs[i * 2 + 0] = int32;
1881 ctorArgs[i * 2 + 1] = int32;
1883 setArgs[rank] = elementType;
1884 return new MethodBase[] {
1885 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
1886 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
1887 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
1888 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
1889 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
1893 public override TypeAttributes Attributes
1895 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1898 public override bool IsArray
1900 get { return true; }
1903 public override int GetArrayRank()
1908 public override bool Equals(object o)
1910 MultiArrayType at = o as MultiArrayType;
1911 return EqualsHelper(at) && at.rank == rank;
1914 public override int GetHashCode()
1916 return elementType.GetHashCode() * 9 + rank;
1919 protected override string GetSuffix()
1927 return "[" + new String(',', rank - 1) + "]";
1931 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1933 return Make(type, rank, requiredCustomModifiers, optionalCustomModifiers);
1937 sealed class BuiltinArrayMethod : ArrayMethod
1939 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
1940 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
1944 public override MethodAttributes Attributes
1946 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
1949 public override MethodImplAttributes GetMethodImplementationFlags()
1951 return MethodImplAttributes.IL;
1954 public override int MetadataToken
1956 get { return 0x06000000; }
1959 public override MethodBody GetMethodBody()
1964 public override ParameterInfo[] GetParameters()
1966 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
1967 for (int i = 0; i < parameterInfos.Length; i++)
1969 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
1971 return parameterInfos;
1974 public override ParameterInfo ReturnParameter
1976 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
1979 private sealed class ParameterInfoImpl : ParameterInfo
1981 private readonly MethodInfo method;
1982 private readonly Type type;
1983 private readonly int pos;
1985 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
1987 this.method = method;
1992 public override Type ParameterType
1994 get { return type; }
1997 public override string Name
1999 get { return null; }
2002 public override ParameterAttributes Attributes
2004 get { return ParameterAttributes.None; }
2007 public override int Position
2012 public override object RawDefaultValue
2014 get { return null; }
2017 public override Type[] GetOptionalCustomModifiers()
2019 return Empty<Type>.Array;
2022 public override Type[] GetRequiredCustomModifiers()
2024 return Empty<Type>.Array;
2027 public override MemberInfo Member
2029 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2032 public override int MetadataToken
2034 get { return 0x8000000; }
2037 internal override Module Module
2039 get { return method.Module; }
2044 sealed class ByRefType : ElementHolderType
2046 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2048 return type.Module.CanonicalizeType(new ByRefType(type, requiredCustomModifiers, optionalCustomModifiers));
2051 private ByRefType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2052 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2056 public override bool Equals(object o)
2058 return EqualsHelper(o as ByRefType);
2061 public override int GetHashCode()
2063 return elementType.GetHashCode() * 3;
2066 public override Type BaseType
2068 get { return null; }
2071 public override TypeAttributes Attributes
2076 public override bool IsByRef
2078 get { return true; }
2081 protected override string GetSuffix()
2086 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2088 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2092 sealed class PointerType : ElementHolderType
2094 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2096 return type.Module.CanonicalizeType(new PointerType(type, requiredCustomModifiers, optionalCustomModifiers));
2099 private PointerType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2100 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2104 public override bool Equals(object o)
2106 return EqualsHelper(o as PointerType);
2109 public override int GetHashCode()
2111 return elementType.GetHashCode() * 7;
2114 public override Type BaseType
2116 get { return null; }
2119 public override TypeAttributes Attributes
2124 public override bool IsPointer
2126 get { return true; }
2129 protected override string GetSuffix()
2134 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2136 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2140 sealed class GenericTypeInstance : Type
2142 private readonly Type type;
2143 private readonly Type[] args;
2144 private readonly Type[][] requiredCustomModifiers;
2145 private readonly Type[][] optionalCustomModifiers;
2146 private Type baseType;
2149 internal static Type Make(Type type, Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2151 bool identity = true;
2152 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2154 // a TypeBuiler identity must be instantiated
2159 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2160 for (int i = 0; i < typeArguments.Length; i++)
2162 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2163 || !IsEmpty(requiredCustomModifiers, i)
2164 || !IsEmpty(optionalCustomModifiers, i))
2177 return type.Module.CanonicalizeType(new GenericTypeInstance(type, typeArguments, requiredCustomModifiers, optionalCustomModifiers));
2181 private static bool IsEmpty(Type[][] mods, int i)
2183 // we need to be extra careful, because mods doesn't not need to be in canonical format
2184 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2185 return mods == null || mods[i] == null || mods[i].Length == 0;
2188 private GenericTypeInstance(Type type, Type[] args, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2192 this.requiredCustomModifiers = requiredCustomModifiers;
2193 this.optionalCustomModifiers = optionalCustomModifiers;
2196 public override bool Equals(object o)
2198 GenericTypeInstance gt = o as GenericTypeInstance;
2199 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2200 && Util.ArrayEquals(gt.requiredCustomModifiers, requiredCustomModifiers)
2201 && Util.ArrayEquals(gt.optionalCustomModifiers, optionalCustomModifiers);
2204 public override int GetHashCode()
2206 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2209 public override string AssemblyQualifiedName
2213 string fn = FullName;
2214 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2218 public override Type BaseType
2222 if (baseType == null)
2224 Type rawBaseType = type.BaseType;
2225 if (rawBaseType == null)
2227 baseType = rawBaseType;
2231 baseType = rawBaseType.BindTypeParameters(this);
2238 public override bool IsValueType
2240 get { return type.IsValueType; }
2243 public override bool IsVisible
2249 foreach (Type arg in args)
2262 public override Type DeclaringType
2264 get { return type.DeclaringType; }
2267 public override TypeAttributes Attributes
2269 get { return type.Attributes; }
2272 internal override void CheckBaked()
2277 public override FieldInfo[] __GetDeclaredFields()
2279 FieldInfo[] fields = type.__GetDeclaredFields();
2280 for (int i = 0; i < fields.Length; i++)
2282 fields[i] = fields[i].BindTypeParameters(this);
2287 public override Type[] __GetDeclaredInterfaces()
2289 Type[] interfaces = type.__GetDeclaredInterfaces();
2290 for (int i = 0; i < interfaces.Length; i++)
2292 interfaces[i] = interfaces[i].BindTypeParameters(this);
2297 public override MethodBase[] __GetDeclaredMethods()
2299 MethodBase[] methods = type.__GetDeclaredMethods();
2300 for (int i = 0; i < methods.Length; i++)
2302 methods[i] = methods[i].BindTypeParameters(this);
2307 public override Type[] __GetDeclaredTypes()
2309 return type.__GetDeclaredTypes();
2312 public override EventInfo[] __GetDeclaredEvents()
2314 EventInfo[] events = type.__GetDeclaredEvents();
2315 for (int i = 0; i < events.Length; i++)
2317 events[i] = events[i].BindTypeParameters(this);
2322 public override PropertyInfo[] __GetDeclaredProperties()
2324 PropertyInfo[] properties = type.__GetDeclaredProperties();
2325 for (int i = 0; i < properties.Length; i++)
2327 properties[i] = properties[i].BindTypeParameters(this);
2332 public override __MethodImplMap __GetMethodImplMap()
2334 __MethodImplMap map = type.__GetMethodImplMap();
2335 map.TargetType = this;
2336 for (int i = 0; i < map.MethodBodies.Length; i++)
2338 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2339 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2341 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2342 if (interfaceType.IsGenericType)
2344 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2351 public override string Namespace
2353 get { return type.Namespace; }
2356 public override Type UnderlyingSystemType
2358 get { return this; }
2361 public override string Name
2363 get { return type.Name; }
2366 public override string FullName
2370 if (this.ContainsGenericParameters)
2374 StringBuilder sb = new StringBuilder(this.type.FullName);
2376 foreach (Type type in args)
2378 sb.Append('[').Append(type.AssemblyQualifiedName.Replace("]", "\\]")).Append(']');
2381 return sb.ToString();
2385 public override string ToString()
2387 StringBuilder sb = new StringBuilder(type.FullName);
2390 foreach (Type arg in args)
2397 return sb.ToString();
2400 public override Module Module
2402 get { return type.Module; }
2405 public override bool IsGenericType
2407 get { return true; }
2410 public override Type GetGenericTypeDefinition()
2415 public override Type[] GetGenericArguments()
2417 return Util.Copy(args);
2420 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
2422 return Util.Copy(requiredCustomModifiers ?? new Type[args.Length][]);
2425 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
2427 return Util.Copy(optionalCustomModifiers ?? new Type[args.Length][]);
2430 internal override Type GetGenericTypeArgument(int index)
2435 public override bool ContainsGenericParameters
2439 foreach (Type type in args)
2441 if (type.ContainsGenericParameters)
2450 public override bool __ContainsMissingType
2454 foreach (Type type in args)
2456 if (type.__ContainsMissingType)
2465 public override StructLayoutAttribute StructLayoutAttribute
2467 get { return type.StructLayoutAttribute; }
2470 internal override int GetModuleBuilderToken()
2474 token = ((ModuleBuilder)type.Module).ImportType(this);
2479 internal override Type BindTypeParameters(IGenericBinder binder)
2481 for (int i = 0; i < args.Length; i++)
2483 Type xarg = args[i].BindTypeParameters(binder);
2484 if (!ReferenceEquals(xarg, args[i]))
2486 Type[] xargs = new Type[args.Length];
2487 Array.Copy(args, xargs, i);
2489 for (; i < args.Length; i++)
2491 xargs[i] = args[i].BindTypeParameters(binder);
2493 return Make(type, xargs, null, null);
2499 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2501 return type.GetCustomAttributesData(attributeType);