2 Copyright (C) 2009-2012 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Runtime.InteropServices;
27 using System.Collections.Generic;
28 using IKVM.Reflection.Emit;
30 namespace IKVM.Reflection
32 interface IGenericContext
34 Type GetGenericTypeArgument(int index);
35 Type GetGenericMethodArgument(int index);
38 interface IGenericBinder
40 Type BindTypeParameter(Type type);
41 Type BindMethodParameter(Type type);
44 public abstract class Type : MemberInfo, IGenericContext, IGenericBinder
46 public static readonly Type[] EmptyTypes = Empty<Type>.Array;
47 protected readonly Type underlyingType;
48 protected TypeFlags typeFlags;
51 protected enum TypeFlags
53 // for use by TypeBuilder
54 IsGenericTypeDefinition = 1,
58 // for use by MissingType
62 // for use by TypeDef, TypeBuilder or MissingType
63 PotentialEnumOrValueType = 32,
67 // prevent subclassing by outsiders
70 this.underlyingType = this;
73 internal Type(Type underlyingType)
75 System.Diagnostics.Debug.Assert(underlyingType.underlyingType == underlyingType);
76 this.underlyingType = underlyingType;
77 this.typeFlags = underlyingType.typeFlags;
80 public static Binder DefaultBinder
82 get { return new DefaultBinder(); }
85 public sealed override MemberTypes MemberType
87 get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
90 public virtual string AssemblyQualifiedName
92 // NOTE the assembly name is not escaped here, only when used in a generic type instantiation
93 get { return this.FullName + ", " + this.Assembly.FullName; }
96 public abstract Type BaseType
101 public abstract TypeAttributes Attributes
106 public virtual Type GetElementType()
111 internal virtual void CheckBaked()
115 public virtual Type[] __GetDeclaredTypes()
117 return Type.EmptyTypes;
120 public virtual Type[] __GetDeclaredInterfaces()
122 return Type.EmptyTypes;
125 public virtual MethodBase[] __GetDeclaredMethods()
127 return Empty<MethodBase>.Array;
130 public virtual __MethodImplMap __GetMethodImplMap()
132 throw new NotSupportedException();
135 public virtual FieldInfo[] __GetDeclaredFields()
137 return Empty<FieldInfo>.Array;
140 public virtual EventInfo[] __GetDeclaredEvents()
142 return Empty<EventInfo>.Array;
145 public virtual PropertyInfo[] __GetDeclaredProperties()
147 return Empty<PropertyInfo>.Array;
150 public virtual CustomModifiers __GetCustomModifiers()
152 return new CustomModifiers();
155 [Obsolete("Please use __GetCustomModifiers() instead.")]
156 public Type[] __GetRequiredCustomModifiers()
158 return __GetCustomModifiers().GetRequired();
161 [Obsolete("Please use __GetCustomModifiers() instead.")]
162 public Type[] __GetOptionalCustomModifiers()
164 return __GetCustomModifiers().GetOptional();
167 public virtual __StandAloneMethodSig __MethodSignature
169 get { throw new InvalidOperationException(); }
172 public virtual bool HasElementType
174 get { return false; }
177 public virtual bool IsArray
179 get { return false; }
182 public virtual bool __IsVector
184 get { return false; }
187 public virtual bool IsByRef
189 get { return false; }
192 public virtual bool IsPointer
194 get { return false; }
197 public virtual bool __IsFunctionPointer
199 get { return false; }
202 public virtual bool IsValueType
206 Type baseType = this.BaseType;
207 return baseType != null
208 && baseType.IsEnumOrValueType
209 && !this.IsEnumOrValueType;
213 public virtual bool IsGenericParameter
215 get { return false; }
218 public virtual int GenericParameterPosition
220 get { throw new NotSupportedException(); }
223 public virtual MethodBase DeclaringMethod
228 public Type UnderlyingSystemType
230 get { return underlyingType; }
233 public override Type DeclaringType
238 public virtual string __Name
240 get { throw new InvalidOperationException(); }
243 public virtual string __Namespace
245 get { throw new InvalidOperationException(); }
248 public abstract override string Name
253 public virtual string Namespace
259 return DeclaringType.Namespace;
265 internal virtual int GetModuleBuilderToken()
267 throw new InvalidOperationException();
270 public static bool operator ==(Type t1, Type t2)
272 // Casting to object results in smaller code than calling ReferenceEquals and makes
273 // this method more likely to be inlined.
274 // On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
275 return (object)t1 == (object)t2
276 || ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
279 public static bool operator !=(Type t1, Type t2)
284 public bool Equals(Type type)
289 public override bool Equals(object obj)
291 return Equals(obj as Type);
294 public override int GetHashCode()
296 Type type = this.UnderlyingSystemType;
297 return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
300 public virtual Type[] GetGenericArguments()
302 return Type.EmptyTypes;
305 public virtual CustomModifiers[] __GetGenericArgumentsCustomModifiers()
307 return Empty<CustomModifiers>.Array;
310 [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
311 public Type[][] __GetGenericArgumentsRequiredCustomModifiers()
313 CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
314 Type[][] array = new Type[customModifiers.Length][];
315 for (int i = 0; i < array.Length; i++)
317 array[i] = customModifiers[i].GetRequired();
322 [Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
323 public Type[][] __GetGenericArgumentsOptionalCustomModifiers()
325 CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
326 Type[][] array = new Type[customModifiers.Length][];
327 for (int i = 0; i < array.Length; i++)
329 array[i] = customModifiers[i].GetOptional();
334 public virtual Type GetGenericTypeDefinition()
336 throw new InvalidOperationException();
339 public virtual StructLayoutAttribute StructLayoutAttribute
344 public virtual bool __GetLayout(out int packingSize, out int typeSize)
351 public virtual bool IsGenericType
353 get { return false; }
356 public virtual bool IsGenericTypeDefinition
358 get { return false; }
361 internal virtual bool IsGenericTypeInstance
363 get { return false; }
366 public virtual bool ContainsGenericParameters
370 if (this.IsGenericParameter)
374 foreach (Type arg in this.GetGenericArguments())
376 if (arg.ContainsGenericParameters)
385 public virtual Type[] GetGenericParameterConstraints()
387 throw new InvalidOperationException();
390 public virtual GenericParameterAttributes GenericParameterAttributes
392 get { throw new InvalidOperationException(); }
395 public virtual int GetArrayRank()
397 throw new NotSupportedException();
400 public virtual int[] __GetArraySizes()
402 throw new NotSupportedException();
405 public virtual int[] __GetArrayLowerBounds()
407 throw new NotSupportedException();
411 public virtual Type GetEnumUnderlyingType()
415 throw new ArgumentException();
418 return GetEnumUnderlyingTypeImpl();
421 internal Type GetEnumUnderlyingTypeImpl()
423 foreach (FieldInfo field in __GetDeclaredFields())
427 // the CLR assumes that an enum has only one instance field, so we can do the same
428 return field.FieldType;
431 throw new InvalidOperationException();
434 public override string ToString()
439 public abstract string FullName
444 protected string GetFullName()
446 string ns = TypeNameParser.Escape(this.__Namespace);
447 Type decl = this.DeclaringType;
456 return ns + "." + this.Name;
463 return decl.FullName + "+" + this.Name;
467 return decl.FullName + "+" + ns + "." + this.Name;
472 internal virtual bool IsModulePseudoType
474 get { return false; }
477 internal virtual Type GetGenericTypeArgument(int index)
479 throw new InvalidOperationException();
482 public MemberInfo[] GetDefaultMembers()
484 Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
485 foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
487 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
489 return GetMember((string)cad.ConstructorArguments[0].Value);
492 return Empty<MemberInfo>.Array;
495 public MemberInfo[] GetMember(string name)
497 return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
500 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
502 return GetMember(name, MemberTypes.All, bindingAttr);
505 public MemberInfo[] GetMembers()
507 return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
510 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
512 List<MemberInfo> members = new List<MemberInfo>();
513 members.AddRange(GetConstructors(bindingAttr));
514 members.AddRange(GetMethods(bindingAttr));
515 members.AddRange(GetFields(bindingAttr));
516 members.AddRange(GetProperties(bindingAttr));
517 members.AddRange(GetEvents(bindingAttr));
518 members.AddRange(GetNestedTypes(bindingAttr));
519 return members.ToArray();
522 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
525 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
527 name = name.ToLowerInvariant();
528 filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.ToLowerInvariant().Equals(filterCriteria); };
532 filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
534 return FindMembers(type, bindingAttr, filter, name);
537 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
539 foreach (MemberInfo member in members)
541 if (filter == null || filter(member, filterCriteria))
548 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
550 List<MemberInfo> members = new List<MemberInfo>();
551 if ((memberType & MemberTypes.Constructor) != 0)
553 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
555 if ((memberType & MemberTypes.Method) != 0)
557 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
559 if ((memberType & MemberTypes.Field) != 0)
561 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
563 if ((memberType & MemberTypes.Property) != 0)
565 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
567 if ((memberType & MemberTypes.Event) != 0)
569 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
571 if ((memberType & MemberTypes.NestedType) != 0)
573 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
575 return members.ToArray();
578 private MemberInfo[] GetMembers<T>()
580 if (typeof(T) == typeof(ConstructorInfo) || typeof(T) == typeof(MethodInfo))
582 return __GetDeclaredMethods();
584 else if (typeof(T) == typeof(FieldInfo))
586 return __GetDeclaredFields();
588 else if (typeof(T) == typeof(PropertyInfo))
590 return __GetDeclaredProperties();
592 else if (typeof(T) == typeof(EventInfo))
594 return __GetDeclaredEvents();
596 else if (typeof(T) == typeof(Type))
598 return __GetDeclaredTypes();
602 throw new InvalidOperationException();
606 private T[] GetMembers<T>(BindingFlags flags)
610 List<T> list = new List<T>();
611 foreach (MemberInfo member in GetMembers<T>())
613 if (member is T && member.BindingFlagsMatch(flags))
618 if ((flags & BindingFlags.DeclaredOnly) == 0)
620 for (Type type = this.BaseType; type != null; type = type.BaseType)
623 foreach (MemberInfo member in type.GetMembers<T>())
625 if (member is T && member.BindingFlagsMatchInherited(flags))
627 list.Add((T)member.SetReflectedType(this));
632 return list.ToArray();
635 private T GetMemberByName<T>(string name, BindingFlags flags, Predicate<T> filter)
639 if ((flags & BindingFlags.IgnoreCase) != 0)
641 name = name.ToLowerInvariant();
644 foreach (MemberInfo member in GetMembers<T>())
646 if (member is T && member.BindingFlagsMatch(flags))
648 string memberName = member.Name;
649 if ((flags & BindingFlags.IgnoreCase) != 0)
651 memberName = memberName.ToLowerInvariant();
653 if (memberName == name && (filter == null || filter((T)member)))
657 throw new AmbiguousMatchException();
663 if ((flags & BindingFlags.DeclaredOnly) == 0)
665 for (Type type = this.BaseType; (found == null || typeof(T) == typeof(MethodInfo)) && type != null; type = type.BaseType)
668 foreach (MemberInfo member in type.GetMembers<T>())
670 if (member is T && member.BindingFlagsMatchInherited(flags))
672 string memberName = member.Name;
673 if ((flags & BindingFlags.IgnoreCase) != 0)
675 memberName = memberName.ToLowerInvariant();
677 if (memberName == name && (filter == null || filter((T)member)))
682 // TODO does this depend on HideBySig vs HideByName?
683 if ((mi = found as MethodInfo) != null
684 && mi.MethodSignature.MatchParameterTypes(((MethodBase)member).MethodSignature))
688 throw new AmbiguousMatchException();
690 found = (T)member.SetReflectedType(this);
699 private T GetMemberByName<T>(string name, BindingFlags flags)
702 return GetMemberByName<T>(name, flags, null);
705 public EventInfo GetEvent(string name)
707 return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
710 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
712 return GetMemberByName<EventInfo>(name, bindingAttr);
715 public EventInfo[] GetEvents()
717 return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
720 public EventInfo[] GetEvents(BindingFlags bindingAttr)
722 return GetMembers<EventInfo>(bindingAttr);
725 public FieldInfo GetField(string name)
727 return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
730 public FieldInfo GetField(string name, BindingFlags bindingAttr)
732 return GetMemberByName<FieldInfo>(name, bindingAttr);
735 public FieldInfo[] GetFields()
737 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
740 public FieldInfo[] GetFields(BindingFlags bindingAttr)
742 return GetMembers<FieldInfo>(bindingAttr);
745 public Type[] GetInterfaces()
747 List<Type> list = new List<Type>();
748 for (Type type = this; type != null; type = type.BaseType)
750 AddInterfaces(list, type);
752 return list.ToArray();
755 private static void AddInterfaces(List<Type> list, Type type)
758 foreach (Type iface in type.__GetDeclaredInterfaces())
760 if (!list.Contains(iface))
763 AddInterfaces(list, iface);
768 public MethodInfo[] GetMethods(BindingFlags bindingAttr)
771 List<MethodInfo> list = new List<MethodInfo>();
772 foreach (MethodBase mb in __GetDeclaredMethods())
774 MethodInfo mi = mb as MethodInfo;
775 if (mi != null && mi.BindingFlagsMatch(bindingAttr))
780 if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
782 List<MethodInfo> baseMethods = new List<MethodInfo>();
783 foreach (MethodInfo mi in list)
787 baseMethods.Add(mi.GetBaseDefinition());
790 for (Type type = this.BaseType; type != null; type = type.BaseType)
793 foreach (MethodBase mb in type.__GetDeclaredMethods())
795 MethodInfo mi = mb as MethodInfo;
796 if (mi != null && mi.BindingFlagsMatchInherited(bindingAttr))
800 if (baseMethods == null)
802 baseMethods = new List<MethodInfo>();
804 else if (FindMethod(baseMethods, mi.GetBaseDefinition()))
808 baseMethods.Add(mi.GetBaseDefinition());
810 list.Add((MethodInfo)mi.SetReflectedType(this));
815 return list.ToArray();
818 private static bool FindMethod(List<MethodInfo> methods, MethodInfo method)
820 foreach (MethodInfo m in methods)
822 if (m.Name == method.Name && m.MethodSignature.Equals(method.MethodSignature))
830 public MethodInfo[] GetMethods()
832 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
835 public MethodInfo GetMethod(string name)
837 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
840 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
842 return GetMemberByName<MethodInfo>(name, bindingAttr);
845 public MethodInfo GetMethod(string name, Type[] types)
847 return GetMethod(name, types, null);
850 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
852 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
855 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
857 // first we try an exact match and only if that fails we fall back to using the binder
858 return GetMemberByName<MethodInfo>(name, bindingAttr,
859 delegate(MethodInfo method) { return method.MethodSignature.MatchParameterTypes(types); })
860 ?? GetMethodWithBinder<MethodInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
863 private T GetMethodWithBinder<T>(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
866 List<MethodBase> list = new List<MethodBase>();
867 GetMemberByName<T>(name, bindingAttr, delegate(T method) {
871 return (T)binder.SelectMethod(bindingAttr, list.ToArray(), types, modifiers);
874 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
876 // FXBUG callConvention seems to be ignored
877 return GetMethod(name, bindingAttr, binder, types, modifiers);
880 public ConstructorInfo[] GetConstructors()
882 return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
885 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
887 return GetMembers<ConstructorInfo>(bindingAttr | BindingFlags.DeclaredOnly);
890 public ConstructorInfo GetConstructor(Type[] types)
892 return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
895 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
897 // first we try an exact match and only if that fails we fall back to using the binder
898 return GetMemberByName<ConstructorInfo>(ConstructorInfo.ConstructorName, bindingAttr | BindingFlags.DeclaredOnly,
899 delegate(ConstructorInfo ctor) { return ctor.MethodSignature.MatchParameterTypes(types); })
900 ?? GetMethodWithBinder<ConstructorInfo>(ConstructorInfo.ConstructorName, bindingAttr, binder ?? DefaultBinder, types, modifiers);
903 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
905 // FXBUG callConvention seems to be ignored
906 return GetConstructor(bindingAttr, binder, types, modifiers);
909 internal Type ResolveNestedType(TypeName typeName)
911 return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
914 // unlike the public API, this takes the namespace and name into account
915 internal virtual Type FindNestedType(TypeName name)
917 foreach (Type type in __GetDeclaredTypes())
919 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
927 internal virtual Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
929 foreach (Type type in __GetDeclaredTypes())
931 if (new TypeName(type.__Namespace, type.__Name).ToLowerInvariant() == lowerCaseName)
939 public Type GetNestedType(string name)
941 return GetNestedType(name, BindingFlags.Public);
944 public Type GetNestedType(string name, BindingFlags bindingAttr)
946 // FXBUG the namespace is ignored, so we can use GetMemberByName
947 return GetMemberByName<Type>(name, bindingAttr | BindingFlags.DeclaredOnly);
950 public Type[] GetNestedTypes()
952 return GetNestedTypes(BindingFlags.Public);
955 public Type[] GetNestedTypes(BindingFlags bindingAttr)
957 // FXBUG the namespace is ignored, so we can use GetMember
958 return GetMembers<Type>(bindingAttr | BindingFlags.DeclaredOnly);
961 public PropertyInfo[] GetProperties()
963 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
966 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
968 return GetMembers<PropertyInfo>(bindingAttr);
971 public PropertyInfo GetProperty(string name)
973 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
976 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
978 return GetMemberByName<PropertyInfo>(name, bindingAttr);
981 public PropertyInfo GetProperty(string name, Type returnType)
983 const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
984 return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertyType.Equals(returnType); })
985 ?? GetPropertyWithBinder(name, flags, DefaultBinder, returnType, null, null);
988 public PropertyInfo GetProperty(string name, Type[] types)
990 const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
991 return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertySignature.MatchParameterTypes(types); })
992 ?? GetPropertyWithBinder(name, flags, DefaultBinder, null, types, null);
995 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
997 return GetProperty(name, returnType, types, null);
1000 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1002 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
1005 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1007 return GetMemberByName<PropertyInfo>(name, bindingAttr,
1008 delegate(PropertyInfo prop) {
1009 return prop.PropertyType.Equals(returnType) && prop.PropertySignature.MatchParameterTypes(types);
1011 ?? GetPropertyWithBinder(name, bindingAttr, binder ?? DefaultBinder, returnType, types, modifiers);
1014 private PropertyInfo GetPropertyWithBinder(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1016 List<PropertyInfo> list = new List<PropertyInfo>();
1017 GetMemberByName<PropertyInfo>(name, bindingAttr, delegate(PropertyInfo property) {
1021 return binder.SelectProperty(bindingAttr, list.ToArray(), returnType, types, modifiers);
1024 public Type GetInterface(string name)
1026 return GetInterface(name, false);
1029 public Type GetInterface(string name, bool ignoreCase)
1033 name = name.ToLowerInvariant();
1036 foreach (Type type in GetInterfaces())
1038 string typeName = type.FullName;
1041 typeName = typeName.ToLowerInvariant();
1043 if (typeName == name)
1047 throw new AmbiguousMatchException();
1055 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
1057 List<Type> list = new List<Type>();
1058 foreach (Type type in GetInterfaces())
1060 if (filter(type, filterCriteria))
1065 return list.ToArray();
1068 public ConstructorInfo TypeInitializer
1070 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
1073 public bool IsPrimitive
1077 Universe u = this.Universe;
1078 return this == u.System_Boolean
1079 || this == u.System_Byte
1080 || this == u.System_SByte
1081 || this == u.System_Int16
1082 || this == u.System_UInt16
1083 || this == u.System_Int32
1084 || this == u.System_UInt32
1085 || this == u.System_Int64
1086 || this == u.System_UInt64
1087 || this == u.System_IntPtr
1088 || this == u.System_UIntPtr
1089 || this == u.System_Char
1090 || this == u.System_Double
1091 || this == u.System_Single
1100 Type baseType = this.BaseType;
1101 return baseType != null
1102 && baseType.IsEnumOrValueType
1103 && baseType.__Name[0] == 'E';
1107 public bool IsSealed
1109 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1112 public bool IsAbstract
1114 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1117 private bool CheckVisibility(TypeAttributes access)
1119 return (Attributes & TypeAttributes.VisibilityMask) == access;
1122 public bool IsPublic
1124 get { return CheckVisibility(TypeAttributes.Public); }
1127 public bool IsNestedPublic
1129 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1132 public bool IsNestedPrivate
1134 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1137 public bool IsNestedFamily
1139 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1142 public bool IsNestedAssembly
1144 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1147 public bool IsNestedFamANDAssem
1149 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1152 public bool IsNestedFamORAssem
1154 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1157 public bool IsNotPublic
1159 get { return CheckVisibility(TypeAttributes.NotPublic); }
1162 public bool IsImport
1164 get { return (Attributes & TypeAttributes.Import) != 0; }
1167 public bool IsCOMObject
1169 get { return IsClass && IsImport; }
1172 public bool IsContextful
1174 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1177 public bool IsMarshalByRef
1179 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1182 public virtual bool IsVisible
1184 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1187 public bool IsAnsiClass
1189 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1192 public bool IsUnicodeClass
1194 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1197 public bool IsAutoClass
1199 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1202 public bool IsAutoLayout
1204 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1207 public bool IsLayoutSequential
1209 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1212 public bool IsExplicitLayout
1214 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1217 public bool IsSpecialName
1219 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1222 public bool IsSerializable
1224 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1229 get { return !IsInterface && !IsValueType; }
1232 public bool IsInterface
1234 get { return (Attributes & TypeAttributes.Interface) != 0; }
1237 public bool IsNested
1239 // FXBUG we check the declaring type (like .NET) and this results
1240 // in IsNested returning true for a generic type parameter
1241 get { return this.DeclaringType != null; }
1244 public virtual bool __ContainsMissingType
1248 if (this.__IsMissing)
1252 foreach (Type arg in this.GetGenericArguments())
1254 if (arg.__ContainsMissingType)
1263 public Type MakeArrayType()
1265 return ArrayType.Make(this, new CustomModifiers());
1268 public Type __MakeArrayType(CustomModifiers customModifiers)
1270 return ArrayType.Make(this, customModifiers);
1273 [Obsolete("Please use __MakeArrayType(CustomModifiers) instead.")]
1274 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1276 return __MakeArrayType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1279 public Type MakeArrayType(int rank)
1281 return __MakeArrayType(rank, new CustomModifiers());
1284 public Type __MakeArrayType(int rank, CustomModifiers customModifiers)
1286 return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], customModifiers);
1289 [Obsolete("Please use __MakeArrayType(int, CustomModifiers) instead.")]
1290 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1292 return __MakeArrayType(rank, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1295 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, CustomModifiers customModifiers)
1297 return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, customModifiers);
1300 [Obsolete("Please use __MakeArrayType(int, int[], int[], CustomModifiers) instead.")]
1301 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1303 return __MakeArrayType(rank, sizes, lobounds, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1306 public Type MakeByRefType()
1308 return ByRefType.Make(this, new CustomModifiers());
1311 public Type __MakeByRefType(CustomModifiers customModifiers)
1313 return ByRefType.Make(this, customModifiers);
1316 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1317 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1319 return __MakeByRefType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1322 public Type MakePointerType()
1324 return PointerType.Make(this, new CustomModifiers());
1327 public Type __MakePointerType(CustomModifiers customModifiers)
1329 return PointerType.Make(this, customModifiers);
1332 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1333 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1335 return __MakePointerType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1338 public Type MakeGenericType(params Type[] typeArguments)
1340 return __MakeGenericType(typeArguments, null);
1343 public Type __MakeGenericType(Type[] typeArguments, CustomModifiers[] customModifiers)
1345 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1347 throw new InvalidOperationException();
1349 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), customModifiers == null ? null : (CustomModifiers[])customModifiers.Clone());
1352 [Obsolete("Please use __MakeGenericType(Type[], CustomModifiers[]) instead.")]
1353 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1355 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1357 throw new InvalidOperationException();
1359 CustomModifiers[] mods = null;
1360 if (requiredCustomModifiers != null || optionalCustomModifiers != null)
1362 mods = new CustomModifiers[typeArguments.Length];
1363 for (int i = 0; i < mods.Length; i++)
1365 mods[i] = CustomModifiers.FromReqOpt(Util.NullSafeElementAt(requiredCustomModifiers, i), Util.NullSafeElementAt(optionalCustomModifiers, i));
1368 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), mods);
1371 public static System.Type __GetSystemType(TypeCode typeCode)
1375 case TypeCode.Boolean:
1376 return typeof(System.Boolean);
1378 return typeof(System.Byte);
1380 return typeof(System.Char);
1381 case TypeCode.DBNull:
1382 return typeof(System.DBNull);
1383 case TypeCode.DateTime:
1384 return typeof(System.DateTime);
1385 case TypeCode.Decimal:
1386 return typeof(System.Decimal);
1387 case TypeCode.Double:
1388 return typeof(System.Double);
1389 case TypeCode.Empty:
1391 case TypeCode.Int16:
1392 return typeof(System.Int16);
1393 case TypeCode.Int32:
1394 return typeof(System.Int32);
1395 case TypeCode.Int64:
1396 return typeof(System.Int64);
1397 case TypeCode.Object:
1398 return typeof(System.Object);
1399 case TypeCode.SByte:
1400 return typeof(System.SByte);
1401 case TypeCode.Single:
1402 return typeof(System.Single);
1403 case TypeCode.String:
1404 return typeof(System.String);
1405 case TypeCode.UInt16:
1406 return typeof(System.UInt16);
1407 case TypeCode.UInt32:
1408 return typeof(System.UInt32);
1409 case TypeCode.UInt64:
1410 return typeof(System.UInt64);
1412 throw new ArgumentOutOfRangeException();
1416 public static TypeCode GetTypeCode(Type type)
1420 return TypeCode.Empty;
1422 if (!type.__IsMissing && type.IsEnum)
1424 type = type.GetEnumUnderlyingType();
1426 Universe u = type.Module.universe;
1427 if (type == u.System_Boolean)
1429 return TypeCode.Boolean;
1431 else if (type == u.System_Char)
1433 return TypeCode.Char;
1435 else if (type == u.System_SByte)
1437 return TypeCode.SByte;
1439 else if (type == u.System_Byte)
1441 return TypeCode.Byte;
1443 else if (type == u.System_Int16)
1445 return TypeCode.Int16;
1447 else if (type == u.System_UInt16)
1449 return TypeCode.UInt16;
1451 else if (type == u.System_Int32)
1453 return TypeCode.Int32;
1455 else if (type == u.System_UInt32)
1457 return TypeCode.UInt32;
1459 else if (type == u.System_Int64)
1461 return TypeCode.Int64;
1463 else if (type == u.System_UInt64)
1465 return TypeCode.UInt64;
1467 else if (type == u.System_Single)
1469 return TypeCode.Single;
1471 else if (type == u.System_Double)
1473 return TypeCode.Double;
1475 else if (type == u.System_DateTime)
1477 return TypeCode.DateTime;
1479 else if (type == u.System_DBNull)
1481 return TypeCode.DBNull;
1483 else if (type == u.System_Decimal)
1485 return TypeCode.Decimal;
1487 else if (type == u.System_String)
1489 return TypeCode.String;
1491 else if (type.__IsMissing)
1493 throw new MissingMemberException(type);
1497 return TypeCode.Object;
1501 public Assembly Assembly
1503 get { return Module.Assembly; }
1506 // note that interface/delegate co- and contravariance is not considered
1507 public bool IsAssignableFrom(Type type)
1509 if (this.Equals(type))
1513 else if (type == null)
1517 else if (this.IsArray && type.IsArray)
1519 if (this.GetArrayRank() != type.GetArrayRank())
1523 else if (this.__IsVector && !type.__IsVector)
1527 Type e1 = this.GetElementType();
1528 Type e2 = type.GetElementType();
1529 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1531 else if (this.IsSealed)
1535 else if (this.IsInterface)
1537 return Array.IndexOf(type.GetInterfaces(), this) != -1;
1539 else if (type.IsInterface)
1541 return this == this.Module.universe.System_Object;
1543 else if (type.IsPointer)
1545 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1549 return type.IsSubclassOf(this);
1553 public bool IsSubclassOf(Type type)
1555 Type thisType = this.BaseType;
1556 while (thisType != null)
1558 if (thisType.Equals(type))
1562 thisType = thisType.BaseType;
1567 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1568 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1569 private bool IsDirectlyImplementedInterface(Type interfaceType)
1571 foreach (Type iface in __GetDeclaredInterfaces())
1573 if (interfaceType.IsAssignableFrom(iface))
1581 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1584 InterfaceMapping map = new InterfaceMapping();
1585 if (!IsDirectlyImplementedInterface(interfaceType))
1587 Type baseType = this.BaseType;
1588 if (baseType == null)
1590 throw new ArgumentException();
1594 map = baseType.GetInterfaceMap(interfaceType);
1599 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1600 map.InterfaceType = interfaceType;
1601 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1602 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1603 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1604 for (int i = 0; i < map.TargetMethods.Length; i++)
1606 if (map.TargetMethods[i] == null)
1608 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1609 for (int j = 0; j < methods.Length; j++)
1611 if (methods[j].Name == map.InterfaceMethods[i].Name
1612 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1614 map.TargetMethods[i] = methods[j];
1619 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1621 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1624 map.TargetType = this;
1628 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1630 __MethodImplMap impl = __GetMethodImplMap();
1631 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1633 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1635 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1636 if (index != -1 && targetMethods[index] == null)
1638 targetMethods[index] = impl.MethodBodies[i];
1644 Type IGenericContext.GetGenericTypeArgument(int index)
1646 return GetGenericTypeArgument(index);
1649 Type IGenericContext.GetGenericMethodArgument(int index)
1651 throw new BadImageFormatException();
1654 Type IGenericBinder.BindTypeParameter(Type type)
1656 return GetGenericTypeArgument(type.GenericParameterPosition);
1659 Type IGenericBinder.BindMethodParameter(Type type)
1661 throw new BadImageFormatException();
1664 internal virtual Type BindTypeParameters(IGenericBinder binder)
1666 if (IsGenericTypeDefinition)
1668 Type[] args = GetGenericArguments();
1669 Type.InplaceBindTypeParameters(binder, args);
1670 return GenericTypeInstance.Make(this, args, null);
1678 private static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1680 for (int i = 0; i < types.Length; i++)
1682 types[i] = types[i].BindTypeParameters(binder);
1686 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1688 foreach (MethodBase method in __GetDeclaredMethods())
1690 if (method.Name == name && method.MethodSignature.Equals(signature))
1698 internal virtual FieldInfo FindField(string name, FieldSignature signature)
1700 foreach (FieldInfo field in __GetDeclaredFields())
1702 if (field.Name == name && field.FieldSignature.Equals(signature))
1710 internal bool IsAllowMultipleCustomAttribute
1714 IList<CustomAttributeData> cad = GetCustomAttributesData(this.Module.universe.System_AttributeUsageAttribute);
1717 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1719 if (arg.MemberInfo.Name == "AllowMultiple")
1721 return (bool)arg.TypedValue.Value;
1729 internal bool IsPseudoCustomAttribute
1733 Universe u = this.Module.universe;
1734 return this == u.System_NonSerializedAttribute
1735 || this == u.System_SerializableAttribute
1736 || this == u.System_Runtime_InteropServices_DllImportAttribute
1737 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1738 || this == u.System_Runtime_InteropServices_InAttribute
1739 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1740 || this == u.System_Runtime_InteropServices_OutAttribute
1741 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1742 || this == u.System_Runtime_InteropServices_OptionalAttribute
1743 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1744 || this == u.System_Runtime_InteropServices_ComImportAttribute
1745 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1746 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1751 internal Type MarkNotValueType()
1753 typeFlags |= TypeFlags.NotValueType;
1757 internal Type MarkValueType()
1759 typeFlags |= TypeFlags.ValueType;
1763 internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
1765 Universe u = this.Module.universe;
1766 MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, new PackedCustomModifiers(), CallingConventions.Standard | CallingConventions.HasThis, 0);
1768 FindMethod(".ctor", methodSig) ??
1769 u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
1770 return (ConstructorInfo)mb;
1773 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1775 return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length));
1778 private MethodBase CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
1780 MethodSignature sig = new MethodSignature(
1781 returnType ?? this.Module.universe.System_Void,
1782 Util.Copy(parameterTypes),
1786 MethodInfo method = new MissingMethod(this, name, sig);
1787 if (name == ".ctor" || name == ".cctor")
1789 return new ConstructorInfoImpl(method);
1794 [Obsolete("Please use __CreateMissingMethod(string, CallingConventions, Type, CustomModifiers, Type[], CustomModifiers[]) instead")]
1795 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
1797 return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length));
1800 public FieldInfo __CreateMissingField(string name, Type fieldType, CustomModifiers customModifiers)
1802 return new MissingField(this, name, FieldSignature.Create(fieldType, customModifiers));
1805 [Obsolete("Please use __CreateMissingField(string, Type, CustomModifiers) instead")]
1806 public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1808 return __CreateMissingField(name, fieldType, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1811 public PropertyInfo __CreateMissingProperty(string name, CallingConventions callingConvention, Type propertyType, CustomModifiers propertyTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1813 PropertySignature sig = PropertySignature.Create(callingConvention,
1816 PackedCustomModifiers.CreateFromExternal(propertyTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
1817 return new MissingProperty(this, name, sig);
1820 internal virtual Type SetMetadataTokenForMissing(int token)
1825 protected void MarkEnumOrValueType(string typeNamespace, string typeName)
1827 // we assume that mscorlib won't have nested types with these names,
1828 // so we don't check that we're not a nested type
1829 if (typeNamespace == "System"
1830 && (typeName == "Enum" || typeName == "ValueType"))
1832 typeFlags |= TypeFlags.PotentialEnumOrValueType;
1836 private bool ResolvePotentialEnumOrValueType()
1838 if (this.Assembly == this.Universe.Mscorlib || this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase))
1840 typeFlags = (typeFlags & ~TypeFlags.PotentialEnumOrValueType) | TypeFlags.EnumOrValueType;
1845 typeFlags &= ~TypeFlags.PotentialEnumOrValueType;
1850 private bool IsEnumOrValueType
1854 return (typeFlags & (TypeFlags.EnumOrValueType | TypeFlags.PotentialEnumOrValueType)) != 0
1855 && ((typeFlags & TypeFlags.EnumOrValueType) != 0 || ResolvePotentialEnumOrValueType());
1859 internal virtual IList<CustomAttributeData> GetInterfaceImplCustomAttributes(Type interfaceType, Type attributeType)
1861 throw new NotSupportedException();
1864 internal virtual Universe Universe
1866 get { return Module.universe; }
1869 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
1871 return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic);
1874 internal sealed override MemberInfo SetReflectedType(Type type)
1876 throw new InvalidOperationException();
1880 abstract class ElementHolderType : Type
1882 protected readonly Type elementType;
1884 private readonly CustomModifiers mods;
1886 protected ElementHolderType(Type elementType, CustomModifiers mods)
1888 this.elementType = elementType;
1892 protected bool EqualsHelper(ElementHolderType other)
1894 return other != null
1895 && other.elementType.Equals(elementType)
1896 && other.mods.Equals(mods);
1899 public override CustomModifiers __GetCustomModifiers()
1904 public sealed override string Name
1906 get { return elementType.Name + GetSuffix(); }
1909 public sealed override string Namespace
1911 get { return elementType.Namespace; }
1914 public sealed override string FullName
1916 get { return elementType.FullName + GetSuffix(); }
1919 public sealed override string ToString()
1921 return elementType.ToString() + GetSuffix();
1924 public sealed override Type GetElementType()
1929 public sealed override bool HasElementType
1931 get { return true; }
1934 public sealed override Module Module
1936 get { return elementType.Module; }
1939 internal sealed override int GetModuleBuilderToken()
1943 token = ((ModuleBuilder)elementType.Module).ImportType(this);
1948 public sealed override bool ContainsGenericParameters
1952 Type type = elementType;
1953 while (type.HasElementType)
1955 type = type.GetElementType();
1957 return type.ContainsGenericParameters;
1961 public sealed override bool __ContainsMissingType
1965 Type type = elementType;
1966 while (type.HasElementType)
1968 type = type.GetElementType();
1970 return type.__ContainsMissingType;
1974 internal sealed override Type BindTypeParameters(IGenericBinder binder)
1976 Type type = elementType.BindTypeParameters(binder);
1977 CustomModifiers mods = this.mods.Bind(binder);
1978 if (ReferenceEquals(type, elementType)
1979 && mods.Equals(this.mods))
1983 return Wrap(type, mods);
1986 internal override void CheckBaked()
1988 elementType.CheckBaked();
1991 internal sealed override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
1993 return CustomAttributeData.EmptyList;
1996 internal sealed override Universe Universe
1998 get { return elementType.Universe; }
2001 internal abstract string GetSuffix();
2003 protected abstract Type Wrap(Type type, CustomModifiers mods);
2006 sealed class ArrayType : ElementHolderType
2008 internal static Type Make(Type type, CustomModifiers mods)
2010 return type.Universe.CanonicalizeType(new ArrayType(type, mods));
2013 private ArrayType(Type type, CustomModifiers mods)
2018 public override Type BaseType
2020 get { return elementType.Module.universe.System_Array; }
2023 public override Type[] __GetDeclaredInterfaces()
2026 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
2027 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
2028 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
2032 public override MethodBase[] __GetDeclaredMethods()
2034 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
2035 List<MethodBase> list = new List<MethodBase>();
2036 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 }));
2037 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
2038 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
2039 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2040 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
2042 Array.Resize(ref int32, int32.Length + 1);
2043 int32[int32.Length - 1] = int32[0];
2044 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2046 return list.ToArray();
2049 public override TypeAttributes Attributes
2051 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2054 public override bool IsArray
2056 get { return true; }
2059 public override bool __IsVector
2061 get { return true; }
2064 public override int GetArrayRank()
2069 public override bool Equals(object o)
2071 return EqualsHelper(o as ArrayType);
2074 public override int GetHashCode()
2076 return elementType.GetHashCode() * 5;
2079 internal override string GetSuffix()
2084 protected override Type Wrap(Type type, CustomModifiers mods)
2086 return Make(type, mods);
2090 sealed class MultiArrayType : ElementHolderType
2092 private readonly int rank;
2093 private readonly int[] sizes;
2094 private readonly int[] lobounds;
2096 internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2098 return type.Universe.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, mods));
2101 private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2106 this.lobounds = lobounds;
2109 public override Type BaseType
2111 get { return elementType.Module.universe.System_Array; }
2114 public override MethodBase[] __GetDeclaredMethods()
2116 Type int32 = this.Module.universe.System_Int32;
2117 Type[] setArgs = new Type[rank + 1];
2118 Type[] getArgs = new Type[rank];
2119 Type[] ctorArgs = new Type[rank * 2];
2120 for (int i = 0; i < rank; i++)
2124 ctorArgs[i * 2 + 0] = int32;
2125 ctorArgs[i * 2 + 1] = int32;
2127 setArgs[rank] = elementType;
2128 return new MethodBase[] {
2129 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
2130 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
2131 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
2132 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
2133 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
2137 public override TypeAttributes Attributes
2139 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2142 public override bool IsArray
2144 get { return true; }
2147 public override int GetArrayRank()
2152 public override int[] __GetArraySizes()
2154 return Util.Copy(sizes);
2157 public override int[] __GetArrayLowerBounds()
2159 return Util.Copy(lobounds);
2162 public override bool Equals(object o)
2164 MultiArrayType at = o as MultiArrayType;
2165 return EqualsHelper(at)
2167 && ArrayEquals(at.sizes, sizes)
2168 && ArrayEquals(at.lobounds, lobounds);
2171 private static bool ArrayEquals(int[] i1, int[] i2)
2173 if (i1.Length == i2.Length)
2175 for (int i = 0; i < i1.Length; i++)
2187 public override int GetHashCode()
2189 return elementType.GetHashCode() * 9 + rank;
2192 internal override string GetSuffix()
2200 return "[" + new String(',', rank - 1) + "]";
2204 protected override Type Wrap(Type type, CustomModifiers mods)
2206 return Make(type, rank, sizes, lobounds, mods);
2210 sealed class BuiltinArrayMethod : ArrayMethod
2212 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
2213 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
2217 public override MethodAttributes Attributes
2219 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
2222 public override MethodImplAttributes GetMethodImplementationFlags()
2224 return MethodImplAttributes.IL;
2227 public override int MetadataToken
2229 get { return 0x06000000; }
2232 public override MethodBody GetMethodBody()
2237 public override ParameterInfo[] GetParameters()
2239 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
2240 for (int i = 0; i < parameterInfos.Length; i++)
2242 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
2244 return parameterInfos;
2247 public override ParameterInfo ReturnParameter
2249 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
2252 private sealed class ParameterInfoImpl : ParameterInfo
2254 private readonly MethodInfo method;
2255 private readonly Type type;
2256 private readonly int pos;
2258 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
2260 this.method = method;
2265 public override Type ParameterType
2267 get { return type; }
2270 public override string Name
2272 get { return null; }
2275 public override ParameterAttributes Attributes
2277 get { return ParameterAttributes.None; }
2280 public override int Position
2285 public override object RawDefaultValue
2287 get { return null; }
2290 public override CustomModifiers __GetCustomModifiers()
2292 return new CustomModifiers();
2295 public override MemberInfo Member
2297 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2300 public override int MetadataToken
2302 get { return 0x8000000; }
2305 internal override Module Module
2307 get { return method.Module; }
2312 sealed class ByRefType : ElementHolderType
2314 internal static Type Make(Type type, CustomModifiers mods)
2316 return type.Universe.CanonicalizeType(new ByRefType(type, mods));
2319 private ByRefType(Type type, CustomModifiers mods)
2324 public override bool Equals(object o)
2326 return EqualsHelper(o as ByRefType);
2329 public override int GetHashCode()
2331 return elementType.GetHashCode() * 3;
2334 public override Type BaseType
2336 get { return null; }
2339 public override TypeAttributes Attributes
2344 public override bool IsByRef
2346 get { return true; }
2349 internal override string GetSuffix()
2354 protected override Type Wrap(Type type, CustomModifiers mods)
2356 return Make(type, mods);
2360 sealed class PointerType : ElementHolderType
2362 internal static Type Make(Type type, CustomModifiers mods)
2364 return type.Universe.CanonicalizeType(new PointerType(type, mods));
2367 private PointerType(Type type, CustomModifiers mods)
2372 public override bool Equals(object o)
2374 return EqualsHelper(o as PointerType);
2377 public override int GetHashCode()
2379 return elementType.GetHashCode() * 7;
2382 public override Type BaseType
2384 get { return null; }
2387 public override TypeAttributes Attributes
2392 public override bool IsPointer
2394 get { return true; }
2397 internal override string GetSuffix()
2402 protected override Type Wrap(Type type, CustomModifiers mods)
2404 return Make(type, mods);
2408 sealed class GenericTypeInstance : Type
2410 private readonly Type type;
2411 private readonly Type[] args;
2412 private readonly CustomModifiers[] mods;
2413 private Type baseType;
2416 internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
2418 bool identity = true;
2419 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2421 // a TypeBuiler identity must be instantiated
2426 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2427 for (int i = 0; i < typeArguments.Length; i++)
2429 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2430 || !IsEmpty(mods, i))
2443 return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
2447 private static bool IsEmpty(CustomModifiers[] mods, int i)
2449 // we need to be extra careful, because mods doesn't not need to be in canonical format
2450 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2451 return mods == null || mods[i].IsEmpty;
2454 private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
2461 public override bool Equals(object o)
2463 GenericTypeInstance gt = o as GenericTypeInstance;
2464 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2465 && Util.ArrayEquals(gt.mods, mods);
2468 public override int GetHashCode()
2470 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2473 public override string AssemblyQualifiedName
2477 string fn = FullName;
2478 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2482 public override Type BaseType
2486 if (baseType == null)
2488 Type rawBaseType = type.BaseType;
2489 if (rawBaseType == null)
2491 baseType = rawBaseType;
2495 baseType = rawBaseType.BindTypeParameters(this);
2502 public override bool IsValueType
2504 get { return type.IsValueType; }
2507 public override bool IsVisible
2513 foreach (Type arg in args)
2526 public override Type DeclaringType
2528 get { return type.DeclaringType; }
2531 public override TypeAttributes Attributes
2533 get { return type.Attributes; }
2536 internal override void CheckBaked()
2541 public override FieldInfo[] __GetDeclaredFields()
2543 FieldInfo[] fields = type.__GetDeclaredFields();
2544 for (int i = 0; i < fields.Length; i++)
2546 fields[i] = fields[i].BindTypeParameters(this);
2551 public override Type[] __GetDeclaredInterfaces()
2553 Type[] interfaces = type.__GetDeclaredInterfaces();
2554 for (int i = 0; i < interfaces.Length; i++)
2556 interfaces[i] = interfaces[i].BindTypeParameters(this);
2561 public override MethodBase[] __GetDeclaredMethods()
2563 MethodBase[] methods = type.__GetDeclaredMethods();
2564 for (int i = 0; i < methods.Length; i++)
2566 methods[i] = methods[i].BindTypeParameters(this);
2571 public override Type[] __GetDeclaredTypes()
2573 return type.__GetDeclaredTypes();
2576 public override EventInfo[] __GetDeclaredEvents()
2578 EventInfo[] events = type.__GetDeclaredEvents();
2579 for (int i = 0; i < events.Length; i++)
2581 events[i] = events[i].BindTypeParameters(this);
2586 public override PropertyInfo[] __GetDeclaredProperties()
2588 PropertyInfo[] properties = type.__GetDeclaredProperties();
2589 for (int i = 0; i < properties.Length; i++)
2591 properties[i] = properties[i].BindTypeParameters(this);
2596 public override __MethodImplMap __GetMethodImplMap()
2598 __MethodImplMap map = type.__GetMethodImplMap();
2599 map.TargetType = this;
2600 for (int i = 0; i < map.MethodBodies.Length; i++)
2602 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2603 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2605 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2606 if (interfaceType.IsGenericType)
2608 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2615 public override string Namespace
2617 get { return type.Namespace; }
2620 public override string Name
2622 get { return type.Name; }
2625 public override string FullName
2629 if (!this.__ContainsMissingType && this.ContainsGenericParameters)
2633 StringBuilder sb = new StringBuilder(this.type.FullName);
2636 foreach (Type type in args)
2638 sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
2642 return sb.ToString();
2646 public override string ToString()
2648 StringBuilder sb = new StringBuilder(type.FullName);
2651 foreach (Type arg in args)
2658 return sb.ToString();
2661 public override Module Module
2663 get { return type.Module; }
2666 public override bool IsGenericType
2668 get { return true; }
2671 internal override bool IsGenericTypeInstance
2673 get { return true; }
2676 public override Type GetGenericTypeDefinition()
2681 public override Type[] GetGenericArguments()
2683 return Util.Copy(args);
2686 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
2688 return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
2691 internal override Type GetGenericTypeArgument(int index)
2696 public override bool ContainsGenericParameters
2700 foreach (Type type in args)
2702 if (type.ContainsGenericParameters)
2711 public override bool __ContainsMissingType
2715 foreach (Type type in args)
2717 if (type.__ContainsMissingType)
2722 return this.type.__IsMissing;
2726 public override StructLayoutAttribute StructLayoutAttribute
2728 get { return type.StructLayoutAttribute; }
2731 internal override int GetModuleBuilderToken()
2735 token = ((ModuleBuilder)type.Module).ImportType(this);
2740 internal override Type BindTypeParameters(IGenericBinder binder)
2742 for (int i = 0; i < args.Length; i++)
2744 Type xarg = args[i].BindTypeParameters(binder);
2745 if (!ReferenceEquals(xarg, args[i]))
2747 Type[] xargs = new Type[args.Length];
2748 Array.Copy(args, xargs, i);
2750 for (; i < args.Length; i++)
2752 xargs[i] = args[i].BindTypeParameters(binder);
2754 return Make(type, xargs, null);
2760 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
2762 return type.GetCustomAttributesData(attributeType);
2766 sealed class FunctionPointerType : Type
2768 private readonly Universe universe;
2769 private readonly __StandAloneMethodSig sig;
2771 internal static Type Make(Universe universe, __StandAloneMethodSig sig)
2773 return universe.CanonicalizeType(new FunctionPointerType(universe, sig));
2776 private FunctionPointerType(Universe universe, __StandAloneMethodSig sig)
2778 this.universe = universe;
2782 public override bool Equals(object obj)
2784 FunctionPointerType other = obj as FunctionPointerType;
2785 return other != null
2786 && other.universe == universe
2787 && other.sig.Equals(sig);
2790 public override int GetHashCode()
2792 return sig.GetHashCode();
2795 public override bool __IsFunctionPointer
2797 get { return true; }
2800 public override __StandAloneMethodSig __MethodSignature
2805 public override Type BaseType
2807 get { return null; }
2810 public override TypeAttributes Attributes
2815 public override string Name
2817 get { throw new InvalidOperationException(); }
2820 public override string FullName
2822 get { throw new InvalidOperationException(); }
2825 public override Module Module
2827 get { throw new InvalidOperationException(); }
2830 internal override Universe Universe
2832 get { return universe; }
2835 public override string ToString()
2837 return "<FunctionPtr>";
2841 sealed class MarkerType : Type
2843 // used by ILGenerator
2844 internal static readonly Type Fault = new MarkerType();
2845 internal static readonly Type Finally = new MarkerType();
2846 internal static readonly Type Filter = new MarkerType();
2847 // used by CustomModifiers and SignatureHelper
2848 internal static readonly Type ModOpt = new MarkerType();
2849 internal static readonly Type ModReq = new MarkerType();
2850 // used by SignatureHelper
2851 internal static readonly Type Sentinel = new MarkerType();
2852 internal static readonly Type Pinned = new MarkerType();
2854 private MarkerType() { }
2856 public override Type BaseType
2858 get { throw new InvalidOperationException(); }
2861 public override TypeAttributes Attributes
2863 get { throw new InvalidOperationException(); }
2866 public override string Name
2868 get { throw new InvalidOperationException(); }
2871 public override string FullName
2873 get { throw new InvalidOperationException(); }
2876 public override Module Module
2878 get { throw new InvalidOperationException(); }