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)
776 foreach (Type type in __GetDeclaredTypes())
778 if (MatchTypeNames(type.__Namespace, type.__Name, name))
786 public Type GetNestedType(string name)
788 return GetNestedType(name, BindingFlags.Public);
791 public Type GetNestedType(string name, BindingFlags bindingAttr)
793 foreach (Type type in GetNestedTypes(bindingAttr))
795 // FXBUG the namespace is ignored
796 if (type.__Name == name)
804 public Type[] GetNestedTypes()
806 return GetNestedTypes(BindingFlags.Public);
809 public Type[] GetNestedTypes(BindingFlags bindingAttr)
812 List<Type> list = new List<Type>();
813 foreach (Type type in __GetDeclaredTypes())
815 if (BindingFlagsMatch(type.IsNestedPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic))
820 return list.ToArray();
823 public PropertyInfo[] GetProperties()
825 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
828 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
830 List<PropertyInfo> list = new List<PropertyInfo>();
835 foreach (PropertyInfo property in type.__GetDeclaredProperties())
837 if (BindingFlagsMatch(property.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
838 && BindingFlagsMatch(property.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
843 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
845 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
847 bindingAttr &= ~BindingFlags.Static;
849 type = type.BaseType;
856 return list.ToArray();
859 public PropertyInfo GetProperty(string name)
861 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
864 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
866 foreach (PropertyInfo prop in GetProperties(bindingAttr))
868 if (prop.Name == name)
876 public PropertyInfo GetProperty(string name, Type returnType)
878 PropertyInfo found = null;
879 foreach (PropertyInfo prop in GetProperties())
881 if (prop.Name == name && prop.PropertyType.Equals(returnType))
885 throw new AmbiguousMatchException();
893 public PropertyInfo GetProperty(string name, Type[] types)
895 PropertyInfo found = null;
896 foreach (PropertyInfo prop in GetProperties())
898 if (prop.Name == name && MatchParameterTypes(prop.GetIndexParameters(), types))
902 throw new AmbiguousMatchException();
910 private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
912 if (parameters.Length == types.Length)
914 for (int i = 0; i < parameters.Length; i++)
916 if (!parameters[i].ParameterType.Equals(types[i]))
926 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
928 return GetProperty(name, returnType, types, null);
931 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
933 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
936 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
938 PropertyInfo found = null;
939 foreach (PropertyInfo prop in GetProperties(bindingAttr))
941 if (prop.Name == name && prop.PropertyType.Equals(returnType) && MatchParameterTypes(prop.GetIndexParameters(), types))
945 throw new AmbiguousMatchException();
953 public Type GetInterface(string name)
955 return GetInterface(name, false);
958 public Type GetInterface(string name, bool ignoreCase)
962 throw new NotImplementedException();
964 foreach (Type type in GetInterfaces())
966 if (type.FullName == name)
974 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
976 List<Type> list = new List<Type>();
977 foreach (Type type in GetInterfaces())
979 if (filter(type, filterCriteria))
984 return list.ToArray();
987 public ConstructorInfo TypeInitializer
989 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
992 public bool IsPrimitive
996 Universe u = this.Module.universe;
997 return this == u.System_Boolean
998 || this == u.System_Byte
999 || this == u.System_SByte
1000 || this == u.System_Int16
1001 || this == u.System_UInt16
1002 || this == u.System_Int32
1003 || this == u.System_UInt32
1004 || this == u.System_Int64
1005 || this == u.System_UInt64
1006 || this == u.System_IntPtr
1007 || this == u.System_UIntPtr
1008 || this == u.System_Char
1009 || this == u.System_Double
1010 || this == u.System_Single
1017 get { return this.BaseType == this.Module.universe.System_Enum; }
1020 public bool IsSealed
1022 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1025 public bool IsAbstract
1027 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1030 private bool CheckVisibility(TypeAttributes access)
1032 return (Attributes & TypeAttributes.VisibilityMask) == access;
1035 public bool IsPublic
1037 get { return CheckVisibility(TypeAttributes.Public); }
1040 public bool IsNestedPublic
1042 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1045 public bool IsNestedPrivate
1047 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1050 public bool IsNestedFamily
1052 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1055 public bool IsNestedAssembly
1057 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1060 public bool IsNestedFamANDAssem
1062 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1065 public bool IsNestedFamORAssem
1067 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1070 public bool IsNotPublic
1072 get { return CheckVisibility(TypeAttributes.NotPublic); }
1075 public bool IsImport
1077 get { return (Attributes & TypeAttributes.Import) != 0; }
1080 public bool IsCOMObject
1082 get { return IsClass && IsImport; }
1085 public bool IsContextful
1087 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1090 public bool IsMarshalByRef
1092 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1095 public virtual bool IsVisible
1097 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1100 public bool IsAnsiClass
1102 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1105 public bool IsUnicodeClass
1107 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1110 public bool IsAutoClass
1112 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1115 public bool IsAutoLayout
1117 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1120 public bool IsLayoutSequential
1122 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1125 public bool IsExplicitLayout
1127 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1130 public bool IsSpecialName
1132 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1135 public bool IsSerializable
1137 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1142 get { return !IsInterface && !IsValueType; }
1145 public bool IsInterface
1147 get { return (Attributes & TypeAttributes.Interface) != 0; }
1150 public bool IsNested
1152 // FXBUG we check the declaring type (like .NET) and this results
1153 // in IsNested returning true for a generic type parameter
1154 get { return this.DeclaringType != null; }
1157 public bool __IsMissing
1159 get { return this is MissingType; }
1162 public virtual bool __ContainsMissingType
1166 if (this.__IsMissing)
1170 foreach (Type arg in this.GetGenericArguments())
1172 if (arg.__ContainsMissingType)
1181 public Type MakeArrayType()
1183 return ArrayType.Make(this, null, null);
1186 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1188 return ArrayType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1191 public Type MakeArrayType(int rank)
1193 return MultiArrayType.Make(this, rank, null, null);
1196 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1198 return MultiArrayType.Make(this, rank, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1201 public Type MakeByRefType()
1203 return ByRefType.Make(this, null, null);
1206 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1208 return ByRefType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1211 public Type MakePointerType()
1213 return PointerType.Make(this, null, null);
1216 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1218 return PointerType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1221 public Type MakeGenericType(params Type[] typeArguments)
1223 return __MakeGenericType(typeArguments, null, null);
1226 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1228 if (!this.IsGenericTypeDefinition)
1230 throw new InvalidOperationException();
1232 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1235 public static System.Type __GetSystemType(TypeCode typeCode)
1239 case TypeCode.Boolean:
1240 return typeof(System.Boolean);
1242 return typeof(System.Byte);
1244 return typeof(System.Char);
1245 case TypeCode.DBNull:
1246 return typeof(System.DBNull);
1247 case TypeCode.DateTime:
1248 return typeof(System.DateTime);
1249 case TypeCode.Decimal:
1250 return typeof(System.Decimal);
1251 case TypeCode.Double:
1252 return typeof(System.Double);
1253 case TypeCode.Empty:
1255 case TypeCode.Int16:
1256 return typeof(System.Int16);
1257 case TypeCode.Int32:
1258 return typeof(System.Int32);
1259 case TypeCode.Int64:
1260 return typeof(System.Int64);
1261 case TypeCode.Object:
1262 return typeof(System.Object);
1263 case TypeCode.SByte:
1264 return typeof(System.SByte);
1265 case TypeCode.Single:
1266 return typeof(System.Single);
1267 case TypeCode.String:
1268 return typeof(System.String);
1269 case TypeCode.UInt16:
1270 return typeof(System.UInt16);
1271 case TypeCode.UInt32:
1272 return typeof(System.UInt32);
1273 case TypeCode.UInt64:
1274 return typeof(System.UInt64);
1276 throw new ArgumentOutOfRangeException();
1280 public static TypeCode GetTypeCode(Type type)
1284 return TypeCode.Empty;
1288 type = type.GetEnumUnderlyingType();
1290 Universe u = type.Module.universe;
1291 if (type == u.System_Boolean)
1293 return TypeCode.Boolean;
1295 else if (type == u.System_Char)
1297 return TypeCode.Char;
1299 else if (type == u.System_SByte)
1301 return TypeCode.SByte;
1303 else if (type == u.System_Byte)
1305 return TypeCode.Byte;
1307 else if (type == u.System_Int16)
1309 return TypeCode.Int16;
1311 else if (type == u.System_UInt16)
1313 return TypeCode.UInt16;
1315 else if (type == u.System_Int32)
1317 return TypeCode.Int32;
1319 else if (type == u.System_UInt32)
1321 return TypeCode.UInt32;
1323 else if (type == u.System_Int64)
1325 return TypeCode.Int64;
1327 else if (type == u.System_UInt64)
1329 return TypeCode.UInt64;
1331 else if (type == u.System_Single)
1333 return TypeCode.Single;
1335 else if (type == u.System_Double)
1337 return TypeCode.Double;
1339 else if (type == u.System_DateTime)
1341 return TypeCode.DateTime;
1343 else if (type == u.System_DBNull)
1345 return TypeCode.DBNull;
1347 else if (type == u.System_Decimal)
1349 return TypeCode.Decimal;
1351 else if (type == u.System_String)
1353 return TypeCode.String;
1357 return TypeCode.Object;
1361 public Assembly Assembly
1363 get { return Module.Assembly; }
1366 // note that interface/delegate co- and contravariance is not considered
1367 public bool IsAssignableFrom(Type type)
1369 if (this.Equals(type))
1373 else if (type == null)
1377 else if (this.IsArray && type.IsArray)
1379 if (this.GetArrayRank() != type.GetArrayRank())
1383 else if (this.__IsVector && !type.__IsVector)
1387 Type e1 = this.GetElementType();
1388 Type e2 = type.GetElementType();
1389 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1391 else if (this.IsSealed)
1395 else if (this.IsInterface)
1397 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1399 else if (type.IsInterface)
1401 return this == this.Module.universe.System_Object;
1403 else if (type.IsPointer)
1405 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1409 return type.IsSubclassOf(this);
1413 public bool IsSubclassOf(Type type)
1415 Type thisType = this.BaseType;
1416 while (thisType != null)
1418 if (thisType.Equals(type))
1422 thisType = thisType.BaseType;
1427 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1428 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1429 private bool IsDirectlyImplementedInterface(Type interfaceType)
1431 foreach (Type iface in __GetDeclaredInterfaces())
1433 if (interfaceType.IsAssignableFrom(iface))
1441 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1444 InterfaceMapping map = new InterfaceMapping();
1445 if (!IsDirectlyImplementedInterface(interfaceType))
1447 Type baseType = this.BaseType;
1448 if (baseType == null)
1450 throw new ArgumentException();
1454 map = baseType.GetInterfaceMap(interfaceType);
1459 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1460 map.InterfaceType = interfaceType;
1461 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1462 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1463 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1464 for (int i = 0; i < map.TargetMethods.Length; i++)
1466 if (map.TargetMethods[i] == null)
1468 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1469 for (int j = 0; j < methods.Length; j++)
1471 if (methods[j].Name == map.InterfaceMethods[i].Name
1472 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1474 map.TargetMethods[i] = methods[j];
1479 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1481 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1484 map.TargetType = this;
1488 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1490 __MethodImplMap impl = __GetMethodImplMap();
1491 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1493 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1495 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1496 if (index != -1 && targetMethods[index] == null)
1498 targetMethods[index] = impl.MethodBodies[i];
1504 Type IGenericContext.GetGenericTypeArgument(int index)
1506 return GetGenericTypeArgument(index);
1509 Type IGenericContext.GetGenericMethodArgument(int index)
1511 throw new BadImageFormatException();
1514 Type IGenericBinder.BindTypeParameter(Type type)
1516 return GetGenericTypeArgument(type.GenericParameterPosition);
1519 Type IGenericBinder.BindMethodParameter(Type type)
1521 throw new BadImageFormatException();
1524 internal virtual Type BindTypeParameters(IGenericBinder binder)
1526 if (IsGenericTypeDefinition)
1528 Type[] args = GetGenericArguments();
1529 Type.InplaceBindTypeParameters(binder, args);
1530 return GenericTypeInstance.Make(this, args, null, null);
1538 internal static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1540 for (int i = 0; i < types.Length; i++)
1542 types[i] = types[i].BindTypeParameters(binder);
1546 internal MethodBase FindMethod(string name, MethodSignature signature)
1548 foreach (MethodBase method in __GetDeclaredMethods())
1550 if (method.Name == name && method.MethodSignature.Equals(signature))
1558 internal FieldInfo FindField(string name, FieldSignature signature)
1560 foreach (FieldInfo field in __GetDeclaredFields())
1562 if (field.Name == name && field.FieldSignature.Equals(signature))
1570 internal bool IsAllowMultipleCustomAttribute
1574 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1577 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1579 if (arg.MemberInfo.Name == "AllowMultiple")
1581 return (bool)arg.TypedValue.Value;
1589 internal bool IsPseudoCustomAttribute
1593 Universe u = this.Module.universe;
1594 return this == u.System_NonSerializedAttribute
1595 || this == u.System_SerializableAttribute
1596 || this == u.System_Runtime_InteropServices_DllImportAttribute
1597 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1598 || this == u.System_Runtime_InteropServices_InAttribute
1599 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1600 || this == u.System_Runtime_InteropServices_OutAttribute
1601 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1602 || this == u.System_Runtime_InteropServices_OptionalAttribute
1603 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1604 || this == u.System_Runtime_InteropServices_ComImportAttribute
1605 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1606 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1612 abstract class ElementHolderType : Type
1614 protected readonly Type elementType;
1616 private readonly Type[] requiredCustomModifiers;
1617 private readonly Type[] optionalCustomModifiers;
1619 protected ElementHolderType(Type elementType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1621 this.elementType = elementType;
1622 this.requiredCustomModifiers = requiredCustomModifiers;
1623 this.optionalCustomModifiers = optionalCustomModifiers;
1626 protected bool EqualsHelper(ElementHolderType other)
1628 return other != null
1629 && other.elementType.Equals(elementType)
1630 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers)
1631 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers);
1634 public override Type[] __GetRequiredCustomModifiers()
1636 return Util.Copy(requiredCustomModifiers);
1639 public override Type[] __GetOptionalCustomModifiers()
1641 return Util.Copy(optionalCustomModifiers);
1644 public sealed override string Name
1646 get { return elementType.Name + GetSuffix(); }
1649 public sealed override string Namespace
1651 get { return elementType.Namespace; }
1654 public sealed override string FullName
1656 get { return elementType.FullName + GetSuffix(); }
1659 public sealed override string ToString()
1661 return elementType.ToString() + GetSuffix();
1664 public sealed override Type GetElementType()
1669 public sealed override bool HasElementType
1671 get { return true; }
1674 public sealed override Module Module
1676 get { return elementType.Module; }
1679 internal sealed override int GetModuleBuilderToken()
1683 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1688 public sealed override bool ContainsGenericParameters
1692 Type type = elementType;
1693 while (type.HasElementType)
1695 type = type.GetElementType();
1697 return type.ContainsGenericParameters;
1701 public sealed override bool __ContainsMissingType
1705 Type type = elementType;
1706 while (type.HasElementType)
1708 type = type.GetElementType();
1710 return type.__ContainsMissingType;
1714 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1716 Type type = elementType.BindTypeParameters(binder);
1717 Type[] req = BindArray(requiredCustomModifiers, binder);
1718 Type[] opt = BindArray(optionalCustomModifiers, binder);
1719 if (ReferenceEquals(type, elementType)
1720 && ReferenceEquals(req, requiredCustomModifiers)
1721 && ReferenceEquals(opt, optionalCustomModifiers))
1725 return Wrap(type, req, opt);
1728 internal override void CheckBaked()
1730 elementType.CheckBaked();
1733 private static Type[] BindArray(Type[] array, IGenericBinder binder)
1735 if (array ==null || array.Length == 0)
1739 Type[] result = array;
1740 for (int i = 0; i < array.Length; i++)
1742 Type type = array[i].BindTypeParameters(binder);
1743 if (!ReferenceEquals(type, array[i]))
1745 if (result == array)
1747 result = (Type[])array.Clone();
1755 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1757 return CustomAttributeData.EmptyList;
1760 protected abstract string GetSuffix();
1762 protected abstract Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers);
1765 sealed class ArrayType : ElementHolderType
1767 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1769 return type.Module.CanonicalizeType(new ArrayType(type, requiredCustomModifiers, optionalCustomModifiers));
1772 private ArrayType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1773 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1777 public override Type BaseType
1779 get { return elementType.Module.universe.System_Array; }
1782 public override Type[] __GetDeclaredInterfaces()
1785 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
1786 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
1787 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
1791 public override MethodBase[] __GetDeclaredMethods()
1793 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
1794 List<MethodBase> list = new List<MethodBase>();
1795 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 }));
1796 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
1797 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
1798 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1799 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
1801 Array.Resize(ref int32, int32.Length + 1);
1802 int32[int32.Length - 1] = int32[0];
1803 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1805 return list.ToArray();
1808 public override TypeAttributes Attributes
1810 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1813 public override bool IsArray
1815 get { return true; }
1818 public override bool __IsVector
1820 get { return true; }
1823 public override int GetArrayRank()
1828 public override bool Equals(object o)
1830 return EqualsHelper(o as ArrayType);
1833 public override int GetHashCode()
1835 return elementType.GetHashCode() * 5;
1838 protected override string GetSuffix()
1843 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1845 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
1849 sealed class MultiArrayType : ElementHolderType
1851 private readonly int rank;
1853 internal static Type Make(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1855 return type.Module.CanonicalizeType(new MultiArrayType(type, rank, requiredCustomModifiers, optionalCustomModifiers));
1858 private MultiArrayType(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1859 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1864 public override Type BaseType
1866 get { return elementType.Module.universe.System_Array; }
1869 public override MethodBase[] __GetDeclaredMethods()
1871 Type int32 = this.Module.universe.System_Int32;
1872 Type[] setArgs = new Type[rank + 1];
1873 Type[] getArgs = new Type[rank];
1874 Type[] ctorArgs = new Type[rank * 2];
1875 for (int i = 0; i < rank; i++)
1879 ctorArgs[i * 2 + 0] = int32;
1880 ctorArgs[i * 2 + 1] = int32;
1882 setArgs[rank] = elementType;
1883 return new MethodBase[] {
1884 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
1885 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
1886 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
1887 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
1888 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
1892 public override TypeAttributes Attributes
1894 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1897 public override bool IsArray
1899 get { return true; }
1902 public override int GetArrayRank()
1907 public override bool Equals(object o)
1909 MultiArrayType at = o as MultiArrayType;
1910 return EqualsHelper(at) && at.rank == rank;
1913 public override int GetHashCode()
1915 return elementType.GetHashCode() * 9 + rank;
1918 protected override string GetSuffix()
1926 return "[" + new String(',', rank - 1) + "]";
1930 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1932 return Make(type, rank, requiredCustomModifiers, optionalCustomModifiers);
1936 sealed class BuiltinArrayMethod : ArrayMethod
1938 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
1939 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
1943 public override MethodAttributes Attributes
1945 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
1948 public override MethodImplAttributes GetMethodImplementationFlags()
1950 return MethodImplAttributes.IL;
1953 public override int MetadataToken
1955 get { return 0x06000000; }
1958 public override MethodBody GetMethodBody()
1963 public override ParameterInfo[] GetParameters()
1965 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
1966 for (int i = 0; i < parameterInfos.Length; i++)
1968 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
1970 return parameterInfos;
1973 public override ParameterInfo ReturnParameter
1975 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
1978 private sealed class ParameterInfoImpl : ParameterInfo
1980 private readonly MethodInfo method;
1981 private readonly Type type;
1982 private readonly int pos;
1984 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
1986 this.method = method;
1991 public override Type ParameterType
1993 get { return type; }
1996 public override string Name
1998 get { return null; }
2001 public override ParameterAttributes Attributes
2003 get { return ParameterAttributes.None; }
2006 public override int Position
2011 public override object RawDefaultValue
2013 get { return null; }
2016 public override Type[] GetOptionalCustomModifiers()
2018 return Empty<Type>.Array;
2021 public override Type[] GetRequiredCustomModifiers()
2023 return Empty<Type>.Array;
2026 public override MemberInfo Member
2028 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2031 public override int MetadataToken
2033 get { return 0x8000000; }
2036 internal override Module Module
2038 get { return method.Module; }
2043 sealed class ByRefType : ElementHolderType
2045 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2047 return type.Module.CanonicalizeType(new ByRefType(type, requiredCustomModifiers, optionalCustomModifiers));
2050 private ByRefType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2051 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2055 public override bool Equals(object o)
2057 return EqualsHelper(o as ByRefType);
2060 public override int GetHashCode()
2062 return elementType.GetHashCode() * 3;
2065 public override Type BaseType
2067 get { return null; }
2070 public override TypeAttributes Attributes
2075 public override bool IsByRef
2077 get { return true; }
2080 protected override string GetSuffix()
2085 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2087 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2091 sealed class PointerType : ElementHolderType
2093 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2095 return type.Module.CanonicalizeType(new PointerType(type, requiredCustomModifiers, optionalCustomModifiers));
2098 private PointerType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2099 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2103 public override bool Equals(object o)
2105 return EqualsHelper(o as PointerType);
2108 public override int GetHashCode()
2110 return elementType.GetHashCode() * 7;
2113 public override Type BaseType
2115 get { return null; }
2118 public override TypeAttributes Attributes
2123 public override bool IsPointer
2125 get { return true; }
2128 protected override string GetSuffix()
2133 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2135 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2139 sealed class GenericTypeInstance : Type
2141 private readonly Type type;
2142 private readonly Type[] args;
2143 private readonly Type[][] requiredCustomModifiers;
2144 private readonly Type[][] optionalCustomModifiers;
2145 private Type baseType;
2148 internal static Type Make(Type type, Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2150 bool identity = true;
2151 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2153 // a TypeBuiler identity must be instantiated
2158 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2159 for (int i = 0; i < typeArguments.Length; i++)
2161 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2162 || !IsEmpty(requiredCustomModifiers, i)
2163 || !IsEmpty(optionalCustomModifiers, i))
2176 return type.Module.CanonicalizeType(new GenericTypeInstance(type, typeArguments, requiredCustomModifiers, optionalCustomModifiers));
2180 private static bool IsEmpty(Type[][] mods, int i)
2182 // we need to be extra careful, because mods doesn't not need to be in canonical format
2183 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2184 return mods == null || mods[i] == null || mods[i].Length == 0;
2187 private GenericTypeInstance(Type type, Type[] args, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2191 this.requiredCustomModifiers = requiredCustomModifiers;
2192 this.optionalCustomModifiers = optionalCustomModifiers;
2195 public override bool Equals(object o)
2197 GenericTypeInstance gt = o as GenericTypeInstance;
2198 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2199 && Util.ArrayEquals(gt.requiredCustomModifiers, requiredCustomModifiers)
2200 && Util.ArrayEquals(gt.optionalCustomModifiers, optionalCustomModifiers);
2203 public override int GetHashCode()
2205 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2208 public override string AssemblyQualifiedName
2212 string fn = FullName;
2213 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2217 public override Type BaseType
2221 if (baseType == null)
2223 Type rawBaseType = type.BaseType;
2224 if (rawBaseType == null)
2226 baseType = rawBaseType;
2230 baseType = rawBaseType.BindTypeParameters(this);
2237 public override bool IsValueType
2239 get { return type.IsValueType; }
2242 public override bool IsVisible
2248 foreach (Type arg in args)
2261 public override Type DeclaringType
2263 get { return type.DeclaringType; }
2266 public override TypeAttributes Attributes
2268 get { return type.Attributes; }
2271 internal override void CheckBaked()
2276 public override FieldInfo[] __GetDeclaredFields()
2278 FieldInfo[] fields = type.__GetDeclaredFields();
2279 for (int i = 0; i < fields.Length; i++)
2281 fields[i] = fields[i].BindTypeParameters(this);
2286 public override Type[] __GetDeclaredInterfaces()
2288 Type[] interfaces = type.__GetDeclaredInterfaces();
2289 for (int i = 0; i < interfaces.Length; i++)
2291 interfaces[i] = interfaces[i].BindTypeParameters(this);
2296 public override MethodBase[] __GetDeclaredMethods()
2298 MethodBase[] methods = type.__GetDeclaredMethods();
2299 for (int i = 0; i < methods.Length; i++)
2301 methods[i] = methods[i].BindTypeParameters(this);
2306 public override Type[] __GetDeclaredTypes()
2308 return type.__GetDeclaredTypes();
2311 public override EventInfo[] __GetDeclaredEvents()
2313 EventInfo[] events = type.__GetDeclaredEvents();
2314 for (int i = 0; i < events.Length; i++)
2316 events[i] = events[i].BindTypeParameters(this);
2321 public override PropertyInfo[] __GetDeclaredProperties()
2323 PropertyInfo[] properties = type.__GetDeclaredProperties();
2324 for (int i = 0; i < properties.Length; i++)
2326 properties[i] = properties[i].BindTypeParameters(this);
2331 public override __MethodImplMap __GetMethodImplMap()
2333 __MethodImplMap map = type.__GetMethodImplMap();
2334 map.TargetType = this;
2335 for (int i = 0; i < map.MethodBodies.Length; i++)
2337 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2338 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2340 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2341 if (interfaceType.IsGenericType)
2343 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2350 public override string Namespace
2352 get { return type.Namespace; }
2355 public override Type UnderlyingSystemType
2357 get { return this; }
2360 public override string Name
2362 get { return type.Name; }
2365 public override string FullName
2369 if (this.ContainsGenericParameters)
2373 StringBuilder sb = new StringBuilder(this.type.FullName);
2375 foreach (Type type in args)
2377 sb.Append('[').Append(type.AssemblyQualifiedName.Replace("]", "\\]")).Append(']');
2380 return sb.ToString();
2384 public override string ToString()
2386 StringBuilder sb = new StringBuilder(type.FullName);
2389 foreach (Type arg in args)
2396 return sb.ToString();
2399 public override Module Module
2401 get { return type.Module; }
2404 public override bool IsGenericType
2406 get { return true; }
2409 public override Type GetGenericTypeDefinition()
2414 public override Type[] GetGenericArguments()
2416 return Util.Copy(args);
2419 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
2421 return Util.Copy(requiredCustomModifiers ?? new Type[args.Length][]);
2424 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
2426 return Util.Copy(optionalCustomModifiers ?? new Type[args.Length][]);
2429 internal override Type GetGenericTypeArgument(int index)
2434 public override bool ContainsGenericParameters
2438 foreach (Type type in args)
2440 if (type.ContainsGenericParameters)
2449 public override bool __ContainsMissingType
2453 foreach (Type type in args)
2455 if (type.__ContainsMissingType)
2460 return this.type.__IsMissing;
2464 public override StructLayoutAttribute StructLayoutAttribute
2466 get { return type.StructLayoutAttribute; }
2469 internal override int GetModuleBuilderToken()
2473 token = ((ModuleBuilder)type.Module).ImportType(this);
2478 internal override Type BindTypeParameters(IGenericBinder binder)
2480 for (int i = 0; i < args.Length; i++)
2482 Type xarg = args[i].BindTypeParameters(binder);
2483 if (!ReferenceEquals(xarg, args[i]))
2485 Type[] xargs = new Type[args.Length];
2486 Array.Copy(args, xargs, i);
2488 for (; i < args.Length; i++)
2490 xargs[i] = args[i].BindTypeParameters(binder);
2492 return Make(type, xargs, null, null);
2498 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2500 return type.GetCustomAttributesData(attributeType);