2 Copyright (C) 2009-2011 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Runtime.InteropServices;
27 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
30 namespace IKVM.Reflection
32 interface IGenericContext
34 Type GetGenericTypeArgument(int index);
35 Type GetGenericMethodArgument(int index);
38 interface IGenericBinder
40 Type BindTypeParameter(Type type);
41 Type BindMethodParameter(Type type);
44 public abstract class Type : MemberInfo, IGenericContext, IGenericBinder
46 public static readonly Type[] EmptyTypes = Empty<Type>.Array;
47 protected readonly Type underlyingType;
49 // prevent subclassing by outsiders
52 this.underlyingType = this;
55 internal Type(Type underlyingType)
57 this.underlyingType = underlyingType;
60 public static Binder DefaultBinder
65 public sealed override MemberTypes MemberType
67 get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
70 public virtual string AssemblyQualifiedName
72 // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
73 get { return this.FullName + ", " + this.Assembly.FullName; }
76 public abstract Type BaseType
81 public abstract TypeAttributes Attributes
86 public virtual Type GetElementType()
91 internal virtual void CheckBaked()
95 public virtual Type[] __GetDeclaredTypes()
97 return Type.EmptyTypes;
100 public virtual Type[] __GetDeclaredInterfaces()
102 return Type.EmptyTypes;
105 public virtual MethodBase[] __GetDeclaredMethods()
107 return Empty<MethodBase>.Array;
110 public virtual __MethodImplMap __GetMethodImplMap()
112 throw new NotSupportedException();
115 public virtual FieldInfo[] __GetDeclaredFields()
117 return Empty<FieldInfo>.Array;
120 public virtual EventInfo[] __GetDeclaredEvents()
122 return Empty<EventInfo>.Array;
125 public virtual PropertyInfo[] __GetDeclaredProperties()
127 return Empty<PropertyInfo>.Array;
130 public virtual Type[] __GetRequiredCustomModifiers()
132 return Type.EmptyTypes;
135 public virtual Type[] __GetOptionalCustomModifiers()
137 return Type.EmptyTypes;
140 public virtual bool HasElementType
142 get { return false; }
145 public virtual bool IsArray
147 get { return false; }
150 public virtual bool __IsVector
152 get { return false; }
155 public virtual bool IsByRef
157 get { return false; }
160 public virtual bool IsPointer
162 get { return false; }
165 public virtual bool IsValueType
169 Type baseType = this.BaseType;
170 return baseType == this.Module.universe.System_Enum
171 || (baseType == this.Module.universe.System_ValueType && this != this.Module.universe.System_Enum);
175 public virtual bool IsGenericParameter
177 get { return false; }
180 public virtual int GenericParameterPosition
182 get { throw new NotSupportedException(); }
185 public virtual MethodBase DeclaringMethod
190 public Type UnderlyingSystemType
192 get { return underlyingType; }
195 public override Type DeclaringType
200 public virtual string __Name
202 get { throw new InvalidOperationException(); }
205 public virtual string __Namespace
207 get { throw new InvalidOperationException(); }
210 public abstract override string Name
215 public virtual string Namespace
221 return DeclaringType.Namespace;
227 internal virtual int GetModuleBuilderToken()
229 throw new InvalidOperationException();
232 public static bool operator ==(Type t1, Type t2)
234 // Casting to object results in smaller code than calling ReferenceEquals and makes
235 // this method more likely to be inlined.
236 // On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
237 return (object)t1 == (object)t2
238 || ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
241 public static bool operator !=(Type t1, Type t2)
246 public bool Equals(Type type)
251 public override bool Equals(object obj)
253 return Equals(obj as Type);
256 public override int GetHashCode()
258 Type type = this.UnderlyingSystemType;
259 return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
262 public virtual Type[] GetGenericArguments()
264 return Type.EmptyTypes;
267 public virtual Type[][] __GetGenericArgumentsRequiredCustomModifiers()
269 return Empty<Type[]>.Array;
272 public virtual Type[][] __GetGenericArgumentsOptionalCustomModifiers()
274 return Empty<Type[]>.Array;
277 public virtual Type GetGenericTypeDefinition()
279 throw new InvalidOperationException();
282 public virtual StructLayoutAttribute StructLayoutAttribute
287 public virtual bool IsGenericType
289 get { return false; }
292 public virtual bool IsGenericTypeDefinition
294 get { return false; }
297 public virtual bool ContainsGenericParameters
301 if (this.IsGenericParameter)
305 foreach (Type arg in this.GetGenericArguments())
307 if (arg.ContainsGenericParameters)
316 public virtual Type[] GetGenericParameterConstraints()
318 throw new InvalidOperationException();
321 public virtual GenericParameterAttributes GenericParameterAttributes
323 get { throw new InvalidOperationException(); }
326 public virtual int GetArrayRank()
328 throw new NotSupportedException();
332 public virtual Type GetEnumUnderlyingType()
336 throw new ArgumentException();
339 return GetEnumUnderlyingTypeImpl();
342 internal Type GetEnumUnderlyingTypeImpl()
344 foreach (FieldInfo field in __GetDeclaredFields())
348 // the CLR assumes that an enum has only one instance field, so we can do the same
349 return field.FieldType;
352 throw new InvalidOperationException();
355 public override string ToString()
360 public abstract string FullName
365 protected string GetFullName()
367 string ns = TypeNameParser.Escape(this.__Namespace);
368 Type decl = this.DeclaringType;
377 return ns + "." + this.Name;
384 return decl.FullName + "+" + this.Name;
388 return decl.FullName + "+" + ns + "." + this.Name;
393 internal virtual bool IsModulePseudoType
395 get { return false; }
398 internal virtual Type GetGenericTypeArgument(int index)
400 throw new InvalidOperationException();
403 public MemberInfo[] GetDefaultMembers()
405 Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
406 foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
408 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
410 return GetMember((string)cad.ConstructorArguments[0].Value);
413 return Empty<MemberInfo>.Array;
416 public MemberInfo[] GetMember(string name)
418 return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
421 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
423 return GetMember(name, MemberTypes.All, bindingAttr);
426 public MemberInfo[] GetMembers()
428 return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
431 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
433 List<MemberInfo> members = new List<MemberInfo>();
434 members.AddRange(GetConstructors(bindingAttr));
435 members.AddRange(GetMethods(bindingAttr));
436 members.AddRange(GetFields(bindingAttr));
437 members.AddRange(GetProperties(bindingAttr));
438 members.AddRange(GetEvents(bindingAttr));
439 members.AddRange(GetNestedTypes(bindingAttr));
440 return members.ToArray();
443 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
445 MemberFilter filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
446 return FindMembers(type, bindingAttr, filter, name);
449 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
451 foreach (MemberInfo member in members)
453 if (filter == null || filter(member, filterCriteria))
460 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
462 List<MemberInfo> members = new List<MemberInfo>();
463 if ((memberType & MemberTypes.Constructor) != 0)
465 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
467 if ((memberType & MemberTypes.Method) != 0)
469 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
471 if ((memberType & MemberTypes.Field) != 0)
473 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
475 if ((memberType & MemberTypes.Property) != 0)
477 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
479 if ((memberType & MemberTypes.Event) != 0)
481 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
483 if ((memberType & MemberTypes.NestedType) != 0)
485 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
487 return members.ToArray();
490 public EventInfo GetEvent(string name)
492 return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
495 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
497 foreach (EventInfo evt in GetEvents(bindingAttr))
499 if (evt.Name == name)
507 public EventInfo[] GetEvents()
509 return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
512 public EventInfo[] GetEvents(BindingFlags bindingAttr)
514 List<EventInfo> list = new List<EventInfo>();
519 foreach (EventInfo evt in type.__GetDeclaredEvents())
521 if (BindingFlagsMatch(evt.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
522 && BindingFlagsMatch(evt.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
527 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
529 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
531 bindingAttr &= ~BindingFlags.Static;
533 type = type.BaseType;
540 return list.ToArray();
543 public FieldInfo GetField(string name)
545 return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
548 public FieldInfo GetField(string name, BindingFlags bindingAttr)
550 foreach (FieldInfo field in GetFields(bindingAttr))
552 if (field.Name == name)
560 public FieldInfo[] GetFields()
562 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
565 public FieldInfo[] GetFields(BindingFlags bindingAttr)
567 List<FieldInfo> list = new List<FieldInfo>();
569 foreach (FieldInfo field in __GetDeclaredFields())
571 if (BindingFlagsMatch(field.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
572 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
577 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
579 for (Type type = this.BaseType; type != null; type = type.BaseType)
582 foreach (FieldInfo field in type.__GetDeclaredFields())
584 if ((field.Attributes & FieldAttributes.FieldAccessMask) > FieldAttributes.Private
585 && BindingFlagsMatch(field.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance))
592 return list.ToArray();
595 public Type[] GetInterfaces()
597 List<Type> list = new List<Type>();
598 for (Type type = this; type != null; type = type.BaseType)
600 AddInterfaces(list, type);
602 return list.ToArray();
605 private static void AddInterfaces(List<Type> list, Type type)
608 foreach (Type iface in type.__GetDeclaredInterfaces())
610 if (!list.Contains(iface))
613 AddInterfaces(list, iface);
618 public MethodInfo[] GetMethods(BindingFlags bindingAttr)
621 List<MethodInfo> list = new List<MethodInfo>();
622 foreach (MethodBase mb in __GetDeclaredMethods())
624 MethodInfo mi = mb as MethodInfo;
626 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
627 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
632 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
634 for (Type type = this.BaseType; type != null; type = type.BaseType)
637 foreach (MethodBase mb in type.__GetDeclaredMethods())
639 MethodInfo mi = mb as MethodInfo;
641 && (mi.Attributes & MethodAttributes.MemberAccessMask) > MethodAttributes.Private
642 && BindingFlagsMatch(mi.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
643 && BindingFlagsMatch(mi.IsStatic, bindingAttr, BindingFlags.Static | BindingFlags.FlattenHierarchy, BindingFlags.Instance)
644 && !FindMethod(list, mi))
651 return list.ToArray();
654 private static bool FindMethod(List<MethodInfo> methods, MethodInfo method)
656 foreach (MethodInfo m in methods)
658 if (m.Name == method.Name && m.MethodSignature.Equals(method.MethodSignature))
666 public MethodInfo[] GetMethods()
668 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
671 public MethodInfo GetMethod(string name)
673 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
676 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
678 MethodInfo found = null;
679 foreach (MethodInfo method in GetMethods(bindingAttr))
681 if (method.Name == name)
685 throw new AmbiguousMatchException();
693 public MethodInfo GetMethod(string name, Type[] types)
695 return GetMethod(name, types, null);
698 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
700 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
703 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
705 MethodInfo found = null;
706 foreach (MethodInfo method in GetMethods(bindingAttr))
708 if (method.Name == name && method.MethodSignature.MatchParameterTypes(types))
712 throw new AmbiguousMatchException();
720 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
722 // FXBUG callConvention seems to be ignored
723 return GetMethod(name, bindingAttr, binder, types, modifiers);
726 public ConstructorInfo[] GetConstructors()
728 return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
731 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
734 List<ConstructorInfo> list = new List<ConstructorInfo>();
735 foreach (MethodBase mb in __GetDeclaredMethods())
737 ConstructorInfo constructor = mb as ConstructorInfo;
738 if (constructor != null
739 && BindingFlagsMatch(constructor.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
740 && BindingFlagsMatch(constructor.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
742 list.Add(constructor);
745 return list.ToArray();
748 public ConstructorInfo GetConstructor(Type[] types)
750 return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
753 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
755 foreach (ConstructorInfo constructor in GetConstructors(bindingAttr))
757 if (constructor.MethodSignature.MatchParameterTypes(types))
765 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
767 // FXBUG callConvention seems to be ignored
768 return GetConstructor(bindingAttr, binder, types, modifiers);
771 internal Type ResolveNestedType(TypeName typeName)
773 return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
776 // unlike the public API, this takes the namespace and name into account
777 internal virtual Type FindNestedType(TypeName name)
779 foreach (Type type in __GetDeclaredTypes())
781 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
789 public Type GetNestedType(string name)
791 return GetNestedType(name, BindingFlags.Public);
794 public Type GetNestedType(string name, BindingFlags bindingAttr)
796 foreach (Type type in GetNestedTypes(bindingAttr))
798 // FXBUG the namespace is ignored
799 if (type.__Name == name)
807 public Type[] GetNestedTypes()
809 return GetNestedTypes(BindingFlags.Public);
812 public Type[] GetNestedTypes(BindingFlags bindingAttr)
815 List<Type> list = new List<Type>();
816 foreach (Type type in __GetDeclaredTypes())
818 if (BindingFlagsMatch(type.IsNestedPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic))
823 return list.ToArray();
826 public PropertyInfo[] GetProperties()
828 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
831 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
833 List<PropertyInfo> list = new List<PropertyInfo>();
838 foreach (PropertyInfo property in type.__GetDeclaredProperties())
840 if (BindingFlagsMatch(property.IsPublic, bindingAttr, BindingFlags.Public, BindingFlags.NonPublic)
841 && BindingFlagsMatch(property.IsStatic, bindingAttr, BindingFlags.Static, BindingFlags.Instance))
846 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
848 if ((bindingAttr & BindingFlags.FlattenHierarchy) == 0)
850 bindingAttr &= ~BindingFlags.Static;
852 type = type.BaseType;
859 return list.ToArray();
862 public PropertyInfo GetProperty(string name)
864 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
867 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
869 foreach (PropertyInfo prop in GetProperties(bindingAttr))
871 if (prop.Name == name)
879 public PropertyInfo GetProperty(string name, Type returnType)
881 PropertyInfo found = null;
882 foreach (PropertyInfo prop in GetProperties())
884 if (prop.Name == name && prop.PropertyType.Equals(returnType))
888 throw new AmbiguousMatchException();
896 public PropertyInfo GetProperty(string name, Type[] types)
898 PropertyInfo found = null;
899 foreach (PropertyInfo prop in GetProperties())
901 if (prop.Name == name && MatchParameterTypes(prop.GetIndexParameters(), types))
905 throw new AmbiguousMatchException();
913 private static bool MatchParameterTypes(ParameterInfo[] parameters, Type[] types)
915 if (parameters.Length == types.Length)
917 for (int i = 0; i < parameters.Length; i++)
919 if (!parameters[i].ParameterType.Equals(types[i]))
929 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
931 return GetProperty(name, returnType, types, null);
934 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
936 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
939 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
941 PropertyInfo found = null;
942 foreach (PropertyInfo prop in GetProperties(bindingAttr))
944 if (prop.Name == name && prop.PropertyType.Equals(returnType) && MatchParameterTypes(prop.GetIndexParameters(), types))
948 throw new AmbiguousMatchException();
956 public Type GetInterface(string name)
958 return GetInterface(name, false);
961 public Type GetInterface(string name, bool ignoreCase)
965 throw new NotImplementedException();
967 foreach (Type type in GetInterfaces())
969 if (type.FullName == name)
977 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
979 List<Type> list = new List<Type>();
980 foreach (Type type in GetInterfaces())
982 if (filter(type, filterCriteria))
987 return list.ToArray();
990 public ConstructorInfo TypeInitializer
992 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
995 public bool IsPrimitive
999 Universe u = this.Module.universe;
1000 return this == u.System_Boolean
1001 || this == u.System_Byte
1002 || this == u.System_SByte
1003 || this == u.System_Int16
1004 || this == u.System_UInt16
1005 || this == u.System_Int32
1006 || this == u.System_UInt32
1007 || this == u.System_Int64
1008 || this == u.System_UInt64
1009 || this == u.System_IntPtr
1010 || this == u.System_UIntPtr
1011 || this == u.System_Char
1012 || this == u.System_Double
1013 || this == u.System_Single
1020 get { return this.BaseType == this.Module.universe.System_Enum; }
1023 public bool IsSealed
1025 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1028 public bool IsAbstract
1030 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1033 private bool CheckVisibility(TypeAttributes access)
1035 return (Attributes & TypeAttributes.VisibilityMask) == access;
1038 public bool IsPublic
1040 get { return CheckVisibility(TypeAttributes.Public); }
1043 public bool IsNestedPublic
1045 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1048 public bool IsNestedPrivate
1050 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1053 public bool IsNestedFamily
1055 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1058 public bool IsNestedAssembly
1060 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1063 public bool IsNestedFamANDAssem
1065 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1068 public bool IsNestedFamORAssem
1070 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1073 public bool IsNotPublic
1075 get { return CheckVisibility(TypeAttributes.NotPublic); }
1078 public bool IsImport
1080 get { return (Attributes & TypeAttributes.Import) != 0; }
1083 public bool IsCOMObject
1085 get { return IsClass && IsImport; }
1088 public bool IsContextful
1090 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1093 public bool IsMarshalByRef
1095 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1098 public virtual bool IsVisible
1100 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1103 public bool IsAnsiClass
1105 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1108 public bool IsUnicodeClass
1110 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1113 public bool IsAutoClass
1115 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1118 public bool IsAutoLayout
1120 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1123 public bool IsLayoutSequential
1125 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1128 public bool IsExplicitLayout
1130 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1133 public bool IsSpecialName
1135 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1138 public bool IsSerializable
1140 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1145 get { return !IsInterface && !IsValueType; }
1148 public bool IsInterface
1150 get { return (Attributes & TypeAttributes.Interface) != 0; }
1153 public bool IsNested
1155 // FXBUG we check the declaring type (like .NET) and this results
1156 // in IsNested returning true for a generic type parameter
1157 get { return this.DeclaringType != null; }
1160 public virtual bool __ContainsMissingType
1164 if (this.__IsMissing)
1168 foreach (Type arg in this.GetGenericArguments())
1170 if (arg.__ContainsMissingType)
1179 public Type MakeArrayType()
1181 return ArrayType.Make(this, null, null);
1184 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1186 return ArrayType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1189 public Type MakeArrayType(int rank)
1191 return MultiArrayType.Make(this, rank, null, null);
1194 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1196 return MultiArrayType.Make(this, rank, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1199 public Type MakeByRefType()
1201 return ByRefType.Make(this, null, null);
1204 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1206 return ByRefType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1209 public Type MakePointerType()
1211 return PointerType.Make(this, null, null);
1214 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1216 return PointerType.Make(this, Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1219 public Type MakeGenericType(params Type[] typeArguments)
1221 return __MakeGenericType(typeArguments, null, null);
1224 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1226 if (!this.IsGenericTypeDefinition)
1228 throw new InvalidOperationException();
1230 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), Util.Copy(requiredCustomModifiers), Util.Copy(optionalCustomModifiers));
1233 public static System.Type __GetSystemType(TypeCode typeCode)
1237 case TypeCode.Boolean:
1238 return typeof(System.Boolean);
1240 return typeof(System.Byte);
1242 return typeof(System.Char);
1243 case TypeCode.DBNull:
1244 return typeof(System.DBNull);
1245 case TypeCode.DateTime:
1246 return typeof(System.DateTime);
1247 case TypeCode.Decimal:
1248 return typeof(System.Decimal);
1249 case TypeCode.Double:
1250 return typeof(System.Double);
1251 case TypeCode.Empty:
1253 case TypeCode.Int16:
1254 return typeof(System.Int16);
1255 case TypeCode.Int32:
1256 return typeof(System.Int32);
1257 case TypeCode.Int64:
1258 return typeof(System.Int64);
1259 case TypeCode.Object:
1260 return typeof(System.Object);
1261 case TypeCode.SByte:
1262 return typeof(System.SByte);
1263 case TypeCode.Single:
1264 return typeof(System.Single);
1265 case TypeCode.String:
1266 return typeof(System.String);
1267 case TypeCode.UInt16:
1268 return typeof(System.UInt16);
1269 case TypeCode.UInt32:
1270 return typeof(System.UInt32);
1271 case TypeCode.UInt64:
1272 return typeof(System.UInt64);
1274 throw new ArgumentOutOfRangeException();
1278 public static TypeCode GetTypeCode(Type type)
1282 return TypeCode.Empty;
1286 type = type.GetEnumUnderlyingType();
1288 Universe u = type.Module.universe;
1289 if (type == u.System_Boolean)
1291 return TypeCode.Boolean;
1293 else if (type == u.System_Char)
1295 return TypeCode.Char;
1297 else if (type == u.System_SByte)
1299 return TypeCode.SByte;
1301 else if (type == u.System_Byte)
1303 return TypeCode.Byte;
1305 else if (type == u.System_Int16)
1307 return TypeCode.Int16;
1309 else if (type == u.System_UInt16)
1311 return TypeCode.UInt16;
1313 else if (type == u.System_Int32)
1315 return TypeCode.Int32;
1317 else if (type == u.System_UInt32)
1319 return TypeCode.UInt32;
1321 else if (type == u.System_Int64)
1323 return TypeCode.Int64;
1325 else if (type == u.System_UInt64)
1327 return TypeCode.UInt64;
1329 else if (type == u.System_Single)
1331 return TypeCode.Single;
1333 else if (type == u.System_Double)
1335 return TypeCode.Double;
1337 else if (type == u.System_DateTime)
1339 return TypeCode.DateTime;
1341 else if (type == u.System_DBNull)
1343 return TypeCode.DBNull;
1345 else if (type == u.System_Decimal)
1347 return TypeCode.Decimal;
1349 else if (type == u.System_String)
1351 return TypeCode.String;
1355 return TypeCode.Object;
1359 public Assembly Assembly
1361 get { return Module.Assembly; }
1364 // note that interface/delegate co- and contravariance is not considered
1365 public bool IsAssignableFrom(Type type)
1367 if (this.Equals(type))
1371 else if (type == null)
1375 else if (this.IsArray && type.IsArray)
1377 if (this.GetArrayRank() != type.GetArrayRank())
1381 else if (this.__IsVector && !type.__IsVector)
1385 Type e1 = this.GetElementType();
1386 Type e2 = type.GetElementType();
1387 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1389 else if (this.IsSealed)
1393 else if (this.IsInterface)
1395 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1397 else if (type.IsInterface)
1399 return this == this.Module.universe.System_Object;
1401 else if (type.IsPointer)
1403 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1407 return type.IsSubclassOf(this);
1411 public bool IsSubclassOf(Type type)
1413 Type thisType = this.BaseType;
1414 while (thisType != null)
1416 if (thisType.Equals(type))
1420 thisType = thisType.BaseType;
1425 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1426 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1427 private bool IsDirectlyImplementedInterface(Type interfaceType)
1429 foreach (Type iface in __GetDeclaredInterfaces())
1431 if (interfaceType.IsAssignableFrom(iface))
1439 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1442 InterfaceMapping map = new InterfaceMapping();
1443 if (!IsDirectlyImplementedInterface(interfaceType))
1445 Type baseType = this.BaseType;
1446 if (baseType == null)
1448 throw new ArgumentException();
1452 map = baseType.GetInterfaceMap(interfaceType);
1457 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1458 map.InterfaceType = interfaceType;
1459 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1460 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1461 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1462 for (int i = 0; i < map.TargetMethods.Length; i++)
1464 if (map.TargetMethods[i] == null)
1466 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1467 for (int j = 0; j < methods.Length; j++)
1469 if (methods[j].Name == map.InterfaceMethods[i].Name
1470 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1472 map.TargetMethods[i] = methods[j];
1477 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1479 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1482 map.TargetType = this;
1486 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1488 __MethodImplMap impl = __GetMethodImplMap();
1489 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1491 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1493 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1494 if (index != -1 && targetMethods[index] == null)
1496 targetMethods[index] = impl.MethodBodies[i];
1502 Type IGenericContext.GetGenericTypeArgument(int index)
1504 return GetGenericTypeArgument(index);
1507 Type IGenericContext.GetGenericMethodArgument(int index)
1509 throw new BadImageFormatException();
1512 Type IGenericBinder.BindTypeParameter(Type type)
1514 return GetGenericTypeArgument(type.GenericParameterPosition);
1517 Type IGenericBinder.BindMethodParameter(Type type)
1519 throw new BadImageFormatException();
1522 internal virtual Type BindTypeParameters(IGenericBinder binder)
1524 if (IsGenericTypeDefinition)
1526 Type[] args = GetGenericArguments();
1527 Type.InplaceBindTypeParameters(binder, args);
1528 return GenericTypeInstance.Make(this, args, null, null);
1536 internal static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1538 for (int i = 0; i < types.Length; i++)
1540 types[i] = types[i].BindTypeParameters(binder);
1544 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1546 foreach (MethodBase method in __GetDeclaredMethods())
1548 if (method.Name == name && method.MethodSignature.Equals(signature))
1556 internal FieldInfo FindField(string name, FieldSignature signature)
1558 foreach (FieldInfo field in __GetDeclaredFields())
1560 if (field.Name == name && field.FieldSignature.Equals(signature))
1568 internal bool IsAllowMultipleCustomAttribute
1572 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1575 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1577 if (arg.MemberInfo.Name == "AllowMultiple")
1579 return (bool)arg.TypedValue.Value;
1587 internal bool IsPseudoCustomAttribute
1591 Universe u = this.Module.universe;
1592 return this == u.System_NonSerializedAttribute
1593 || this == u.System_SerializableAttribute
1594 || this == u.System_Runtime_InteropServices_DllImportAttribute
1595 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1596 || this == u.System_Runtime_InteropServices_InAttribute
1597 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1598 || this == u.System_Runtime_InteropServices_OutAttribute
1599 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1600 || this == u.System_Runtime_InteropServices_OptionalAttribute
1601 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1602 || this == u.System_Runtime_InteropServices_ComImportAttribute
1603 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1604 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1610 abstract class ElementHolderType : Type
1612 protected readonly Type elementType;
1614 private readonly Type[] requiredCustomModifiers;
1615 private readonly Type[] optionalCustomModifiers;
1617 protected ElementHolderType(Type elementType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1619 this.elementType = elementType;
1620 this.requiredCustomModifiers = requiredCustomModifiers;
1621 this.optionalCustomModifiers = optionalCustomModifiers;
1624 protected bool EqualsHelper(ElementHolderType other)
1626 return other != null
1627 && other.elementType.Equals(elementType)
1628 && Util.ArrayEquals(other.requiredCustomModifiers, requiredCustomModifiers)
1629 && Util.ArrayEquals(other.optionalCustomModifiers, optionalCustomModifiers);
1632 public override Type[] __GetRequiredCustomModifiers()
1634 return Util.Copy(requiredCustomModifiers);
1637 public override Type[] __GetOptionalCustomModifiers()
1639 return Util.Copy(optionalCustomModifiers);
1642 public sealed override string Name
1644 get { return elementType.Name + GetSuffix(); }
1647 public sealed override string Namespace
1649 get { return elementType.Namespace; }
1652 public sealed override string FullName
1654 get { return elementType.FullName + GetSuffix(); }
1657 public sealed override string ToString()
1659 return elementType.ToString() + GetSuffix();
1662 public sealed override Type GetElementType()
1667 public sealed override bool HasElementType
1669 get { return true; }
1672 public sealed override Module Module
1674 get { return elementType.Module; }
1677 internal sealed override int GetModuleBuilderToken()
1681 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1686 public sealed override bool ContainsGenericParameters
1690 Type type = elementType;
1691 while (type.HasElementType)
1693 type = type.GetElementType();
1695 return type.ContainsGenericParameters;
1699 public sealed override bool __ContainsMissingType
1703 Type type = elementType;
1704 while (type.HasElementType)
1706 type = type.GetElementType();
1708 return type.__ContainsMissingType;
1712 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1714 Type type = elementType.BindTypeParameters(binder);
1715 Type[] req = BindArray(requiredCustomModifiers, binder);
1716 Type[] opt = BindArray(optionalCustomModifiers, binder);
1717 if (ReferenceEquals(type, elementType)
1718 && ReferenceEquals(req, requiredCustomModifiers)
1719 && ReferenceEquals(opt, optionalCustomModifiers))
1723 return Wrap(type, req, opt);
1726 internal override void CheckBaked()
1728 elementType.CheckBaked();
1731 private static Type[] BindArray(Type[] array, IGenericBinder binder)
1733 if (array ==null || array.Length == 0)
1737 Type[] result = array;
1738 for (int i = 0; i < array.Length; i++)
1740 Type type = array[i].BindTypeParameters(binder);
1741 if (!ReferenceEquals(type, array[i]))
1743 if (result == array)
1745 result = (Type[])array.Clone();
1753 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1755 return CustomAttributeData.EmptyList;
1758 protected abstract string GetSuffix();
1760 protected abstract Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers);
1763 sealed class ArrayType : ElementHolderType
1765 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1767 return type.Module.CanonicalizeType(new ArrayType(type, requiredCustomModifiers, optionalCustomModifiers));
1770 private ArrayType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1771 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1775 public override Type BaseType
1777 get { return elementType.Module.universe.System_Array; }
1780 public override Type[] __GetDeclaredInterfaces()
1783 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
1784 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
1785 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
1789 public override MethodBase[] __GetDeclaredMethods()
1791 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
1792 List<MethodBase> list = new List<MethodBase>();
1793 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 }));
1794 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
1795 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
1796 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1797 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
1799 Array.Resize(ref int32, int32.Length + 1);
1800 int32[int32.Length - 1] = int32[0];
1801 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
1803 return list.ToArray();
1806 public override TypeAttributes Attributes
1808 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1811 public override bool IsArray
1813 get { return true; }
1816 public override bool __IsVector
1818 get { return true; }
1821 public override int GetArrayRank()
1826 public override bool Equals(object o)
1828 return EqualsHelper(o as ArrayType);
1831 public override int GetHashCode()
1833 return elementType.GetHashCode() * 5;
1836 protected override string GetSuffix()
1841 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1843 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
1847 sealed class MultiArrayType : ElementHolderType
1849 private readonly int rank;
1851 internal static Type Make(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1853 return type.Module.CanonicalizeType(new MultiArrayType(type, rank, requiredCustomModifiers, optionalCustomModifiers));
1856 private MultiArrayType(Type type, int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1857 : base(type, requiredCustomModifiers, optionalCustomModifiers)
1862 public override Type BaseType
1864 get { return elementType.Module.universe.System_Array; }
1867 public override MethodBase[] __GetDeclaredMethods()
1869 Type int32 = this.Module.universe.System_Int32;
1870 Type[] setArgs = new Type[rank + 1];
1871 Type[] getArgs = new Type[rank];
1872 Type[] ctorArgs = new Type[rank * 2];
1873 for (int i = 0; i < rank; i++)
1877 ctorArgs[i * 2 + 0] = int32;
1878 ctorArgs[i * 2 + 1] = int32;
1880 setArgs[rank] = elementType;
1881 return new MethodBase[] {
1882 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
1883 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
1884 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
1885 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
1886 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
1890 public override TypeAttributes Attributes
1892 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
1895 public override bool IsArray
1897 get { return true; }
1900 public override int GetArrayRank()
1905 public override bool Equals(object o)
1907 MultiArrayType at = o as MultiArrayType;
1908 return EqualsHelper(at) && at.rank == rank;
1911 public override int GetHashCode()
1913 return elementType.GetHashCode() * 9 + rank;
1916 protected override string GetSuffix()
1924 return "[" + new String(',', rank - 1) + "]";
1928 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1930 return Make(type, rank, requiredCustomModifiers, optionalCustomModifiers);
1934 sealed class BuiltinArrayMethod : ArrayMethod
1936 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
1937 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
1941 public override MethodAttributes Attributes
1943 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
1946 public override MethodImplAttributes GetMethodImplementationFlags()
1948 return MethodImplAttributes.IL;
1951 public override int MetadataToken
1953 get { return 0x06000000; }
1956 public override MethodBody GetMethodBody()
1961 public override ParameterInfo[] GetParameters()
1963 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
1964 for (int i = 0; i < parameterInfos.Length; i++)
1966 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
1968 return parameterInfos;
1971 public override ParameterInfo ReturnParameter
1973 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
1976 private sealed class ParameterInfoImpl : ParameterInfo
1978 private readonly MethodInfo method;
1979 private readonly Type type;
1980 private readonly int pos;
1982 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
1984 this.method = method;
1989 public override Type ParameterType
1991 get { return type; }
1994 public override string Name
1996 get { return null; }
1999 public override ParameterAttributes Attributes
2001 get { return ParameterAttributes.None; }
2004 public override int Position
2009 public override object RawDefaultValue
2011 get { return null; }
2014 public override Type[] GetOptionalCustomModifiers()
2016 return Empty<Type>.Array;
2019 public override Type[] GetRequiredCustomModifiers()
2021 return Empty<Type>.Array;
2024 public override MemberInfo Member
2026 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2029 public override int MetadataToken
2031 get { return 0x8000000; }
2034 internal override Module Module
2036 get { return method.Module; }
2041 sealed class ByRefType : ElementHolderType
2043 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2045 return type.Module.CanonicalizeType(new ByRefType(type, requiredCustomModifiers, optionalCustomModifiers));
2048 private ByRefType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2049 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2053 public override bool Equals(object o)
2055 return EqualsHelper(o as ByRefType);
2058 public override int GetHashCode()
2060 return elementType.GetHashCode() * 3;
2063 public override Type BaseType
2065 get { return null; }
2068 public override TypeAttributes Attributes
2073 public override bool IsByRef
2075 get { return true; }
2078 protected override string GetSuffix()
2083 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2085 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2089 sealed class PointerType : ElementHolderType
2091 internal static Type Make(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2093 return type.Module.CanonicalizeType(new PointerType(type, requiredCustomModifiers, optionalCustomModifiers));
2096 private PointerType(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2097 : base(type, requiredCustomModifiers, optionalCustomModifiers)
2101 public override bool Equals(object o)
2103 return EqualsHelper(o as PointerType);
2106 public override int GetHashCode()
2108 return elementType.GetHashCode() * 7;
2111 public override Type BaseType
2113 get { return null; }
2116 public override TypeAttributes Attributes
2121 public override bool IsPointer
2123 get { return true; }
2126 protected override string GetSuffix()
2131 protected override Type Wrap(Type type, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
2133 return Make(type, requiredCustomModifiers, optionalCustomModifiers);
2137 sealed class GenericTypeInstance : Type
2139 private readonly Type type;
2140 private readonly Type[] args;
2141 private readonly Type[][] requiredCustomModifiers;
2142 private readonly Type[][] optionalCustomModifiers;
2143 private Type baseType;
2146 internal static Type Make(Type type, Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2148 bool identity = true;
2149 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2151 // a TypeBuiler identity must be instantiated
2156 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2157 for (int i = 0; i < typeArguments.Length; i++)
2159 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2160 || !IsEmpty(requiredCustomModifiers, i)
2161 || !IsEmpty(optionalCustomModifiers, i))
2174 return type.Module.CanonicalizeType(new GenericTypeInstance(type, typeArguments, requiredCustomModifiers, optionalCustomModifiers));
2178 private static bool IsEmpty(Type[][] mods, int i)
2180 // we need to be extra careful, because mods doesn't not need to be in canonical format
2181 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2182 return mods == null || mods[i] == null || mods[i].Length == 0;
2185 private GenericTypeInstance(Type type, Type[] args, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
2189 this.requiredCustomModifiers = requiredCustomModifiers;
2190 this.optionalCustomModifiers = optionalCustomModifiers;
2193 public override bool Equals(object o)
2195 GenericTypeInstance gt = o as GenericTypeInstance;
2196 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2197 && Util.ArrayEquals(gt.requiredCustomModifiers, requiredCustomModifiers)
2198 && Util.ArrayEquals(gt.optionalCustomModifiers, optionalCustomModifiers);
2201 public override int GetHashCode()
2203 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2206 public override string AssemblyQualifiedName
2210 string fn = FullName;
2211 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2215 public override Type BaseType
2219 if (baseType == null)
2221 Type rawBaseType = type.BaseType;
2222 if (rawBaseType == null)
2224 baseType = rawBaseType;
2228 baseType = rawBaseType.BindTypeParameters(this);
2235 public override bool IsValueType
2237 get { return type.IsValueType; }
2240 public override bool IsVisible
2246 foreach (Type arg in args)
2259 public override Type DeclaringType
2261 get { return type.DeclaringType; }
2264 public override TypeAttributes Attributes
2266 get { return type.Attributes; }
2269 internal override void CheckBaked()
2274 public override FieldInfo[] __GetDeclaredFields()
2276 FieldInfo[] fields = type.__GetDeclaredFields();
2277 for (int i = 0; i < fields.Length; i++)
2279 fields[i] = fields[i].BindTypeParameters(this);
2284 public override Type[] __GetDeclaredInterfaces()
2286 Type[] interfaces = type.__GetDeclaredInterfaces();
2287 for (int i = 0; i < interfaces.Length; i++)
2289 interfaces[i] = interfaces[i].BindTypeParameters(this);
2294 public override MethodBase[] __GetDeclaredMethods()
2296 MethodBase[] methods = type.__GetDeclaredMethods();
2297 for (int i = 0; i < methods.Length; i++)
2299 methods[i] = methods[i].BindTypeParameters(this);
2304 public override Type[] __GetDeclaredTypes()
2306 return type.__GetDeclaredTypes();
2309 public override EventInfo[] __GetDeclaredEvents()
2311 EventInfo[] events = type.__GetDeclaredEvents();
2312 for (int i = 0; i < events.Length; i++)
2314 events[i] = events[i].BindTypeParameters(this);
2319 public override PropertyInfo[] __GetDeclaredProperties()
2321 PropertyInfo[] properties = type.__GetDeclaredProperties();
2322 for (int i = 0; i < properties.Length; i++)
2324 properties[i] = properties[i].BindTypeParameters(this);
2329 public override __MethodImplMap __GetMethodImplMap()
2331 __MethodImplMap map = type.__GetMethodImplMap();
2332 map.TargetType = this;
2333 for (int i = 0; i < map.MethodBodies.Length; i++)
2335 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2336 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2338 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2339 if (interfaceType.IsGenericType)
2341 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2348 public override string Namespace
2350 get { return type.Namespace; }
2353 public override string Name
2355 get { return type.Name; }
2358 public override string FullName
2362 if (this.ContainsGenericParameters)
2366 StringBuilder sb = new StringBuilder(this.type.FullName);
2368 foreach (Type type in args)
2370 sb.Append('[').Append(type.AssemblyQualifiedName.Replace("]", "\\]")).Append(']');
2373 return sb.ToString();
2377 public override string ToString()
2379 StringBuilder sb = new StringBuilder(type.FullName);
2382 foreach (Type arg in args)
2389 return sb.ToString();
2392 public override Module Module
2394 get { return type.Module; }
2397 public override bool IsGenericType
2399 get { return true; }
2402 public override Type GetGenericTypeDefinition()
2407 public override Type[] GetGenericArguments()
2409 return Util.Copy(args);
2412 public override Type[][] __GetGenericArgumentsRequiredCustomModifiers()
2414 return Util.Copy(requiredCustomModifiers ?? new Type[args.Length][]);
2417 public override Type[][] __GetGenericArgumentsOptionalCustomModifiers()
2419 return Util.Copy(optionalCustomModifiers ?? new Type[args.Length][]);
2422 internal override Type GetGenericTypeArgument(int index)
2427 public override bool ContainsGenericParameters
2431 foreach (Type type in args)
2433 if (type.ContainsGenericParameters)
2442 public override bool __ContainsMissingType
2446 foreach (Type type in args)
2448 if (type.__ContainsMissingType)
2453 return this.type.__IsMissing;
2457 public override StructLayoutAttribute StructLayoutAttribute
2459 get { return type.StructLayoutAttribute; }
2462 internal override int GetModuleBuilderToken()
2466 token = ((ModuleBuilder)type.Module).ImportType(this);
2471 internal override Type BindTypeParameters(IGenericBinder binder)
2473 for (int i = 0; i < args.Length; i++)
2475 Type xarg = args[i].BindTypeParameters(binder);
2476 if (!ReferenceEquals(xarg, args[i]))
2478 Type[] xargs = new Type[args.Length];
2479 Array.Copy(args, xargs, i);
2481 for (; i < args.Length; i++)
2483 xargs[i] = args[i].BindTypeParameters(binder);
2485 return Make(type, xargs, null, null);
2491 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2493 return type.GetCustomAttributesData(attributeType);