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; }
362 public virtual bool IsConstructedGenericType
364 get { return false; }
367 public virtual bool ContainsGenericParameters
371 if (this.IsGenericParameter)
375 foreach (Type arg in this.GetGenericArguments())
377 if (arg.ContainsGenericParameters)
386 public virtual Type[] GetGenericParameterConstraints()
388 throw new InvalidOperationException();
391 public virtual GenericParameterAttributes GenericParameterAttributes
393 get { throw new InvalidOperationException(); }
396 public virtual int GetArrayRank()
398 throw new NotSupportedException();
401 public virtual int[] __GetArraySizes()
403 throw new NotSupportedException();
406 public virtual int[] __GetArrayLowerBounds()
408 throw new NotSupportedException();
412 public virtual Type GetEnumUnderlyingType()
416 throw new ArgumentException();
419 return GetEnumUnderlyingTypeImpl();
422 internal Type GetEnumUnderlyingTypeImpl()
424 foreach (FieldInfo field in __GetDeclaredFields())
428 // the CLR assumes that an enum has only one instance field, so we can do the same
429 return field.FieldType;
432 throw new InvalidOperationException();
435 public override string ToString()
440 public abstract string FullName
445 protected string GetFullName()
447 string ns = TypeNameParser.Escape(this.__Namespace);
448 Type decl = this.DeclaringType;
457 return ns + "." + this.Name;
464 return decl.FullName + "+" + this.Name;
468 return decl.FullName + "+" + ns + "." + this.Name;
473 internal virtual bool IsModulePseudoType
475 get { return false; }
478 internal virtual Type GetGenericTypeArgument(int index)
480 throw new InvalidOperationException();
483 public MemberInfo[] GetDefaultMembers()
485 Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
486 foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
488 if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
490 return GetMember((string)cad.ConstructorArguments[0].Value);
493 return Empty<MemberInfo>.Array;
496 public MemberInfo[] GetMember(string name)
498 return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
501 public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
503 return GetMember(name, MemberTypes.All, bindingAttr);
506 public MemberInfo[] GetMembers()
508 return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
511 public MemberInfo[] GetMembers(BindingFlags bindingAttr)
513 List<MemberInfo> members = new List<MemberInfo>();
514 members.AddRange(GetConstructors(bindingAttr));
515 members.AddRange(GetMethods(bindingAttr));
516 members.AddRange(GetFields(bindingAttr));
517 members.AddRange(GetProperties(bindingAttr));
518 members.AddRange(GetEvents(bindingAttr));
519 members.AddRange(GetNestedTypes(bindingAttr));
520 return members.ToArray();
523 public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
526 if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
528 name = name.ToLowerInvariant();
529 filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.ToLowerInvariant().Equals(filterCriteria); };
533 filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
535 return FindMembers(type, bindingAttr, filter, name);
538 private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
540 foreach (MemberInfo member in members)
542 if (filter == null || filter(member, filterCriteria))
549 public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
551 List<MemberInfo> members = new List<MemberInfo>();
552 if ((memberType & MemberTypes.Constructor) != 0)
554 AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
556 if ((memberType & MemberTypes.Method) != 0)
558 AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
560 if ((memberType & MemberTypes.Field) != 0)
562 AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
564 if ((memberType & MemberTypes.Property) != 0)
566 AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
568 if ((memberType & MemberTypes.Event) != 0)
570 AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
572 if ((memberType & MemberTypes.NestedType) != 0)
574 AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
576 return members.ToArray();
579 private MemberInfo[] GetMembers<T>()
581 if (typeof(T) == typeof(ConstructorInfo) || typeof(T) == typeof(MethodInfo))
583 return __GetDeclaredMethods();
585 else if (typeof(T) == typeof(FieldInfo))
587 return __GetDeclaredFields();
589 else if (typeof(T) == typeof(PropertyInfo))
591 return __GetDeclaredProperties();
593 else if (typeof(T) == typeof(EventInfo))
595 return __GetDeclaredEvents();
597 else if (typeof(T) == typeof(Type))
599 return __GetDeclaredTypes();
603 throw new InvalidOperationException();
607 private T[] GetMembers<T>(BindingFlags flags)
611 List<T> list = new List<T>();
612 foreach (MemberInfo member in GetMembers<T>())
614 if (member is T && member.BindingFlagsMatch(flags))
619 if ((flags & BindingFlags.DeclaredOnly) == 0)
621 for (Type type = this.BaseType; type != null; type = type.BaseType)
624 foreach (MemberInfo member in type.GetMembers<T>())
626 if (member is T && member.BindingFlagsMatchInherited(flags))
628 list.Add((T)member.SetReflectedType(this));
633 return list.ToArray();
636 private T GetMemberByName<T>(string name, BindingFlags flags, Predicate<T> filter)
640 if ((flags & BindingFlags.IgnoreCase) != 0)
642 name = name.ToLowerInvariant();
645 foreach (MemberInfo member in GetMembers<T>())
647 if (member is T && member.BindingFlagsMatch(flags))
649 string memberName = member.Name;
650 if ((flags & BindingFlags.IgnoreCase) != 0)
652 memberName = memberName.ToLowerInvariant();
654 if (memberName == name && (filter == null || filter((T)member)))
658 throw new AmbiguousMatchException();
664 if ((flags & BindingFlags.DeclaredOnly) == 0)
666 for (Type type = this.BaseType; (found == null || typeof(T) == typeof(MethodInfo)) && type != null; type = type.BaseType)
669 foreach (MemberInfo member in type.GetMembers<T>())
671 if (member is T && member.BindingFlagsMatchInherited(flags))
673 string memberName = member.Name;
674 if ((flags & BindingFlags.IgnoreCase) != 0)
676 memberName = memberName.ToLowerInvariant();
678 if (memberName == name && (filter == null || filter((T)member)))
683 // TODO does this depend on HideBySig vs HideByName?
684 if ((mi = found as MethodInfo) != null
685 && mi.MethodSignature.MatchParameterTypes(((MethodBase)member).MethodSignature))
689 throw new AmbiguousMatchException();
691 found = (T)member.SetReflectedType(this);
700 private T GetMemberByName<T>(string name, BindingFlags flags)
703 return GetMemberByName<T>(name, flags, null);
706 public EventInfo GetEvent(string name)
708 return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
711 public EventInfo GetEvent(string name, BindingFlags bindingAttr)
713 return GetMemberByName<EventInfo>(name, bindingAttr);
716 public EventInfo[] GetEvents()
718 return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
721 public EventInfo[] GetEvents(BindingFlags bindingAttr)
723 return GetMembers<EventInfo>(bindingAttr);
726 public FieldInfo GetField(string name)
728 return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
731 public FieldInfo GetField(string name, BindingFlags bindingAttr)
733 return GetMemberByName<FieldInfo>(name, bindingAttr);
736 public FieldInfo[] GetFields()
738 return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
741 public FieldInfo[] GetFields(BindingFlags bindingAttr)
743 return GetMembers<FieldInfo>(bindingAttr);
746 public Type[] GetInterfaces()
748 List<Type> list = new List<Type>();
749 for (Type type = this; type != null; type = type.BaseType)
751 AddInterfaces(list, type);
753 return list.ToArray();
756 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 (baseMethods.Contains(mi.GetBaseDefinition()))
808 baseMethods.Add(mi.GetBaseDefinition());
810 list.Add((MethodInfo)mi.SetReflectedType(this));
815 return list.ToArray();
818 public MethodInfo[] GetMethods()
820 return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
823 public MethodInfo GetMethod(string name)
825 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
828 public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
830 return GetMemberByName<MethodInfo>(name, bindingAttr);
833 public MethodInfo GetMethod(string name, Type[] types)
835 return GetMethod(name, types, null);
838 public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
840 return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
843 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
845 // first we try an exact match and only if that fails we fall back to using the binder
846 return GetMemberByName<MethodInfo>(name, bindingAttr,
847 delegate(MethodInfo method) { return method.MethodSignature.MatchParameterTypes(types); })
848 ?? GetMethodWithBinder<MethodInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
851 private T GetMethodWithBinder<T>(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
854 List<MethodBase> list = new List<MethodBase>();
855 GetMemberByName<T>(name, bindingAttr, delegate(T method) {
859 return (T)binder.SelectMethod(bindingAttr, list.ToArray(), types, modifiers);
862 public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
864 // FXBUG callConvention seems to be ignored
865 return GetMethod(name, bindingAttr, binder, types, modifiers);
868 public ConstructorInfo[] GetConstructors()
870 return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
873 public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
875 return GetMembers<ConstructorInfo>(bindingAttr | BindingFlags.DeclaredOnly);
878 public ConstructorInfo GetConstructor(Type[] types)
880 return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
883 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
885 ConstructorInfo ci1 = null;
886 if ((bindingAttr & BindingFlags.Instance) != 0)
888 ci1 = GetConstructorImpl(ConstructorInfo.ConstructorName, bindingAttr, binder, types, modifiers);
890 if ((bindingAttr & BindingFlags.Static) != 0)
892 ConstructorInfo ci2 = GetConstructorImpl(ConstructorInfo.TypeConstructorName, bindingAttr, binder, types, modifiers);
897 throw new AmbiguousMatchException();
905 private ConstructorInfo GetConstructorImpl(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
907 // first we try an exact match and only if that fails we fall back to using the binder
908 return GetMemberByName<ConstructorInfo>(name, bindingAttr | BindingFlags.DeclaredOnly,
909 delegate(ConstructorInfo ctor) { return ctor.MethodSignature.MatchParameterTypes(types); })
910 ?? GetMethodWithBinder<ConstructorInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
913 public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
915 // FXBUG callConvention seems to be ignored
916 return GetConstructor(bindingAttr, binder, types, modifiers);
919 internal Type ResolveNestedType(TypeName typeName)
921 return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
924 // unlike the public API, this takes the namespace and name into account
925 internal virtual Type FindNestedType(TypeName name)
927 foreach (Type type in __GetDeclaredTypes())
929 if (type.__Namespace == name.Namespace && type.__Name == name.Name)
937 internal virtual Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
939 foreach (Type type in __GetDeclaredTypes())
941 if (new TypeName(type.__Namespace, type.__Name).ToLowerInvariant() == lowerCaseName)
949 public Type GetNestedType(string name)
951 return GetNestedType(name, BindingFlags.Public);
954 public Type GetNestedType(string name, BindingFlags bindingAttr)
956 // FXBUG the namespace is ignored, so we can use GetMemberByName
957 return GetMemberByName<Type>(name, bindingAttr | BindingFlags.DeclaredOnly);
960 public Type[] GetNestedTypes()
962 return GetNestedTypes(BindingFlags.Public);
965 public Type[] GetNestedTypes(BindingFlags bindingAttr)
967 // FXBUG the namespace is ignored, so we can use GetMember
968 return GetMembers<Type>(bindingAttr | BindingFlags.DeclaredOnly);
971 public PropertyInfo[] GetProperties()
973 return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
976 public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
978 return GetMembers<PropertyInfo>(bindingAttr);
981 public PropertyInfo GetProperty(string name)
983 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
986 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
988 return GetMemberByName<PropertyInfo>(name, bindingAttr);
991 public PropertyInfo GetProperty(string name, Type returnType)
993 const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
994 return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertyType.Equals(returnType); })
995 ?? GetPropertyWithBinder(name, flags, DefaultBinder, returnType, null, null);
998 public PropertyInfo GetProperty(string name, Type[] types)
1000 const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
1001 return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertySignature.MatchParameterTypes(types); })
1002 ?? GetPropertyWithBinder(name, flags, DefaultBinder, null, types, null);
1005 public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
1007 return GetProperty(name, returnType, types, null);
1010 public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1012 return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
1015 public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1017 return GetMemberByName<PropertyInfo>(name, bindingAttr,
1018 delegate(PropertyInfo prop) {
1019 return prop.PropertyType.Equals(returnType) && prop.PropertySignature.MatchParameterTypes(types);
1021 ?? GetPropertyWithBinder(name, bindingAttr, binder ?? DefaultBinder, returnType, types, modifiers);
1024 private PropertyInfo GetPropertyWithBinder(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1026 List<PropertyInfo> list = new List<PropertyInfo>();
1027 GetMemberByName<PropertyInfo>(name, bindingAttr, delegate(PropertyInfo property) {
1031 return binder.SelectProperty(bindingAttr, list.ToArray(), returnType, types, modifiers);
1034 public Type GetInterface(string name)
1036 return GetInterface(name, false);
1039 public Type GetInterface(string name, bool ignoreCase)
1043 name = name.ToLowerInvariant();
1046 foreach (Type type in GetInterfaces())
1048 string typeName = type.FullName;
1051 typeName = typeName.ToLowerInvariant();
1053 if (typeName == name)
1057 throw new AmbiguousMatchException();
1065 public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
1067 List<Type> list = new List<Type>();
1068 foreach (Type type in GetInterfaces())
1070 if (filter(type, filterCriteria))
1075 return list.ToArray();
1078 public ConstructorInfo TypeInitializer
1080 get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
1083 public bool IsPrimitive
1087 Universe u = this.Universe;
1088 return this == u.System_Boolean
1089 || this == u.System_Byte
1090 || this == u.System_SByte
1091 || this == u.System_Int16
1092 || this == u.System_UInt16
1093 || this == u.System_Int32
1094 || this == u.System_UInt32
1095 || this == u.System_Int64
1096 || this == u.System_UInt64
1097 || this == u.System_IntPtr
1098 || this == u.System_UIntPtr
1099 || this == u.System_Char
1100 || this == u.System_Double
1101 || this == u.System_Single
1110 Type baseType = this.BaseType;
1111 return baseType != null
1112 && baseType.IsEnumOrValueType
1113 && baseType.__Name[0] == 'E';
1117 public bool IsSealed
1119 get { return (Attributes & TypeAttributes.Sealed) != 0; }
1122 public bool IsAbstract
1124 get { return (Attributes & TypeAttributes.Abstract) != 0; }
1127 private bool CheckVisibility(TypeAttributes access)
1129 return (Attributes & TypeAttributes.VisibilityMask) == access;
1132 public bool IsPublic
1134 get { return CheckVisibility(TypeAttributes.Public); }
1137 public bool IsNestedPublic
1139 get { return CheckVisibility(TypeAttributes.NestedPublic); }
1142 public bool IsNestedPrivate
1144 get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1147 public bool IsNestedFamily
1149 get { return CheckVisibility(TypeAttributes.NestedFamily); }
1152 public bool IsNestedAssembly
1154 get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1157 public bool IsNestedFamANDAssem
1159 get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1162 public bool IsNestedFamORAssem
1164 get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1167 public bool IsNotPublic
1169 get { return CheckVisibility(TypeAttributes.NotPublic); }
1172 public bool IsImport
1174 get { return (Attributes & TypeAttributes.Import) != 0; }
1177 public bool IsCOMObject
1179 get { return IsClass && IsImport; }
1182 public bool IsContextful
1184 get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1187 public bool IsMarshalByRef
1189 get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1192 public virtual bool IsVisible
1194 get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1197 public bool IsAnsiClass
1199 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1202 public bool IsUnicodeClass
1204 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1207 public bool IsAutoClass
1209 get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1212 public bool IsAutoLayout
1214 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1217 public bool IsLayoutSequential
1219 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1222 public bool IsExplicitLayout
1224 get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1227 public bool IsSpecialName
1229 get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1232 public bool IsSerializable
1234 get { return (Attributes & TypeAttributes.Serializable) != 0; }
1239 get { return !IsInterface && !IsValueType; }
1242 public bool IsInterface
1244 get { return (Attributes & TypeAttributes.Interface) != 0; }
1247 public bool IsNested
1249 // FXBUG we check the declaring type (like .NET) and this results
1250 // in IsNested returning true for a generic type parameter
1251 get { return this.DeclaringType != null; }
1254 public virtual bool __ContainsMissingType
1258 if (this.__IsMissing)
1262 foreach (Type arg in this.GetGenericArguments())
1264 if (arg.__ContainsMissingType)
1273 public Type MakeArrayType()
1275 return ArrayType.Make(this, new CustomModifiers());
1278 public Type __MakeArrayType(CustomModifiers customModifiers)
1280 return ArrayType.Make(this, customModifiers);
1283 [Obsolete("Please use __MakeArrayType(CustomModifiers) instead.")]
1284 public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1286 return __MakeArrayType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1289 public Type MakeArrayType(int rank)
1291 return __MakeArrayType(rank, new CustomModifiers());
1294 public Type __MakeArrayType(int rank, CustomModifiers customModifiers)
1296 return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], customModifiers);
1299 [Obsolete("Please use __MakeArrayType(int, CustomModifiers) instead.")]
1300 public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1302 return __MakeArrayType(rank, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1305 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, CustomModifiers customModifiers)
1307 return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, customModifiers);
1310 [Obsolete("Please use __MakeArrayType(int, int[], int[], CustomModifiers) instead.")]
1311 public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1313 return __MakeArrayType(rank, sizes, lobounds, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1316 public Type MakeByRefType()
1318 return ByRefType.Make(this, new CustomModifiers());
1321 public Type __MakeByRefType(CustomModifiers customModifiers)
1323 return ByRefType.Make(this, customModifiers);
1326 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1327 public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1329 return __MakeByRefType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1332 public Type MakePointerType()
1334 return PointerType.Make(this, new CustomModifiers());
1337 public Type __MakePointerType(CustomModifiers customModifiers)
1339 return PointerType.Make(this, customModifiers);
1342 [Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1343 public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1345 return __MakePointerType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1348 public Type MakeGenericType(params Type[] typeArguments)
1350 return __MakeGenericType(typeArguments, null);
1353 public Type __MakeGenericType(Type[] typeArguments, CustomModifiers[] customModifiers)
1355 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1357 throw new InvalidOperationException();
1359 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), customModifiers == null ? null : (CustomModifiers[])customModifiers.Clone());
1362 [Obsolete("Please use __MakeGenericType(Type[], CustomModifiers[]) instead.")]
1363 public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1365 if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1367 throw new InvalidOperationException();
1369 CustomModifiers[] mods = null;
1370 if (requiredCustomModifiers != null || optionalCustomModifiers != null)
1372 mods = new CustomModifiers[typeArguments.Length];
1373 for (int i = 0; i < mods.Length; i++)
1375 mods[i] = CustomModifiers.FromReqOpt(Util.NullSafeElementAt(requiredCustomModifiers, i), Util.NullSafeElementAt(optionalCustomModifiers, i));
1378 return GenericTypeInstance.Make(this, Util.Copy(typeArguments), mods);
1381 public static System.Type __GetSystemType(TypeCode typeCode)
1385 case TypeCode.Boolean:
1386 return typeof(System.Boolean);
1388 return typeof(System.Byte);
1390 return typeof(System.Char);
1391 case TypeCode.DBNull:
1392 return typeof(System.DBNull);
1393 case TypeCode.DateTime:
1394 return typeof(System.DateTime);
1395 case TypeCode.Decimal:
1396 return typeof(System.Decimal);
1397 case TypeCode.Double:
1398 return typeof(System.Double);
1399 case TypeCode.Empty:
1401 case TypeCode.Int16:
1402 return typeof(System.Int16);
1403 case TypeCode.Int32:
1404 return typeof(System.Int32);
1405 case TypeCode.Int64:
1406 return typeof(System.Int64);
1407 case TypeCode.Object:
1408 return typeof(System.Object);
1409 case TypeCode.SByte:
1410 return typeof(System.SByte);
1411 case TypeCode.Single:
1412 return typeof(System.Single);
1413 case TypeCode.String:
1414 return typeof(System.String);
1415 case TypeCode.UInt16:
1416 return typeof(System.UInt16);
1417 case TypeCode.UInt32:
1418 return typeof(System.UInt32);
1419 case TypeCode.UInt64:
1420 return typeof(System.UInt64);
1422 throw new ArgumentOutOfRangeException();
1426 public static TypeCode GetTypeCode(Type type)
1430 return TypeCode.Empty;
1432 if (!type.__IsMissing && type.IsEnum)
1434 type = type.GetEnumUnderlyingType();
1436 Universe u = type.Module.universe;
1437 if (type == u.System_Boolean)
1439 return TypeCode.Boolean;
1441 else if (type == u.System_Char)
1443 return TypeCode.Char;
1445 else if (type == u.System_SByte)
1447 return TypeCode.SByte;
1449 else if (type == u.System_Byte)
1451 return TypeCode.Byte;
1453 else if (type == u.System_Int16)
1455 return TypeCode.Int16;
1457 else if (type == u.System_UInt16)
1459 return TypeCode.UInt16;
1461 else if (type == u.System_Int32)
1463 return TypeCode.Int32;
1465 else if (type == u.System_UInt32)
1467 return TypeCode.UInt32;
1469 else if (type == u.System_Int64)
1471 return TypeCode.Int64;
1473 else if (type == u.System_UInt64)
1475 return TypeCode.UInt64;
1477 else if (type == u.System_Single)
1479 return TypeCode.Single;
1481 else if (type == u.System_Double)
1483 return TypeCode.Double;
1485 else if (type == u.System_DateTime)
1487 return TypeCode.DateTime;
1489 else if (type == u.System_DBNull)
1491 return TypeCode.DBNull;
1493 else if (type == u.System_Decimal)
1495 return TypeCode.Decimal;
1497 else if (type == u.System_String)
1499 return TypeCode.String;
1501 else if (type.__IsMissing)
1503 throw new MissingMemberException(type);
1507 return TypeCode.Object;
1511 public Assembly Assembly
1513 get { return Module.Assembly; }
1516 public bool IsAssignableFrom(Type type)
1518 if (this.Equals(type))
1522 else if (type == null)
1526 else if (this.IsArray && type.IsArray)
1528 if (this.GetArrayRank() != type.GetArrayRank())
1532 else if (this.__IsVector && !type.__IsVector)
1536 Type e1 = this.GetElementType();
1537 Type e2 = type.GetElementType();
1538 return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1540 else if (this.IsCovariant(type))
1544 else if (this.IsSealed)
1548 else if (this.IsInterface)
1550 foreach (Type iface in type.GetInterfaces())
1552 if (this.Equals(iface) || this.IsCovariant(iface))
1559 else if (type.IsInterface)
1561 return this == this.Module.universe.System_Object;
1563 else if (type.IsPointer)
1565 return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1569 return type.IsSubclassOf(this);
1573 private bool IsCovariant(Type other)
1575 if (this.IsConstructedGenericType
1576 && other.IsConstructedGenericType
1577 && this.GetGenericTypeDefinition() == other.GetGenericTypeDefinition())
1579 Type[] typeParameters = GetGenericTypeDefinition().GetGenericArguments();
1580 for (int i = 0; i < typeParameters.Length; i++)
1582 Type t1 = this.GetGenericTypeArgument(i);
1583 Type t2 = other.GetGenericTypeArgument(i);
1584 if (t1.IsValueType != t2.IsValueType)
1588 switch (typeParameters[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask)
1590 case GenericParameterAttributes.Covariant:
1591 if (!t1.IsAssignableFrom(t2))
1596 case GenericParameterAttributes.Contravariant:
1597 if (!t2.IsAssignableFrom(t1))
1602 case GenericParameterAttributes.None:
1615 public bool IsSubclassOf(Type type)
1617 Type thisType = this.BaseType;
1618 while (thisType != null)
1620 if (thisType.Equals(type))
1624 thisType = thisType.BaseType;
1629 // This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1630 // Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1631 private bool IsDirectlyImplementedInterface(Type interfaceType)
1633 foreach (Type iface in __GetDeclaredInterfaces())
1635 if (interfaceType.IsAssignableFrom(iface))
1643 public InterfaceMapping GetInterfaceMap(Type interfaceType)
1646 InterfaceMapping map = new InterfaceMapping();
1647 if (!IsDirectlyImplementedInterface(interfaceType))
1649 Type baseType = this.BaseType;
1650 if (baseType == null)
1652 throw new ArgumentException();
1656 map = baseType.GetInterfaceMap(interfaceType);
1661 map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1662 map.InterfaceType = interfaceType;
1663 map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1664 FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1665 MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1666 for (int i = 0; i < map.TargetMethods.Length; i++)
1668 if (map.TargetMethods[i] == null)
1670 // TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1671 for (int j = 0; j < methods.Length; j++)
1673 if (methods[j].Name == map.InterfaceMethods[i].Name
1674 && methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1676 map.TargetMethods[i] = methods[j];
1681 for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1683 baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1686 map.TargetType = this;
1690 internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1692 __MethodImplMap impl = __GetMethodImplMap();
1693 for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1695 for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1697 int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1698 if (index != -1 && targetMethods[index] == null)
1700 targetMethods[index] = impl.MethodBodies[i];
1706 Type IGenericContext.GetGenericTypeArgument(int index)
1708 return GetGenericTypeArgument(index);
1711 Type IGenericContext.GetGenericMethodArgument(int index)
1713 throw new BadImageFormatException();
1716 Type IGenericBinder.BindTypeParameter(Type type)
1718 return GetGenericTypeArgument(type.GenericParameterPosition);
1721 Type IGenericBinder.BindMethodParameter(Type type)
1723 throw new BadImageFormatException();
1726 internal virtual Type BindTypeParameters(IGenericBinder binder)
1728 if (IsGenericTypeDefinition)
1730 Type[] args = GetGenericArguments();
1731 Type.InplaceBindTypeParameters(binder, args);
1732 return GenericTypeInstance.Make(this, args, null);
1740 private static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1742 for (int i = 0; i < types.Length; i++)
1744 types[i] = types[i].BindTypeParameters(binder);
1748 internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1750 foreach (MethodBase method in __GetDeclaredMethods())
1752 if (method.Name == name && method.MethodSignature.Equals(signature))
1760 internal virtual FieldInfo FindField(string name, FieldSignature signature)
1762 foreach (FieldInfo field in __GetDeclaredFields())
1764 if (field.Name == name && field.FieldSignature.Equals(signature))
1772 internal bool IsAllowMultipleCustomAttribute
1776 IList<CustomAttributeData> cad = CustomAttributeData.__GetCustomAttributes(this, this.Module.universe.System_AttributeUsageAttribute, false);
1779 foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1781 if (arg.MemberInfo.Name == "AllowMultiple")
1783 return (bool)arg.TypedValue.Value;
1791 internal bool IsPseudoCustomAttribute
1795 Universe u = this.Module.universe;
1796 return this == u.System_NonSerializedAttribute
1797 || this == u.System_SerializableAttribute
1798 || this == u.System_Runtime_InteropServices_DllImportAttribute
1799 || this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1800 || this == u.System_Runtime_InteropServices_InAttribute
1801 || this == u.System_Runtime_InteropServices_MarshalAsAttribute
1802 || this == u.System_Runtime_InteropServices_OutAttribute
1803 || this == u.System_Runtime_InteropServices_StructLayoutAttribute
1804 || this == u.System_Runtime_InteropServices_OptionalAttribute
1805 || this == u.System_Runtime_InteropServices_PreserveSigAttribute
1806 || this == u.System_Runtime_InteropServices_ComImportAttribute
1807 || this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1808 || this == u.System_Runtime_CompilerServices_MethodImplAttribute
1813 internal Type MarkNotValueType()
1815 typeFlags |= TypeFlags.NotValueType;
1819 internal Type MarkValueType()
1821 typeFlags |= TypeFlags.ValueType;
1825 internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
1827 Universe u = this.Module.universe;
1828 MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, new PackedCustomModifiers(), CallingConventions.Standard | CallingConventions.HasThis, 0);
1830 FindMethod(".ctor", methodSig) ??
1831 u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
1832 return (ConstructorInfo)mb;
1835 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1837 return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length));
1840 private MethodBase CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
1842 MethodSignature sig = new MethodSignature(
1843 returnType ?? this.Module.universe.System_Void,
1844 Util.Copy(parameterTypes),
1848 MethodInfo method = new MissingMethod(this, name, sig);
1849 if (name == ".ctor" || name == ".cctor")
1851 return new ConstructorInfoImpl(method);
1856 [Obsolete("Please use __CreateMissingMethod(string, CallingConventions, Type, CustomModifiers, Type[], CustomModifiers[]) instead")]
1857 public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
1859 return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length));
1862 public FieldInfo __CreateMissingField(string name, Type fieldType, CustomModifiers customModifiers)
1864 return new MissingField(this, name, FieldSignature.Create(fieldType, customModifiers));
1867 [Obsolete("Please use __CreateMissingField(string, Type, CustomModifiers) instead")]
1868 public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1870 return __CreateMissingField(name, fieldType, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1873 public PropertyInfo __CreateMissingProperty(string name, CallingConventions callingConvention, Type propertyType, CustomModifiers propertyTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1875 PropertySignature sig = PropertySignature.Create(callingConvention,
1878 PackedCustomModifiers.CreateFromExternal(propertyTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
1879 return new MissingProperty(this, name, sig);
1882 internal virtual Type SetMetadataTokenForMissing(int token)
1887 protected void MarkEnumOrValueType(string typeNamespace, string typeName)
1889 // we assume that mscorlib won't have nested types with these names,
1890 // so we don't check that we're not a nested type
1891 if (typeNamespace == "System"
1892 && (typeName == "Enum" || typeName == "ValueType"))
1894 typeFlags |= TypeFlags.PotentialEnumOrValueType;
1898 private bool ResolvePotentialEnumOrValueType()
1900 if (this.Assembly == this.Universe.Mscorlib
1901 || this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)
1902 // check if mscorlib forwards the type (.NETCore profile reference mscorlib forwards System.Enum and System.ValueType to System.Runtime.dll)
1903 || this.Universe.Mscorlib.FindType(new TypeName(__Namespace, __Name)) == this)
1905 typeFlags = (typeFlags & ~TypeFlags.PotentialEnumOrValueType) | TypeFlags.EnumOrValueType;
1910 typeFlags &= ~TypeFlags.PotentialEnumOrValueType;
1915 private bool IsEnumOrValueType
1919 return (typeFlags & (TypeFlags.EnumOrValueType | TypeFlags.PotentialEnumOrValueType)) != 0
1920 && ((typeFlags & TypeFlags.EnumOrValueType) != 0 || ResolvePotentialEnumOrValueType());
1924 internal virtual Universe Universe
1926 get { return Module.universe; }
1929 internal sealed override bool BindingFlagsMatch(BindingFlags flags)
1931 return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic);
1934 internal sealed override MemberInfo SetReflectedType(Type type)
1936 throw new InvalidOperationException();
1939 internal override int GetCurrentToken()
1941 return this.MetadataToken;
1944 internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
1946 // types don't have pseudo custom attributes
1951 abstract class ElementHolderType : Type
1953 protected readonly Type elementType;
1955 private readonly CustomModifiers mods;
1957 protected ElementHolderType(Type elementType, CustomModifiers mods)
1959 this.elementType = elementType;
1963 protected bool EqualsHelper(ElementHolderType other)
1965 return other != null
1966 && other.elementType.Equals(elementType)
1967 && other.mods.Equals(mods);
1970 public override CustomModifiers __GetCustomModifiers()
1975 public sealed override string Name
1977 get { return elementType.Name + GetSuffix(); }
1980 public sealed override string Namespace
1982 get { return elementType.Namespace; }
1985 public sealed override string FullName
1987 get { return elementType.FullName + GetSuffix(); }
1990 public sealed override string ToString()
1992 return elementType.ToString() + GetSuffix();
1995 public sealed override Type GetElementType()
2000 public sealed override bool HasElementType
2002 get { return true; }
2005 public sealed override Module Module
2007 get { return elementType.Module; }
2010 internal sealed override int GetModuleBuilderToken()
2014 token = ((ModuleBuilder)elementType.Module).ImportType(this);
2019 public sealed override bool ContainsGenericParameters
2023 Type type = elementType;
2024 while (type.HasElementType)
2026 type = type.GetElementType();
2028 return type.ContainsGenericParameters;
2032 public sealed override bool __ContainsMissingType
2036 Type type = elementType;
2037 while (type.HasElementType)
2039 type = type.GetElementType();
2041 return type.__ContainsMissingType;
2045 internal sealed override Type BindTypeParameters(IGenericBinder binder)
2047 Type type = elementType.BindTypeParameters(binder);
2048 CustomModifiers mods = this.mods.Bind(binder);
2049 if (ReferenceEquals(type, elementType)
2050 && mods.Equals(this.mods))
2054 return Wrap(type, mods);
2057 internal override void CheckBaked()
2059 elementType.CheckBaked();
2062 internal sealed override Universe Universe
2064 get { return elementType.Universe; }
2067 internal sealed override bool IsBaked
2069 get { return elementType.IsBaked; }
2072 internal sealed override int GetCurrentToken()
2074 // we don't have a token, so we return 0 (which is never a valid token)
2078 internal abstract string GetSuffix();
2080 protected abstract Type Wrap(Type type, CustomModifiers mods);
2083 sealed class ArrayType : ElementHolderType
2085 internal static Type Make(Type type, CustomModifiers mods)
2087 return type.Universe.CanonicalizeType(new ArrayType(type, mods));
2090 private ArrayType(Type type, CustomModifiers mods)
2095 public override Type BaseType
2097 get { return elementType.Module.universe.System_Array; }
2100 public override Type[] __GetDeclaredInterfaces()
2103 this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
2104 this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
2105 this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
2109 public override MethodBase[] __GetDeclaredMethods()
2111 Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
2112 List<MethodBase> list = new List<MethodBase>();
2113 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 }));
2114 list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
2115 list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
2116 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2117 for (Type type = elementType; type.__IsVector; type = type.GetElementType())
2119 Array.Resize(ref int32, int32.Length + 1);
2120 int32[int32.Length - 1] = int32[0];
2121 list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2123 return list.ToArray();
2126 public override TypeAttributes Attributes
2128 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2131 public override bool IsArray
2133 get { return true; }
2136 public override bool __IsVector
2138 get { return true; }
2141 public override int GetArrayRank()
2146 public override bool Equals(object o)
2148 return EqualsHelper(o as ArrayType);
2151 public override int GetHashCode()
2153 return elementType.GetHashCode() * 5;
2156 internal override string GetSuffix()
2161 protected override Type Wrap(Type type, CustomModifiers mods)
2163 return Make(type, mods);
2167 sealed class MultiArrayType : ElementHolderType
2169 private readonly int rank;
2170 private readonly int[] sizes;
2171 private readonly int[] lobounds;
2173 internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2175 return type.Universe.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, mods));
2178 private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2183 this.lobounds = lobounds;
2186 public override Type BaseType
2188 get { return elementType.Module.universe.System_Array; }
2191 public override MethodBase[] __GetDeclaredMethods()
2193 Type int32 = this.Module.universe.System_Int32;
2194 Type[] setArgs = new Type[rank + 1];
2195 Type[] getArgs = new Type[rank];
2196 Type[] ctorArgs = new Type[rank * 2];
2197 for (int i = 0; i < rank; i++)
2201 ctorArgs[i * 2 + 0] = int32;
2202 ctorArgs[i * 2 + 1] = int32;
2204 setArgs[rank] = elementType;
2205 return new MethodBase[] {
2206 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
2207 new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
2208 new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
2209 new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
2210 new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
2214 public override TypeAttributes Attributes
2216 get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2219 public override bool IsArray
2221 get { return true; }
2224 public override int GetArrayRank()
2229 public override int[] __GetArraySizes()
2231 return Util.Copy(sizes);
2234 public override int[] __GetArrayLowerBounds()
2236 return Util.Copy(lobounds);
2239 public override bool Equals(object o)
2241 MultiArrayType at = o as MultiArrayType;
2242 return EqualsHelper(at)
2244 && ArrayEquals(at.sizes, sizes)
2245 && ArrayEquals(at.lobounds, lobounds);
2248 private static bool ArrayEquals(int[] i1, int[] i2)
2250 if (i1.Length == i2.Length)
2252 for (int i = 0; i < i1.Length; i++)
2264 public override int GetHashCode()
2266 return elementType.GetHashCode() * 9 + rank;
2269 internal override string GetSuffix()
2277 return "[" + new String(',', rank - 1) + "]";
2281 protected override Type Wrap(Type type, CustomModifiers mods)
2283 return Make(type, rank, sizes, lobounds, mods);
2287 sealed class BuiltinArrayMethod : ArrayMethod
2289 internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
2290 : base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
2294 public override MethodAttributes Attributes
2296 get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
2299 public override MethodImplAttributes GetMethodImplementationFlags()
2301 return MethodImplAttributes.IL;
2304 public override int MetadataToken
2306 get { return 0x06000000; }
2309 public override MethodBody GetMethodBody()
2314 public override ParameterInfo[] GetParameters()
2316 ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
2317 for (int i = 0; i < parameterInfos.Length; i++)
2319 parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
2321 return parameterInfos;
2324 public override ParameterInfo ReturnParameter
2326 get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
2329 private sealed class ParameterInfoImpl : ParameterInfo
2331 private readonly MethodInfo method;
2332 private readonly Type type;
2333 private readonly int pos;
2335 internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
2337 this.method = method;
2342 public override Type ParameterType
2344 get { return type; }
2347 public override string Name
2349 get { return null; }
2352 public override ParameterAttributes Attributes
2354 get { return ParameterAttributes.None; }
2357 public override int Position
2362 public override object RawDefaultValue
2364 get { return null; }
2367 public override CustomModifiers __GetCustomModifiers()
2369 return new CustomModifiers();
2372 public override bool __TryGetFieldMarshal(out FieldMarshal fieldMarshal)
2374 fieldMarshal = new FieldMarshal();
2378 public override MemberInfo Member
2380 get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2383 public override int MetadataToken
2385 get { return 0x08000000; }
2388 internal override Module Module
2390 get { return method.Module; }
2395 sealed class ByRefType : ElementHolderType
2397 internal static Type Make(Type type, CustomModifiers mods)
2399 return type.Universe.CanonicalizeType(new ByRefType(type, mods));
2402 private ByRefType(Type type, CustomModifiers mods)
2407 public override bool Equals(object o)
2409 return EqualsHelper(o as ByRefType);
2412 public override int GetHashCode()
2414 return elementType.GetHashCode() * 3;
2417 public override Type BaseType
2419 get { return null; }
2422 public override TypeAttributes Attributes
2427 public override bool IsByRef
2429 get { return true; }
2432 internal override string GetSuffix()
2437 protected override Type Wrap(Type type, CustomModifiers mods)
2439 return Make(type, mods);
2443 sealed class PointerType : ElementHolderType
2445 internal static Type Make(Type type, CustomModifiers mods)
2447 return type.Universe.CanonicalizeType(new PointerType(type, mods));
2450 private PointerType(Type type, CustomModifiers mods)
2455 public override bool Equals(object o)
2457 return EqualsHelper(o as PointerType);
2460 public override int GetHashCode()
2462 return elementType.GetHashCode() * 7;
2465 public override Type BaseType
2467 get { return null; }
2470 public override TypeAttributes Attributes
2475 public override bool IsPointer
2477 get { return true; }
2480 internal override string GetSuffix()
2485 protected override Type Wrap(Type type, CustomModifiers mods)
2487 return Make(type, mods);
2491 sealed class GenericTypeInstance : Type
2493 private readonly Type type;
2494 private readonly Type[] args;
2495 private readonly CustomModifiers[] mods;
2496 private Type baseType;
2499 internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
2501 bool identity = true;
2502 if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2504 // a TypeBuiler identity must be instantiated
2509 // we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2510 for (int i = 0; i < typeArguments.Length; i++)
2512 if (typeArguments[i] != type.GetGenericTypeArgument(i)
2513 || !IsEmpty(mods, i))
2526 return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
2530 private static bool IsEmpty(CustomModifiers[] mods, int i)
2532 // we need to be extra careful, because mods doesn't not need to be in canonical format
2533 // (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2534 return mods == null || mods[i].IsEmpty;
2537 private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
2544 public override bool Equals(object o)
2546 GenericTypeInstance gt = o as GenericTypeInstance;
2547 return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2548 && Util.ArrayEquals(gt.mods, mods);
2551 public override int GetHashCode()
2553 return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2556 public override string AssemblyQualifiedName
2560 string fn = FullName;
2561 return fn == null ? null : fn + ", " + type.Assembly.FullName;
2565 public override Type BaseType
2569 if (baseType == null)
2571 Type rawBaseType = type.BaseType;
2572 if (rawBaseType == null)
2574 baseType = rawBaseType;
2578 baseType = rawBaseType.BindTypeParameters(this);
2585 public override bool IsValueType
2587 get { return type.IsValueType; }
2590 public override bool IsVisible
2596 foreach (Type arg in args)
2609 public override Type DeclaringType
2611 get { return type.DeclaringType; }
2614 public override TypeAttributes Attributes
2616 get { return type.Attributes; }
2619 internal override void CheckBaked()
2624 public override FieldInfo[] __GetDeclaredFields()
2626 FieldInfo[] fields = type.__GetDeclaredFields();
2627 for (int i = 0; i < fields.Length; i++)
2629 fields[i] = fields[i].BindTypeParameters(this);
2634 public override Type[] __GetDeclaredInterfaces()
2636 Type[] interfaces = type.__GetDeclaredInterfaces();
2637 for (int i = 0; i < interfaces.Length; i++)
2639 interfaces[i] = interfaces[i].BindTypeParameters(this);
2644 public override MethodBase[] __GetDeclaredMethods()
2646 MethodBase[] methods = type.__GetDeclaredMethods();
2647 for (int i = 0; i < methods.Length; i++)
2649 methods[i] = methods[i].BindTypeParameters(this);
2654 public override Type[] __GetDeclaredTypes()
2656 return type.__GetDeclaredTypes();
2659 public override EventInfo[] __GetDeclaredEvents()
2661 EventInfo[] events = type.__GetDeclaredEvents();
2662 for (int i = 0; i < events.Length; i++)
2664 events[i] = events[i].BindTypeParameters(this);
2669 public override PropertyInfo[] __GetDeclaredProperties()
2671 PropertyInfo[] properties = type.__GetDeclaredProperties();
2672 for (int i = 0; i < properties.Length; i++)
2674 properties[i] = properties[i].BindTypeParameters(this);
2679 public override __MethodImplMap __GetMethodImplMap()
2681 __MethodImplMap map = type.__GetMethodImplMap();
2682 map.TargetType = this;
2683 for (int i = 0; i < map.MethodBodies.Length; i++)
2685 map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2686 for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2688 Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2689 if (interfaceType.IsGenericType)
2691 map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2698 public override string Namespace
2700 get { return type.Namespace; }
2703 public override string Name
2705 get { return type.Name; }
2708 public override string FullName
2712 if (!this.__ContainsMissingType && this.ContainsGenericParameters)
2716 StringBuilder sb = new StringBuilder(this.type.FullName);
2719 foreach (Type type in args)
2721 sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
2725 return sb.ToString();
2729 public override string ToString()
2731 StringBuilder sb = new StringBuilder(type.FullName);
2734 foreach (Type arg in args)
2741 return sb.ToString();
2744 public override Module Module
2746 get { return type.Module; }
2749 public override bool IsGenericType
2751 get { return true; }
2754 public override bool IsConstructedGenericType
2756 get { return true; }
2759 public override Type GetGenericTypeDefinition()
2764 public override Type[] GetGenericArguments()
2766 return Util.Copy(args);
2769 public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
2771 return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
2774 internal override Type GetGenericTypeArgument(int index)
2779 public override bool ContainsGenericParameters
2783 foreach (Type type in args)
2785 if (type.ContainsGenericParameters)
2794 public override bool __ContainsMissingType
2798 foreach (Type type in args)
2800 if (type.__ContainsMissingType)
2805 return this.type.__IsMissing;
2809 public override StructLayoutAttribute StructLayoutAttribute
2811 get { return type.StructLayoutAttribute; }
2814 internal override int GetModuleBuilderToken()
2818 token = ((ModuleBuilder)type.Module).ImportType(this);
2823 internal override Type BindTypeParameters(IGenericBinder binder)
2825 for (int i = 0; i < args.Length; i++)
2827 Type xarg = args[i].BindTypeParameters(binder);
2828 if (!ReferenceEquals(xarg, args[i]))
2830 Type[] xargs = new Type[args.Length];
2831 Array.Copy(args, xargs, i);
2833 for (; i < args.Length; i++)
2835 xargs[i] = args[i].BindTypeParameters(binder);
2837 return Make(type, xargs, null);
2843 internal override int GetCurrentToken()
2845 return type.GetCurrentToken();
2848 internal override bool IsBaked
2850 get { return type.IsBaked; }
2854 sealed class FunctionPointerType : Type
2856 private readonly Universe universe;
2857 private readonly __StandAloneMethodSig sig;
2859 internal static Type Make(Universe universe, __StandAloneMethodSig sig)
2861 return universe.CanonicalizeType(new FunctionPointerType(universe, sig));
2864 private FunctionPointerType(Universe universe, __StandAloneMethodSig sig)
2866 this.universe = universe;
2870 public override bool Equals(object obj)
2872 FunctionPointerType other = obj as FunctionPointerType;
2873 return other != null
2874 && other.universe == universe
2875 && other.sig.Equals(sig);
2878 public override int GetHashCode()
2880 return sig.GetHashCode();
2883 public override bool __IsFunctionPointer
2885 get { return true; }
2888 public override __StandAloneMethodSig __MethodSignature
2893 public override Type BaseType
2895 get { return null; }
2898 public override TypeAttributes Attributes
2903 public override string Name
2905 get { throw new InvalidOperationException(); }
2908 public override string FullName
2910 get { throw new InvalidOperationException(); }
2913 public override Module Module
2915 get { throw new InvalidOperationException(); }
2918 internal override Universe Universe
2920 get { return universe; }
2923 public override string ToString()
2925 return "<FunctionPtr>";
2928 internal override bool IsBaked
2930 get { return true; }
2934 sealed class MarkerType : Type
2936 // used by ILGenerator
2937 internal static readonly Type Fault = new MarkerType();
2938 internal static readonly Type Finally = new MarkerType();
2939 internal static readonly Type Filter = new MarkerType();
2940 // used by CustomModifiers and SignatureHelper
2941 internal static readonly Type ModOpt = new MarkerType();
2942 internal static readonly Type ModReq = new MarkerType();
2943 // used by SignatureHelper
2944 internal static readonly Type Sentinel = new MarkerType();
2945 internal static readonly Type Pinned = new MarkerType();
2947 private MarkerType() { }
2949 public override Type BaseType
2951 get { throw new InvalidOperationException(); }
2954 public override TypeAttributes Attributes
2956 get { throw new InvalidOperationException(); }
2959 public override string Name
2961 get { throw new InvalidOperationException(); }
2964 public override string FullName
2966 get { throw new InvalidOperationException(); }
2969 public override Module Module
2971 get { throw new InvalidOperationException(); }
2974 internal override bool IsBaked
2976 get { throw new InvalidOperationException(); }