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 or TypeDefImpl
54
IsGenericTypeDefinition = 1,
56
// for use by TypeBuilder
60
// for use by MissingType
64
// for use by TypeDefImpl, TypeBuilder or MissingType
65
PotentialEnumOrValueType = 32,
68
// for use by TypeDefImpl
69
NotGenericTypeDefinition = 128,
72
// prevent subclassing by outsiders
75
this.underlyingType = this;
78
internal Type(Type underlyingType)
80
System.Diagnostics.Debug.Assert(underlyingType.underlyingType == underlyingType);
81
this.underlyingType = underlyingType;
82
this.typeFlags = underlyingType.typeFlags;
85
public static Binder DefaultBinder
87
get { return new DefaultBinder(); }
90
public sealed override MemberTypes MemberType
92
get { return IsNested ? MemberTypes.NestedType : MemberTypes.TypeInfo; }
95
public virtual string AssemblyQualifiedName
97
// NOTE the assembly name is not escaped here, only when used in a generic type instantiation
98
get { return this.FullName + ", " + this.Assembly.FullName; }
101
public abstract Type BaseType
106
public abstract TypeAttributes Attributes
111
public virtual Type GetElementType()
116
internal virtual void CheckBaked()
120
public virtual Type[] __GetDeclaredTypes()
122
return Type.EmptyTypes;
125
public virtual Type[] __GetDeclaredInterfaces()
127
return Type.EmptyTypes;
130
public virtual MethodBase[] __GetDeclaredMethods()
132
return Empty<MethodBase>.Array;
135
public virtual __MethodImplMap __GetMethodImplMap()
137
throw new NotSupportedException();
140
public virtual FieldInfo[] __GetDeclaredFields()
142
return Empty<FieldInfo>.Array;
145
public virtual EventInfo[] __GetDeclaredEvents()
147
return Empty<EventInfo>.Array;
150
public virtual PropertyInfo[] __GetDeclaredProperties()
152
return Empty<PropertyInfo>.Array;
155
public virtual CustomModifiers __GetCustomModifiers()
157
return new CustomModifiers();
160
[Obsolete("Please use __GetCustomModifiers() instead.")]
161
public Type[] __GetRequiredCustomModifiers()
163
return __GetCustomModifiers().GetRequired();
166
[Obsolete("Please use __GetCustomModifiers() instead.")]
167
public Type[] __GetOptionalCustomModifiers()
169
return __GetCustomModifiers().GetOptional();
172
public virtual __StandAloneMethodSig __MethodSignature
174
get { throw new InvalidOperationException(); }
177
public virtual bool HasElementType
179
get { return false; }
182
public virtual bool IsArray
184
get { return false; }
187
public virtual bool __IsVector
189
get { return false; }
192
public virtual bool IsByRef
194
get { return false; }
197
public virtual bool IsPointer
199
get { return false; }
202
public virtual bool __IsFunctionPointer
204
get { return false; }
207
public virtual bool IsValueType
211
Type baseType = this.BaseType;
212
return baseType != null
213
&& baseType.IsEnumOrValueType
214
&& !this.IsEnumOrValueType;
218
public virtual bool IsGenericParameter
220
get { return false; }
223
public virtual int GenericParameterPosition
225
get { throw new NotSupportedException(); }
228
public virtual MethodBase DeclaringMethod
233
public Type UnderlyingSystemType
235
get { return underlyingType; }
238
public override Type DeclaringType
243
public virtual string __Name
245
get { throw new InvalidOperationException(); }
248
public virtual string __Namespace
250
get { throw new InvalidOperationException(); }
253
public abstract override string Name
258
public virtual string Namespace
264
return DeclaringType.Namespace;
270
internal virtual int GetModuleBuilderToken()
272
throw new InvalidOperationException();
275
public static bool operator ==(Type t1, Type t2)
277
// Casting to object results in smaller code than calling ReferenceEquals and makes
278
// this method more likely to be inlined.
279
// On CLR v2 x86, microbenchmarks show this to be faster than calling ReferenceEquals.
280
return (object)t1 == (object)t2
281
|| ((object)t1 != null && (object)t2 != null && (object)t1.underlyingType == (object)t2.underlyingType);
284
public static bool operator !=(Type t1, Type t2)
289
public bool Equals(Type type)
294
public override bool Equals(object obj)
296
return Equals(obj as Type);
299
public override int GetHashCode()
301
Type type = this.UnderlyingSystemType;
302
return ReferenceEquals(type, this) ? base.GetHashCode() : type.GetHashCode();
305
public Type[] GenericTypeArguments
307
get { return IsConstructedGenericType ? GetGenericArguments() : Type.EmptyTypes; }
310
public virtual Type[] GetGenericArguments()
312
return Type.EmptyTypes;
315
public virtual CustomModifiers[] __GetGenericArgumentsCustomModifiers()
317
return Empty<CustomModifiers>.Array;
320
[Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
321
public Type[][] __GetGenericArgumentsRequiredCustomModifiers()
323
CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
324
Type[][] array = new Type[customModifiers.Length][];
325
for (int i = 0; i < array.Length; i++)
327
array[i] = customModifiers[i].GetRequired();
332
[Obsolete("Please use __GetGenericArgumentsCustomModifiers() instead")]
333
public Type[][] __GetGenericArgumentsOptionalCustomModifiers()
335
CustomModifiers[] customModifiers = __GetGenericArgumentsCustomModifiers();
336
Type[][] array = new Type[customModifiers.Length][];
337
for (int i = 0; i < array.Length; i++)
339
array[i] = customModifiers[i].GetOptional();
344
public virtual Type GetGenericTypeDefinition()
346
throw new InvalidOperationException();
349
public virtual StructLayoutAttribute StructLayoutAttribute
354
public virtual bool __GetLayout(out int packingSize, out int typeSize)
361
public virtual bool IsGenericType
363
get { return false; }
366
public virtual bool IsGenericTypeDefinition
368
get { return false; }
372
public virtual bool IsConstructedGenericType
374
get { return false; }
377
public virtual bool ContainsGenericParameters
381
if (this.IsGenericParameter)
385
foreach (Type arg in this.GetGenericArguments())
387
if (arg.ContainsGenericParameters)
396
public virtual Type[] GetGenericParameterConstraints()
398
throw new InvalidOperationException();
401
public virtual GenericParameterAttributes GenericParameterAttributes
403
get { throw new InvalidOperationException(); }
406
public virtual int GetArrayRank()
408
throw new NotSupportedException();
411
public virtual int[] __GetArraySizes()
413
throw new NotSupportedException();
416
public virtual int[] __GetArrayLowerBounds()
418
throw new NotSupportedException();
422
public virtual Type GetEnumUnderlyingType()
426
throw new ArgumentException();
429
return GetEnumUnderlyingTypeImpl();
432
internal Type GetEnumUnderlyingTypeImpl()
434
foreach (FieldInfo field in __GetDeclaredFields())
438
// the CLR assumes that an enum has only one instance field, so we can do the same
439
return field.FieldType;
442
throw new InvalidOperationException();
445
public string[] GetEnumNames()
449
throw new ArgumentException();
451
List<string> names = new List<string>();
452
foreach (FieldInfo field in __GetDeclaredFields())
456
names.Add(field.Name);
459
return names.ToArray();
462
public string GetEnumName(object value)
466
throw new ArgumentException();
470
throw new ArgumentNullException();
474
value = Convert.ChangeType(value, GetTypeCode(GetEnumUnderlyingType()));
476
catch (FormatException)
478
throw new ArgumentException();
480
catch (OverflowException)
484
catch (InvalidCastException)
488
foreach (FieldInfo field in __GetDeclaredFields())
490
if (field.IsLiteral && field.GetRawConstantValue().Equals(value))
498
public bool IsEnumDefined(object value)
502
return Array.IndexOf(GetEnumNames(), value) != -1;
506
throw new ArgumentException();
510
throw new ArgumentNullException();
512
if (System.Type.GetTypeCode(value.GetType()) != GetTypeCode(GetEnumUnderlyingType()))
514
throw new ArgumentException();
516
foreach (FieldInfo field in __GetDeclaredFields())
518
if (field.IsLiteral && field.GetRawConstantValue().Equals(value))
526
public override string ToString()
531
public abstract string FullName
536
protected string GetFullName()
538
string ns = TypeNameParser.Escape(this.__Namespace);
539
Type decl = this.DeclaringType;
548
return ns + "." + this.Name;
555
return decl.FullName + "+" + this.Name;
559
return decl.FullName + "+" + ns + "." + this.Name;
564
internal virtual bool IsModulePseudoType
566
get { return false; }
569
internal virtual Type GetGenericTypeArgument(int index)
571
throw new InvalidOperationException();
574
public MemberInfo[] GetDefaultMembers()
576
Type defaultMemberAttribute = this.Module.universe.Import(typeof(System.Reflection.DefaultMemberAttribute));
577
foreach (CustomAttributeData cad in CustomAttributeData.GetCustomAttributes(this))
579
if (cad.Constructor.DeclaringType.Equals(defaultMemberAttribute))
581
return GetMember((string)cad.ConstructorArguments[0].Value);
584
return Empty<MemberInfo>.Array;
587
public MemberInfo[] GetMember(string name)
589
return GetMember(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
592
public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
594
return GetMember(name, MemberTypes.All, bindingAttr);
597
public MemberInfo[] GetMembers()
599
return GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
602
public MemberInfo[] GetMembers(BindingFlags bindingAttr)
604
List<MemberInfo> members = new List<MemberInfo>();
605
members.AddRange(GetConstructors(bindingAttr));
606
members.AddRange(GetMethods(bindingAttr));
607
members.AddRange(GetFields(bindingAttr));
608
members.AddRange(GetProperties(bindingAttr));
609
members.AddRange(GetEvents(bindingAttr));
610
members.AddRange(GetNestedTypes(bindingAttr));
611
return members.ToArray();
614
public MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
617
if ((bindingAttr & BindingFlags.IgnoreCase) != 0)
619
name = name.ToLowerInvariant();
620
filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.ToLowerInvariant().Equals(filterCriteria); };
624
filter = delegate(MemberInfo member, object filterCriteria) { return member.Name.Equals(filterCriteria); };
626
return FindMembers(type, bindingAttr, filter, name);
629
private static void AddMembers(List<MemberInfo> list, MemberFilter filter, object filterCriteria, MemberInfo[] members)
631
foreach (MemberInfo member in members)
633
if (filter == null || filter(member, filterCriteria))
640
public MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter filter, object filterCriteria)
642
List<MemberInfo> members = new List<MemberInfo>();
643
if ((memberType & MemberTypes.Constructor) != 0)
645
AddMembers(members, filter, filterCriteria, GetConstructors(bindingAttr));
647
if ((memberType & MemberTypes.Method) != 0)
649
AddMembers(members, filter, filterCriteria, GetMethods(bindingAttr));
651
if ((memberType & MemberTypes.Field) != 0)
653
AddMembers(members, filter, filterCriteria, GetFields(bindingAttr));
655
if ((memberType & MemberTypes.Property) != 0)
657
AddMembers(members, filter, filterCriteria, GetProperties(bindingAttr));
659
if ((memberType & MemberTypes.Event) != 0)
661
AddMembers(members, filter, filterCriteria, GetEvents(bindingAttr));
663
if ((memberType & MemberTypes.NestedType) != 0)
665
AddMembers(members, filter, filterCriteria, GetNestedTypes(bindingAttr));
667
return members.ToArray();
670
private MemberInfo[] GetMembers<T>()
672
if (typeof(T) == typeof(ConstructorInfo) || typeof(T) == typeof(MethodInfo))
674
return __GetDeclaredMethods();
676
else if (typeof(T) == typeof(FieldInfo))
678
return __GetDeclaredFields();
680
else if (typeof(T) == typeof(PropertyInfo))
682
return __GetDeclaredProperties();
684
else if (typeof(T) == typeof(EventInfo))
686
return __GetDeclaredEvents();
688
else if (typeof(T) == typeof(Type))
690
return __GetDeclaredTypes();
694
throw new InvalidOperationException();
698
private T[] GetMembers<T>(BindingFlags flags)
702
List<T> list = new List<T>();
703
foreach (MemberInfo member in GetMembers<T>())
705
if (member is T && member.BindingFlagsMatch(flags))
710
if ((flags & BindingFlags.DeclaredOnly) == 0)
712
for (Type type = this.BaseType; type != null; type = type.BaseType)
715
foreach (MemberInfo member in type.GetMembers<T>())
717
if (member is T && member.BindingFlagsMatchInherited(flags))
719
list.Add((T)member.SetReflectedType(this));
724
return list.ToArray();
727
private T GetMemberByName<T>(string name, BindingFlags flags, Predicate<T> filter)
731
if ((flags & BindingFlags.IgnoreCase) != 0)
733
name = name.ToLowerInvariant();
736
foreach (MemberInfo member in GetMembers<T>())
738
if (member is T && member.BindingFlagsMatch(flags))
740
string memberName = member.Name;
741
if ((flags & BindingFlags.IgnoreCase) != 0)
743
memberName = memberName.ToLowerInvariant();
745
if (memberName == name && (filter == null || filter((T)member)))
749
throw new AmbiguousMatchException();
755
if ((flags & BindingFlags.DeclaredOnly) == 0)
757
for (Type type = this.BaseType; (found == null || typeof(T) == typeof(MethodInfo)) && type != null; type = type.BaseType)
760
foreach (MemberInfo member in type.GetMembers<T>())
762
if (member is T && member.BindingFlagsMatchInherited(flags))
764
string memberName = member.Name;
765
if ((flags & BindingFlags.IgnoreCase) != 0)
767
memberName = memberName.ToLowerInvariant();
769
if (memberName == name && (filter == null || filter((T)member)))
774
// TODO does this depend on HideBySig vs HideByName?
775
if ((mi = found as MethodInfo) != null
776
&& mi.MethodSignature.MatchParameterTypes(((MethodBase)member).MethodSignature))
780
throw new AmbiguousMatchException();
782
found = (T)member.SetReflectedType(this);
791
private T GetMemberByName<T>(string name, BindingFlags flags)
794
return GetMemberByName<T>(name, flags, null);
797
public EventInfo GetEvent(string name)
799
return GetEvent(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
802
public EventInfo GetEvent(string name, BindingFlags bindingAttr)
804
return GetMemberByName<EventInfo>(name, bindingAttr);
807
public EventInfo[] GetEvents()
809
return GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
812
public EventInfo[] GetEvents(BindingFlags bindingAttr)
814
return GetMembers<EventInfo>(bindingAttr);
817
public FieldInfo GetField(string name)
819
return GetField(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
822
public FieldInfo GetField(string name, BindingFlags bindingAttr)
824
return GetMemberByName<FieldInfo>(name, bindingAttr);
827
public FieldInfo[] GetFields()
829
return GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
832
public FieldInfo[] GetFields(BindingFlags bindingAttr)
834
return GetMembers<FieldInfo>(bindingAttr);
837
public Type[] GetInterfaces()
839
List<Type> list = new List<Type>();
840
for (Type type = this; type != null; type = type.BaseType)
842
AddInterfaces(list, type);
844
return list.ToArray();
847
private static void AddInterfaces(List<Type> list, Type type)
849
foreach (Type iface in type.__GetDeclaredInterfaces())
851
if (!list.Contains(iface))
854
AddInterfaces(list, iface);
859
public MethodInfo[] GetMethods(BindingFlags bindingAttr)
862
List<MethodInfo> list = new List<MethodInfo>();
863
foreach (MethodBase mb in __GetDeclaredMethods())
865
MethodInfo mi = mb as MethodInfo;
866
if (mi != null && mi.BindingFlagsMatch(bindingAttr))
871
if ((bindingAttr & BindingFlags.DeclaredOnly) == 0)
873
List<MethodInfo> baseMethods = new List<MethodInfo>();
874
foreach (MethodInfo mi in list)
878
baseMethods.Add(mi.GetBaseDefinition());
881
for (Type type = this.BaseType; type != null; type = type.BaseType)
884
foreach (MethodBase mb in type.__GetDeclaredMethods())
886
MethodInfo mi = mb as MethodInfo;
887
if (mi != null && mi.BindingFlagsMatchInherited(bindingAttr))
891
if (baseMethods == null)
893
baseMethods = new List<MethodInfo>();
895
else if (baseMethods.Contains(mi.GetBaseDefinition()))
899
baseMethods.Add(mi.GetBaseDefinition());
901
list.Add((MethodInfo)mi.SetReflectedType(this));
906
return list.ToArray();
909
public MethodInfo[] GetMethods()
911
return GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
914
public MethodInfo GetMethod(string name)
916
return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
919
public MethodInfo GetMethod(string name, BindingFlags bindingAttr)
921
return GetMemberByName<MethodInfo>(name, bindingAttr);
924
public MethodInfo GetMethod(string name, Type[] types)
926
return GetMethod(name, types, null);
929
public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers)
931
return GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, types, modifiers);
934
public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
936
// first we try an exact match and only if that fails we fall back to using the binder
937
return GetMemberByName<MethodInfo>(name, bindingAttr,
938
delegate(MethodInfo method) { return method.MethodSignature.MatchParameterTypes(types); })
939
?? GetMethodWithBinder<MethodInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
942
private T GetMethodWithBinder<T>(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
945
List<MethodBase> list = new List<MethodBase>();
946
GetMemberByName<T>(name, bindingAttr, delegate(T method) {
950
return (T)binder.SelectMethod(bindingAttr, list.ToArray(), types, modifiers);
953
public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
955
// FXBUG callConvention seems to be ignored
956
return GetMethod(name, bindingAttr, binder, types, modifiers);
959
public ConstructorInfo[] GetConstructors()
961
return GetConstructors(BindingFlags.Public | BindingFlags.Instance);
964
public ConstructorInfo[] GetConstructors(BindingFlags bindingAttr)
966
return GetMembers<ConstructorInfo>(bindingAttr | BindingFlags.DeclaredOnly);
969
public ConstructorInfo GetConstructor(Type[] types)
971
return GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Standard, types, null);
974
public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
976
ConstructorInfo ci1 = null;
977
if ((bindingAttr & BindingFlags.Instance) != 0)
979
ci1 = GetConstructorImpl(ConstructorInfo.ConstructorName, bindingAttr, binder, types, modifiers);
981
if ((bindingAttr & BindingFlags.Static) != 0)
983
ConstructorInfo ci2 = GetConstructorImpl(ConstructorInfo.TypeConstructorName, bindingAttr, binder, types, modifiers);
988
throw new AmbiguousMatchException();
996
private ConstructorInfo GetConstructorImpl(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
998
// first we try an exact match and only if that fails we fall back to using the binder
999
return GetMemberByName<ConstructorInfo>(name, bindingAttr | BindingFlags.DeclaredOnly,
1000
delegate(ConstructorInfo ctor) { return ctor.MethodSignature.MatchParameterTypes(types); })
1001
?? GetMethodWithBinder<ConstructorInfo>(name, bindingAttr, binder ?? DefaultBinder, types, modifiers);
1004
public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, CallingConventions callingConvention, Type[] types, ParameterModifier[] modifiers)
1006
// FXBUG callConvention seems to be ignored
1007
return GetConstructor(bindingAttr, binder, types, modifiers);
1010
internal Type ResolveNestedType(TypeName typeName)
1012
return FindNestedType(typeName) ?? Module.universe.GetMissingTypeOrThrow(Module, this, typeName);
1015
// unlike the public API, this takes the namespace and name into account
1016
internal virtual Type FindNestedType(TypeName name)
1018
foreach (Type type in __GetDeclaredTypes())
1020
if (type.__Namespace == name.Namespace && type.__Name == name.Name)
1028
internal virtual Type FindNestedTypeIgnoreCase(TypeName lowerCaseName)
1030
foreach (Type type in __GetDeclaredTypes())
1032
if (new TypeName(type.__Namespace, type.__Name).ToLowerInvariant() == lowerCaseName)
1040
public Type GetNestedType(string name)
1042
return GetNestedType(name, BindingFlags.Public);
1045
public Type GetNestedType(string name, BindingFlags bindingAttr)
1047
// FXBUG the namespace is ignored, so we can use GetMemberByName
1048
return GetMemberByName<Type>(name, bindingAttr | BindingFlags.DeclaredOnly);
1051
public Type[] GetNestedTypes()
1053
return GetNestedTypes(BindingFlags.Public);
1056
public Type[] GetNestedTypes(BindingFlags bindingAttr)
1058
// FXBUG the namespace is ignored, so we can use GetMember
1059
return GetMembers<Type>(bindingAttr | BindingFlags.DeclaredOnly);
1062
public PropertyInfo[] GetProperties()
1064
return GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
1067
public PropertyInfo[] GetProperties(BindingFlags bindingAttr)
1069
return GetMembers<PropertyInfo>(bindingAttr);
1072
public PropertyInfo GetProperty(string name)
1074
return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
1077
public PropertyInfo GetProperty(string name, BindingFlags bindingAttr)
1079
return GetMemberByName<PropertyInfo>(name, bindingAttr);
1082
public PropertyInfo GetProperty(string name, Type returnType)
1084
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
1085
return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertyType.Equals(returnType); })
1086
?? GetPropertyWithBinder(name, flags, DefaultBinder, returnType, null, null);
1089
public PropertyInfo GetProperty(string name, Type[] types)
1091
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
1092
return GetMemberByName<PropertyInfo>(name, flags, delegate(PropertyInfo prop) { return prop.PropertySignature.MatchParameterTypes(types); })
1093
?? GetPropertyWithBinder(name, flags, DefaultBinder, null, types, null);
1096
public PropertyInfo GetProperty(string name, Type returnType, Type[] types)
1098
return GetProperty(name, returnType, types, null);
1101
public PropertyInfo GetProperty(string name, Type returnType, Type[] types, ParameterModifier[] modifiers)
1103
return GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, returnType, types, modifiers);
1106
public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1108
return GetMemberByName<PropertyInfo>(name, bindingAttr,
1109
delegate(PropertyInfo prop) {
1110
return prop.PropertyType.Equals(returnType) && prop.PropertySignature.MatchParameterTypes(types);
1112
?? GetPropertyWithBinder(name, bindingAttr, binder ?? DefaultBinder, returnType, types, modifiers);
1115
private PropertyInfo GetPropertyWithBinder(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
1117
List<PropertyInfo> list = new List<PropertyInfo>();
1118
GetMemberByName<PropertyInfo>(name, bindingAttr, delegate(PropertyInfo property) {
1122
return binder.SelectProperty(bindingAttr, list.ToArray(), returnType, types, modifiers);
1125
public Type GetInterface(string name)
1127
return GetInterface(name, false);
1130
public Type GetInterface(string name, bool ignoreCase)
1134
name = name.ToLowerInvariant();
1137
foreach (Type type in GetInterfaces())
1139
string typeName = type.FullName;
1142
typeName = typeName.ToLowerInvariant();
1144
if (typeName == name)
1148
throw new AmbiguousMatchException();
1156
public Type[] FindInterfaces(TypeFilter filter, object filterCriteria)
1158
List<Type> list = new List<Type>();
1159
foreach (Type type in GetInterfaces())
1161
if (filter(type, filterCriteria))
1166
return list.ToArray();
1169
public ConstructorInfo TypeInitializer
1171
get { return GetConstructor(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); }
1174
public bool IsPrimitive
1178
Universe u = this.Universe;
1179
return this == u.System_Boolean
1180
|| this == u.System_Byte
1181
|| this == u.System_SByte
1182
|| this == u.System_Int16
1183
|| this == u.System_UInt16
1184
|| this == u.System_Int32
1185
|| this == u.System_UInt32
1186
|| this == u.System_Int64
1187
|| this == u.System_UInt64
1188
|| this == u.System_IntPtr
1189
|| this == u.System_UIntPtr
1190
|| this == u.System_Char
1191
|| this == u.System_Double
1192
|| this == u.System_Single
1201
Type baseType = this.BaseType;
1202
return baseType != null
1203
&& baseType.IsEnumOrValueType
1204
&& baseType.__Name[0] == 'E';
1208
public bool IsSealed
1210
get { return (Attributes & TypeAttributes.Sealed) != 0; }
1213
public bool IsAbstract
1215
get { return (Attributes & TypeAttributes.Abstract) != 0; }
1218
private bool CheckVisibility(TypeAttributes access)
1220
return (Attributes & TypeAttributes.VisibilityMask) == access;
1223
public bool IsPublic
1225
get { return CheckVisibility(TypeAttributes.Public); }
1228
public bool IsNestedPublic
1230
get { return CheckVisibility(TypeAttributes.NestedPublic); }
1233
public bool IsNestedPrivate
1235
get { return CheckVisibility(TypeAttributes.NestedPrivate); }
1238
public bool IsNestedFamily
1240
get { return CheckVisibility(TypeAttributes.NestedFamily); }
1243
public bool IsNestedAssembly
1245
get { return CheckVisibility(TypeAttributes.NestedAssembly); }
1248
public bool IsNestedFamANDAssem
1250
get { return CheckVisibility(TypeAttributes.NestedFamANDAssem); }
1253
public bool IsNestedFamORAssem
1255
get { return CheckVisibility(TypeAttributes.NestedFamORAssem); }
1258
public bool IsNotPublic
1260
get { return CheckVisibility(TypeAttributes.NotPublic); }
1263
public bool IsImport
1265
get { return (Attributes & TypeAttributes.Import) != 0; }
1268
public bool IsCOMObject
1270
get { return IsClass && IsImport; }
1273
public bool IsContextful
1275
get { return IsSubclassOf(this.Module.universe.Import(typeof(ContextBoundObject))); }
1278
public bool IsMarshalByRef
1280
get { return IsSubclassOf(this.Module.universe.Import(typeof(MarshalByRefObject))); }
1283
public virtual bool IsVisible
1285
get { return IsPublic || (IsNestedPublic && this.DeclaringType.IsVisible); }
1288
public bool IsAnsiClass
1290
get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AnsiClass; }
1293
public bool IsUnicodeClass
1295
get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass; }
1298
public bool IsAutoClass
1300
get { return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass; }
1303
public bool IsAutoLayout
1305
get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout; }
1308
public bool IsLayoutSequential
1310
get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout; }
1313
public bool IsExplicitLayout
1315
get { return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout; }
1318
public bool IsSpecialName
1320
get { return (Attributes & TypeAttributes.SpecialName) != 0; }
1323
public bool IsSerializable
1325
get { return (Attributes & TypeAttributes.Serializable) != 0; }
1330
get { return !IsInterface && !IsValueType; }
1333
public bool IsInterface
1335
get { return (Attributes & TypeAttributes.Interface) != 0; }
1338
public bool IsNested
1340
// FXBUG we check the declaring type (like .NET) and this results
1341
// in IsNested returning true for a generic type parameter
1342
get { return this.DeclaringType != null; }
1345
public virtual bool __ContainsMissingType
1349
if (this.__IsMissing)
1353
foreach (Type arg in this.GetGenericArguments())
1355
if (arg.__ContainsMissingType)
1364
public Type MakeArrayType()
1366
return ArrayType.Make(this, new CustomModifiers());
1369
public Type __MakeArrayType(CustomModifiers customModifiers)
1371
return ArrayType.Make(this, customModifiers);
1374
[Obsolete("Please use __MakeArrayType(CustomModifiers) instead.")]
1375
public Type __MakeArrayType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1377
return __MakeArrayType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1380
public Type MakeArrayType(int rank)
1382
return __MakeArrayType(rank, new CustomModifiers());
1385
public Type __MakeArrayType(int rank, CustomModifiers customModifiers)
1387
return MultiArrayType.Make(this, rank, Empty<int>.Array, new int[rank], customModifiers);
1390
[Obsolete("Please use __MakeArrayType(int, CustomModifiers) instead.")]
1391
public Type __MakeArrayType(int rank, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1393
return __MakeArrayType(rank, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1396
public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, CustomModifiers customModifiers)
1398
return MultiArrayType.Make(this, rank, sizes ?? Empty<int>.Array, lobounds ?? Empty<int>.Array, customModifiers);
1401
[Obsolete("Please use __MakeArrayType(int, int[], int[], CustomModifiers) instead.")]
1402
public Type __MakeArrayType(int rank, int[] sizes, int[] lobounds, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1404
return __MakeArrayType(rank, sizes, lobounds, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1407
public Type MakeByRefType()
1409
return ByRefType.Make(this, new CustomModifiers());
1412
public Type __MakeByRefType(CustomModifiers customModifiers)
1414
return ByRefType.Make(this, customModifiers);
1417
[Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1418
public Type __MakeByRefType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1420
return __MakeByRefType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1423
public Type MakePointerType()
1425
return PointerType.Make(this, new CustomModifiers());
1428
public Type __MakePointerType(CustomModifiers customModifiers)
1430
return PointerType.Make(this, customModifiers);
1433
[Obsolete("Please use __MakeByRefType(CustomModifiers) instead.")]
1434
public Type __MakePointerType(Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1436
return __MakePointerType(CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1439
public Type MakeGenericType(params Type[] typeArguments)
1441
return __MakeGenericType(typeArguments, null);
1444
public Type __MakeGenericType(Type[] typeArguments, CustomModifiers[] customModifiers)
1446
if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1448
throw new InvalidOperationException();
1450
return GenericTypeInstance.Make(this, Util.Copy(typeArguments), customModifiers == null ? null : (CustomModifiers[])customModifiers.Clone());
1453
[Obsolete("Please use __MakeGenericType(Type[], CustomModifiers[]) instead.")]
1454
public Type __MakeGenericType(Type[] typeArguments, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers)
1456
if (!this.__IsMissing && !this.IsGenericTypeDefinition)
1458
throw new InvalidOperationException();
1460
CustomModifiers[] mods = null;
1461
if (requiredCustomModifiers != null || optionalCustomModifiers != null)
1463
mods = new CustomModifiers[typeArguments.Length];
1464
for (int i = 0; i < mods.Length; i++)
1466
mods[i] = CustomModifiers.FromReqOpt(Util.NullSafeElementAt(requiredCustomModifiers, i), Util.NullSafeElementAt(optionalCustomModifiers, i));
1469
return GenericTypeInstance.Make(this, Util.Copy(typeArguments), mods);
1472
public static System.Type __GetSystemType(TypeCode typeCode)
1476
case TypeCode.Boolean:
1477
return typeof(System.Boolean);
1479
return typeof(System.Byte);
1481
return typeof(System.Char);
1482
case TypeCode.DBNull:
1483
return typeof(System.DBNull);
1484
case TypeCode.DateTime:
1485
return typeof(System.DateTime);
1486
case TypeCode.Decimal:
1487
return typeof(System.Decimal);
1488
case TypeCode.Double:
1489
return typeof(System.Double);
1490
case TypeCode.Empty:
1492
case TypeCode.Int16:
1493
return typeof(System.Int16);
1494
case TypeCode.Int32:
1495
return typeof(System.Int32);
1496
case TypeCode.Int64:
1497
return typeof(System.Int64);
1498
case TypeCode.Object:
1499
return typeof(System.Object);
1500
case TypeCode.SByte:
1501
return typeof(System.SByte);
1502
case TypeCode.Single:
1503
return typeof(System.Single);
1504
case TypeCode.String:
1505
return typeof(System.String);
1506
case TypeCode.UInt16:
1507
return typeof(System.UInt16);
1508
case TypeCode.UInt32:
1509
return typeof(System.UInt32);
1510
case TypeCode.UInt64:
1511
return typeof(System.UInt64);
1513
throw new ArgumentOutOfRangeException();
1517
public static TypeCode GetTypeCode(Type type)
1521
return TypeCode.Empty;
1523
if (!type.__IsMissing && type.IsEnum)
1525
type = type.GetEnumUnderlyingType();
1527
Universe u = type.Module.universe;
1528
if (type == u.System_Boolean)
1530
return TypeCode.Boolean;
1532
else if (type == u.System_Char)
1534
return TypeCode.Char;
1536
else if (type == u.System_SByte)
1538
return TypeCode.SByte;
1540
else if (type == u.System_Byte)
1542
return TypeCode.Byte;
1544
else if (type == u.System_Int16)
1546
return TypeCode.Int16;
1548
else if (type == u.System_UInt16)
1550
return TypeCode.UInt16;
1552
else if (type == u.System_Int32)
1554
return TypeCode.Int32;
1556
else if (type == u.System_UInt32)
1558
return TypeCode.UInt32;
1560
else if (type == u.System_Int64)
1562
return TypeCode.Int64;
1564
else if (type == u.System_UInt64)
1566
return TypeCode.UInt64;
1568
else if (type == u.System_Single)
1570
return TypeCode.Single;
1572
else if (type == u.System_Double)
1574
return TypeCode.Double;
1576
else if (type == u.System_DateTime)
1578
return TypeCode.DateTime;
1580
else if (type == u.System_DBNull)
1582
return TypeCode.DBNull;
1584
else if (type == u.System_Decimal)
1586
return TypeCode.Decimal;
1588
else if (type == u.System_String)
1590
return TypeCode.String;
1592
else if (type.__IsMissing)
1594
throw new MissingMemberException(type);
1598
return TypeCode.Object;
1602
public Assembly Assembly
1604
get { return Module.Assembly; }
1607
public bool IsAssignableFrom(Type type)
1609
if (this.Equals(type))
1613
else if (type == null)
1617
else if (this.IsArray && type.IsArray)
1619
if (this.GetArrayRank() != type.GetArrayRank())
1623
else if (this.__IsVector && !type.__IsVector)
1627
Type e1 = this.GetElementType();
1628
Type e2 = type.GetElementType();
1629
return e1.IsValueType == e2.IsValueType && e1.IsAssignableFrom(e2);
1631
else if (this.IsCovariant(type))
1635
else if (this.IsSealed)
1639
else if (this.IsInterface)
1641
foreach (Type iface in type.GetInterfaces())
1643
if (this.Equals(iface) || this.IsCovariant(iface))
1650
else if (type.IsInterface)
1652
return this == this.Module.universe.System_Object;
1654
else if (type.IsPointer)
1656
return this == this.Module.universe.System_Object || this == this.Module.universe.System_ValueType;
1660
return type.IsSubclassOf(this);
1664
private bool IsCovariant(Type other)
1666
if (this.IsConstructedGenericType
1667
&& other.IsConstructedGenericType
1668
&& this.GetGenericTypeDefinition() == other.GetGenericTypeDefinition())
1670
Type[] typeParameters = GetGenericTypeDefinition().GetGenericArguments();
1671
for (int i = 0; i < typeParameters.Length; i++)
1673
Type t1 = this.GetGenericTypeArgument(i);
1674
Type t2 = other.GetGenericTypeArgument(i);
1675
if (t1.IsValueType != t2.IsValueType)
1679
switch (typeParameters[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask)
1681
case GenericParameterAttributes.Covariant:
1682
if (!t1.IsAssignableFrom(t2))
1687
case GenericParameterAttributes.Contravariant:
1688
if (!t2.IsAssignableFrom(t1))
1693
case GenericParameterAttributes.None:
1706
public bool IsSubclassOf(Type type)
1708
Type thisType = this.BaseType;
1709
while (thisType != null)
1711
if (thisType.Equals(type))
1715
thisType = thisType.BaseType;
1720
// This returns true if this type directly (i.e. not inherited from the base class) implements the interface.
1721
// Note that a complicating factor is that the interface itself can be implemented by an interface that extends it.
1722
private bool IsDirectlyImplementedInterface(Type interfaceType)
1724
foreach (Type iface in __GetDeclaredInterfaces())
1726
if (interfaceType.IsAssignableFrom(iface))
1734
public InterfaceMapping GetInterfaceMap(Type interfaceType)
1737
InterfaceMapping map = new InterfaceMapping();
1738
if (!IsDirectlyImplementedInterface(interfaceType))
1740
Type baseType = this.BaseType;
1741
if (baseType == null)
1743
throw new ArgumentException();
1747
map = baseType.GetInterfaceMap(interfaceType);
1752
map.InterfaceMethods = interfaceType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
1753
map.InterfaceType = interfaceType;
1754
map.TargetMethods = new MethodInfo[map.InterfaceMethods.Length];
1755
FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1756
MethodInfo[] methods = GetMethods(BindingFlags.Instance | BindingFlags.Public);
1757
for (int i = 0; i < map.TargetMethods.Length; i++)
1759
if (map.TargetMethods[i] == null)
1761
// TODO use proper method resolution (also take into account that no implicit base class implementation is used across assembly boundaries)
1762
for (int j = 0; j < methods.Length; j++)
1764
if (methods[j].Name == map.InterfaceMethods[i].Name
1765
&& methods[j].MethodSignature.Equals(map.InterfaceMethods[i].MethodSignature))
1767
map.TargetMethods[i] = methods[j];
1772
for (Type baseType = this.BaseType; baseType != null && interfaceType.IsAssignableFrom(baseType); baseType = baseType.BaseType)
1774
baseType.FillInExplicitInterfaceMethods(map.InterfaceMethods, map.TargetMethods);
1777
map.TargetType = this;
1781
internal void FillInExplicitInterfaceMethods(MethodInfo[] interfaceMethods, MethodInfo[] targetMethods)
1783
__MethodImplMap impl = __GetMethodImplMap();
1784
for (int i = 0; i < impl.MethodDeclarations.Length; i++)
1786
for (int j = 0; j < impl.MethodDeclarations[i].Length; j++)
1788
int index = Array.IndexOf(interfaceMethods, impl.MethodDeclarations[i][j]);
1789
if (index != -1 && targetMethods[index] == null)
1791
targetMethods[index] = impl.MethodBodies[i];
1797
Type IGenericContext.GetGenericTypeArgument(int index)
1799
return GetGenericTypeArgument(index);
1802
Type IGenericContext.GetGenericMethodArgument(int index)
1804
throw new BadImageFormatException();
1807
Type IGenericBinder.BindTypeParameter(Type type)
1809
return GetGenericTypeArgument(type.GenericParameterPosition);
1812
Type IGenericBinder.BindMethodParameter(Type type)
1814
throw new BadImageFormatException();
1817
internal virtual Type BindTypeParameters(IGenericBinder binder)
1819
if (IsGenericTypeDefinition)
1821
Type[] args = GetGenericArguments();
1822
Type.InplaceBindTypeParameters(binder, args);
1823
return GenericTypeInstance.Make(this, args, null);
1831
private static void InplaceBindTypeParameters(IGenericBinder binder, Type[] types)
1833
for (int i = 0; i < types.Length; i++)
1835
types[i] = types[i].BindTypeParameters(binder);
1839
internal virtual MethodBase FindMethod(string name, MethodSignature signature)
1841
foreach (MethodBase method in __GetDeclaredMethods())
1843
if (method.Name == name && method.MethodSignature.Equals(signature))
1851
internal virtual FieldInfo FindField(string name, FieldSignature signature)
1853
foreach (FieldInfo field in __GetDeclaredFields())
1855
if (field.Name == name && field.FieldSignature.Equals(signature))
1863
internal bool IsAllowMultipleCustomAttribute
1867
IList<CustomAttributeData> cad = CustomAttributeData.__GetCustomAttributes(this, this.Module.universe.System_AttributeUsageAttribute, false);
1870
foreach (CustomAttributeNamedArgument arg in cad[0].NamedArguments)
1872
if (arg.MemberInfo.Name == "AllowMultiple")
1874
return (bool)arg.TypedValue.Value;
1882
internal bool IsPseudoCustomAttribute
1886
Universe u = this.Module.universe;
1887
return this == u.System_NonSerializedAttribute
1888
|| this == u.System_SerializableAttribute
1889
|| this == u.System_Runtime_InteropServices_DllImportAttribute
1890
|| this == u.System_Runtime_InteropServices_FieldOffsetAttribute
1891
|| this == u.System_Runtime_InteropServices_InAttribute
1892
|| this == u.System_Runtime_InteropServices_MarshalAsAttribute
1893
|| this == u.System_Runtime_InteropServices_OutAttribute
1894
|| this == u.System_Runtime_InteropServices_StructLayoutAttribute
1895
|| this == u.System_Runtime_InteropServices_OptionalAttribute
1896
|| this == u.System_Runtime_InteropServices_PreserveSigAttribute
1897
|| this == u.System_Runtime_InteropServices_ComImportAttribute
1898
|| this == u.System_Runtime_CompilerServices_SpecialNameAttribute
1899
|| this == u.System_Runtime_CompilerServices_MethodImplAttribute
1904
internal Type MarkNotValueType()
1906
typeFlags |= TypeFlags.NotValueType;
1910
internal Type MarkValueType()
1912
typeFlags |= TypeFlags.ValueType;
1916
internal ConstructorInfo GetPseudoCustomAttributeConstructor(params Type[] parameterTypes)
1918
Universe u = this.Module.universe;
1919
MethodSignature methodSig = MethodSignature.MakeFromBuilder(u.System_Void, parameterTypes, new PackedCustomModifiers(), CallingConventions.Standard | CallingConventions.HasThis, 0);
1921
FindMethod(".ctor", methodSig) ??
1922
u.GetMissingMethodOrThrow(this, ".ctor", methodSig);
1923
return (ConstructorInfo)mb;
1926
public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1928
return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, parameterTypes.Length));
1931
private MethodBase CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, PackedCustomModifiers customModifiers)
1933
MethodSignature sig = new MethodSignature(
1934
returnType ?? this.Module.universe.System_Void,
1935
Util.Copy(parameterTypes),
1939
MethodInfo method = new MissingMethod(this, name, sig);
1940
if (name == ".ctor" || name == ".cctor")
1942
return new ConstructorInfoImpl(method);
1947
[Obsolete("Please use __CreateMissingMethod(string, CallingConventions, Type, CustomModifiers, Type[], CustomModifiers[]) instead")]
1948
public MethodBase __CreateMissingMethod(string name, CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
1950
return CreateMissingMethod(name, callingConvention, returnType, parameterTypes, PackedCustomModifiers.CreateFromExternal(returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers, parameterTypeRequiredCustomModifiers, parameterTypes.Length));
1953
public FieldInfo __CreateMissingField(string name, Type fieldType, CustomModifiers customModifiers)
1955
return new MissingField(this, name, FieldSignature.Create(fieldType, customModifiers));
1958
[Obsolete("Please use __CreateMissingField(string, Type, CustomModifiers) instead")]
1959
public FieldInfo __CreateMissingField(string name, Type fieldType, Type[] requiredCustomModifiers, Type[] optionalCustomModifiers)
1961
return __CreateMissingField(name, fieldType, CustomModifiers.FromReqOpt(requiredCustomModifiers, optionalCustomModifiers));
1964
public PropertyInfo __CreateMissingProperty(string name, CallingConventions callingConvention, Type propertyType, CustomModifiers propertyTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
1966
PropertySignature sig = PropertySignature.Create(callingConvention,
1969
PackedCustomModifiers.CreateFromExternal(propertyTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes)));
1970
return new MissingProperty(this, name, sig);
1973
internal virtual Type SetMetadataTokenForMissing(int token, int flags)
1978
protected void MarkEnumOrValueType(string typeNamespace, string typeName)
1980
// we assume that mscorlib won't have nested types with these names,
1981
// so we don't check that we're not a nested type
1982
if (typeNamespace == "System"
1983
&& (typeName == "Enum" || typeName == "ValueType"))
1985
typeFlags |= TypeFlags.PotentialEnumOrValueType;
1989
private bool ResolvePotentialEnumOrValueType()
1991
if (this.Assembly == this.Universe.Mscorlib
1992
|| this.Assembly.GetName().Name.Equals("mscorlib", StringComparison.OrdinalIgnoreCase)
1993
// check if mscorlib forwards the type (.NETCore profile reference mscorlib forwards System.Enum and System.ValueType to System.Runtime.dll)
1994
|| this.Universe.Mscorlib.FindType(new TypeName(__Namespace, __Name)) == this)
1996
typeFlags = (typeFlags & ~TypeFlags.PotentialEnumOrValueType) | TypeFlags.EnumOrValueType;
2001
typeFlags &= ~TypeFlags.PotentialEnumOrValueType;
2006
private bool IsEnumOrValueType
2010
return (typeFlags & (TypeFlags.EnumOrValueType | TypeFlags.PotentialEnumOrValueType)) != 0
2011
&& ((typeFlags & TypeFlags.EnumOrValueType) != 0 || ResolvePotentialEnumOrValueType());
2015
internal virtual Universe Universe
2017
get { return Module.universe; }
2020
internal sealed override bool BindingFlagsMatch(BindingFlags flags)
2022
return BindingFlagsMatch(IsNestedPublic, flags, BindingFlags.Public, BindingFlags.NonPublic);
2025
internal sealed override MemberInfo SetReflectedType(Type type)
2027
throw new InvalidOperationException();
2030
internal override int GetCurrentToken()
2032
return this.MetadataToken;
2035
internal sealed override List<CustomAttributeData> GetPseudoCustomAttributes(Type attributeType)
2037
// types don't have pseudo custom attributes
2041
// in .NET this is an extension method, but we target .NET 2.0, so we have an instance method
2042
public TypeInfo GetTypeInfo()
2044
TypeInfo type = this as TypeInfo;
2047
throw new MissingMemberException(this);
2052
public virtual bool __IsTypeForwarder
2054
get { return false; }
2058
abstract class ElementHolderType : TypeInfo
2060
protected readonly Type elementType;
2062
private readonly CustomModifiers mods;
2064
protected ElementHolderType(Type elementType, CustomModifiers mods)
2066
this.elementType = elementType;
2070
protected bool EqualsHelper(ElementHolderType other)
2072
return other != null
2073
&& other.elementType.Equals(elementType)
2074
&& other.mods.Equals(mods);
2077
public override CustomModifiers __GetCustomModifiers()
2082
public sealed override string Name
2084
get { return elementType.Name + GetSuffix(); }
2087
public sealed override string Namespace
2089
get { return elementType.Namespace; }
2092
public sealed override string FullName
2094
get { return elementType.FullName + GetSuffix(); }
2097
public sealed override string ToString()
2099
return elementType.ToString() + GetSuffix();
2102
public sealed override Type GetElementType()
2107
public sealed override bool HasElementType
2109
get { return true; }
2112
public sealed override Module Module
2114
get { return elementType.Module; }
2117
internal sealed override int GetModuleBuilderToken()
2121
token = ((ModuleBuilder)elementType.Module).ImportType(this);
2126
public sealed override bool ContainsGenericParameters
2130
Type type = elementType;
2131
while (type.HasElementType)
2133
type = type.GetElementType();
2135
return type.ContainsGenericParameters;
2139
public sealed override bool __ContainsMissingType
2143
Type type = elementType;
2144
while (type.HasElementType)
2146
type = type.GetElementType();
2148
return type.__ContainsMissingType;
2152
internal sealed override Type BindTypeParameters(IGenericBinder binder)
2154
Type type = elementType.BindTypeParameters(binder);
2155
CustomModifiers mods = this.mods.Bind(binder);
2156
if (ReferenceEquals(type, elementType)
2157
&& mods.Equals(this.mods))
2161
return Wrap(type, mods);
2164
internal override void CheckBaked()
2166
elementType.CheckBaked();
2169
internal sealed override Universe Universe
2171
get { return elementType.Universe; }
2174
internal sealed override bool IsBaked
2176
get { return elementType.IsBaked; }
2179
internal sealed override int GetCurrentToken()
2181
// we don't have a token, so we return 0 (which is never a valid token)
2185
internal abstract string GetSuffix();
2187
protected abstract Type Wrap(Type type, CustomModifiers mods);
2190
sealed class ArrayType : ElementHolderType
2192
internal static Type Make(Type type, CustomModifiers mods)
2194
return type.Universe.CanonicalizeType(new ArrayType(type, mods));
2197
private ArrayType(Type type, CustomModifiers mods)
2202
public override Type BaseType
2204
get { return elementType.Module.universe.System_Array; }
2207
public override Type[] __GetDeclaredInterfaces()
2210
this.Module.universe.Import(typeof(IList<>)).MakeGenericType(elementType),
2211
this.Module.universe.Import(typeof(ICollection<>)).MakeGenericType(elementType),
2212
this.Module.universe.Import(typeof(IEnumerable<>)).MakeGenericType(elementType)
2216
public override MethodBase[] __GetDeclaredMethods()
2218
Type[] int32 = new Type[] { this.Module.universe.System_Int32 };
2219
List<MethodBase> list = new List<MethodBase>();
2220
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 }));
2221
list.Add(new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), int32));
2222
list.Add(new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, int32));
2223
list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2224
for (Type type = elementType; type.__IsVector; type = type.GetElementType())
2226
Array.Resize(ref int32, int32.Length + 1);
2227
int32[int32.Length - 1] = int32[0];
2228
list.Add(new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, int32)));
2230
return list.ToArray();
2233
public override TypeAttributes Attributes
2235
get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2238
public override bool IsArray
2240
get { return true; }
2243
public override bool __IsVector
2245
get { return true; }
2248
public override int GetArrayRank()
2253
public override bool Equals(object o)
2255
return EqualsHelper(o as ArrayType);
2258
public override int GetHashCode()
2260
return elementType.GetHashCode() * 5;
2263
internal override string GetSuffix()
2268
protected override Type Wrap(Type type, CustomModifiers mods)
2270
return Make(type, mods);
2274
sealed class MultiArrayType : ElementHolderType
2276
private readonly int rank;
2277
private readonly int[] sizes;
2278
private readonly int[] lobounds;
2280
internal static Type Make(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2282
return type.Universe.CanonicalizeType(new MultiArrayType(type, rank, sizes, lobounds, mods));
2285
private MultiArrayType(Type type, int rank, int[] sizes, int[] lobounds, CustomModifiers mods)
2290
this.lobounds = lobounds;
2293
public override Type BaseType
2295
get { return elementType.Module.universe.System_Array; }
2298
public override MethodBase[] __GetDeclaredMethods()
2300
Type int32 = this.Module.universe.System_Int32;
2301
Type[] setArgs = new Type[rank + 1];
2302
Type[] getArgs = new Type[rank];
2303
Type[] ctorArgs = new Type[rank * 2];
2304
for (int i = 0; i < rank; i++)
2308
ctorArgs[i * 2 + 0] = int32;
2309
ctorArgs[i * 2 + 1] = int32;
2311
setArgs[rank] = elementType;
2312
return new MethodBase[] {
2313
new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, getArgs)),
2314
new ConstructorInfoImpl(new BuiltinArrayMethod(this.Module, this, ".ctor", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, ctorArgs)),
2315
new BuiltinArrayMethod(this.Module, this, "Set", CallingConventions.Standard | CallingConventions.HasThis, this.Module.universe.System_Void, setArgs),
2316
new BuiltinArrayMethod(this.Module, this, "Address", CallingConventions.Standard | CallingConventions.HasThis, elementType.MakeByRefType(), getArgs),
2317
new BuiltinArrayMethod(this.Module, this, "Get", CallingConventions.Standard | CallingConventions.HasThis, elementType, getArgs),
2321
public override TypeAttributes Attributes
2323
get { return TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable; }
2326
public override bool IsArray
2328
get { return true; }
2331
public override int GetArrayRank()
2336
public override int[] __GetArraySizes()
2338
return Util.Copy(sizes);
2341
public override int[] __GetArrayLowerBounds()
2343
return Util.Copy(lobounds);
2346
public override bool Equals(object o)
2348
MultiArrayType at = o as MultiArrayType;
2349
return EqualsHelper(at)
2351
&& ArrayEquals(at.sizes, sizes)
2352
&& ArrayEquals(at.lobounds, lobounds);
2355
private static bool ArrayEquals(int[] i1, int[] i2)
2357
if (i1.Length == i2.Length)
2359
for (int i = 0; i < i1.Length; i++)
2371
public override int GetHashCode()
2373
return elementType.GetHashCode() * 9 + rank;
2376
internal override string GetSuffix()
2384
return "[" + new String(',', rank - 1) + "]";
2388
protected override Type Wrap(Type type, CustomModifiers mods)
2390
return Make(type, rank, sizes, lobounds, mods);
2394
sealed class BuiltinArrayMethod : ArrayMethod
2396
internal BuiltinArrayMethod(Module module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
2397
: base(module, arrayClass, methodName, callingConvention, returnType, parameterTypes)
2401
public override MethodAttributes Attributes
2403
get { return this.Name == ".ctor" ? MethodAttributes.RTSpecialName | MethodAttributes.Public : MethodAttributes.Public; }
2406
public override MethodImplAttributes GetMethodImplementationFlags()
2408
return MethodImplAttributes.IL;
2411
public override int MetadataToken
2413
get { return 0x06000000; }
2416
public override MethodBody GetMethodBody()
2421
public override ParameterInfo[] GetParameters()
2423
ParameterInfo[] parameterInfos = new ParameterInfo[parameterTypes.Length];
2424
for (int i = 0; i < parameterInfos.Length; i++)
2426
parameterInfos[i] = new ParameterInfoImpl(this, parameterTypes[i], i);
2428
return parameterInfos;
2431
public override ParameterInfo ReturnParameter
2433
get { return new ParameterInfoImpl(this, this.ReturnType, -1); }
2436
private sealed class ParameterInfoImpl : ParameterInfo
2438
private readonly MethodInfo method;
2439
private readonly Type type;
2440
private readonly int pos;
2442
internal ParameterInfoImpl(MethodInfo method, Type type, int pos)
2444
this.method = method;
2449
public override Type ParameterType
2451
get { return type; }
2454
public override string Name
2456
get { return null; }
2459
public override ParameterAttributes Attributes
2461
get { return ParameterAttributes.None; }
2464
public override int Position
2469
public override object RawDefaultValue
2471
get { return null; }
2474
public override CustomModifiers __GetCustomModifiers()
2476
return new CustomModifiers();
2479
public override bool __TryGetFieldMarshal(out FieldMarshal fieldMarshal)
2481
fieldMarshal = new FieldMarshal();
2485
public override MemberInfo Member
2487
get { return method.IsConstructor ? (MethodBase)new ConstructorInfoImpl(method) : method; }
2490
public override int MetadataToken
2492
get { return 0x08000000; }
2495
internal override Module Module
2497
get { return method.Module; }
2502
sealed class ByRefType : ElementHolderType
2504
internal static Type Make(Type type, CustomModifiers mods)
2506
return type.Universe.CanonicalizeType(new ByRefType(type, mods));
2509
private ByRefType(Type type, CustomModifiers mods)
2514
public override bool Equals(object o)
2516
return EqualsHelper(o as ByRefType);
2519
public override int GetHashCode()
2521
return elementType.GetHashCode() * 3;
2524
public override Type BaseType
2526
get { return null; }
2529
public override TypeAttributes Attributes
2534
public override bool IsByRef
2536
get { return true; }
2539
internal override string GetSuffix()
2544
protected override Type Wrap(Type type, CustomModifiers mods)
2546
return Make(type, mods);
2550
sealed class PointerType : ElementHolderType
2552
internal static Type Make(Type type, CustomModifiers mods)
2554
return type.Universe.CanonicalizeType(new PointerType(type, mods));
2557
private PointerType(Type type, CustomModifiers mods)
2562
public override bool Equals(object o)
2564
return EqualsHelper(o as PointerType);
2567
public override int GetHashCode()
2569
return elementType.GetHashCode() * 7;
2572
public override Type BaseType
2574
get { return null; }
2577
public override TypeAttributes Attributes
2582
public override bool IsPointer
2584
get { return true; }
2587
internal override string GetSuffix()
2592
protected override Type Wrap(Type type, CustomModifiers mods)
2594
return Make(type, mods);
2598
sealed class GenericTypeInstance : TypeInfo
2600
private readonly Type type;
2601
private readonly Type[] args;
2602
private readonly CustomModifiers[] mods;
2603
private Type baseType;
2606
internal static Type Make(Type type, Type[] typeArguments, CustomModifiers[] mods)
2608
bool identity = true;
2609
if (type is TypeBuilder || type is BakedType || type.__IsMissing)
2611
// a TypeBuiler identity must be instantiated
2616
// we must not instantiate the identity instance, because typeof(Foo<>).MakeGenericType(typeof(Foo<>).GetGenericArguments()) == typeof(Foo<>)
2617
for (int i = 0; i < typeArguments.Length; i++)
2619
if (typeArguments[i] != type.GetGenericTypeArgument(i)
2620
|| !IsEmpty(mods, i))
2633
return type.Universe.CanonicalizeType(new GenericTypeInstance(type, typeArguments, mods));
2637
private static bool IsEmpty(CustomModifiers[] mods, int i)
2639
// we need to be extra careful, because mods doesn't not need to be in canonical format
2640
// (Signature.ReadGenericInst() calls Make() directly, without copying the modifier arrays)
2641
return mods == null || mods[i].IsEmpty;
2644
private GenericTypeInstance(Type type, Type[] args, CustomModifiers[] mods)
2651
public override bool Equals(object o)
2653
GenericTypeInstance gt = o as GenericTypeInstance;
2654
return gt != null && gt.type.Equals(type) && Util.ArrayEquals(gt.args, args)
2655
&& Util.ArrayEquals(gt.mods, mods);
2658
public override int GetHashCode()
2660
return type.GetHashCode() * 3 ^ Util.GetHashCode(args);
2663
public override string AssemblyQualifiedName
2667
string fn = FullName;
2668
return fn == null ? null : fn + ", " + type.Assembly.FullName;
2672
public override Type BaseType
2676
if (baseType == null)
2678
Type rawBaseType = type.BaseType;
2679
if (rawBaseType == null)
2681
baseType = rawBaseType;
2685
baseType = rawBaseType.BindTypeParameters(this);
2692
public override bool IsValueType
2694
get { return type.IsValueType; }
2697
public override bool IsVisible
2703
foreach (Type arg in args)
2716
public override Type DeclaringType
2718
get { return type.DeclaringType; }
2721
public override TypeAttributes Attributes
2723
get { return type.Attributes; }
2726
internal override void CheckBaked()
2731
public override FieldInfo[] __GetDeclaredFields()
2733
FieldInfo[] fields = type.__GetDeclaredFields();
2734
for (int i = 0; i < fields.Length; i++)
2736
fields[i] = fields[i].BindTypeParameters(this);
2741
public override Type[] __GetDeclaredInterfaces()
2743
Type[] interfaces = type.__GetDeclaredInterfaces();
2744
for (int i = 0; i < interfaces.Length; i++)
2746
interfaces[i] = interfaces[i].BindTypeParameters(this);
2751
public override MethodBase[] __GetDeclaredMethods()
2753
MethodBase[] methods = type.__GetDeclaredMethods();
2754
for (int i = 0; i < methods.Length; i++)
2756
methods[i] = methods[i].BindTypeParameters(this);
2761
public override Type[] __GetDeclaredTypes()
2763
return type.__GetDeclaredTypes();
2766
public override EventInfo[] __GetDeclaredEvents()
2768
EventInfo[] events = type.__GetDeclaredEvents();
2769
for (int i = 0; i < events.Length; i++)
2771
events[i] = events[i].BindTypeParameters(this);
2776
public override PropertyInfo[] __GetDeclaredProperties()
2778
PropertyInfo[] properties = type.__GetDeclaredProperties();
2779
for (int i = 0; i < properties.Length; i++)
2781
properties[i] = properties[i].BindTypeParameters(this);
2786
public override __MethodImplMap __GetMethodImplMap()
2788
__MethodImplMap map = type.__GetMethodImplMap();
2789
map.TargetType = this;
2790
for (int i = 0; i < map.MethodBodies.Length; i++)
2792
map.MethodBodies[i] = (MethodInfo)map.MethodBodies[i].BindTypeParameters(this);
2793
for (int j = 0; j < map.MethodDeclarations[i].Length; j++)
2795
Type interfaceType = map.MethodDeclarations[i][j].DeclaringType;
2796
if (interfaceType.IsGenericType)
2798
map.MethodDeclarations[i][j] = (MethodInfo)map.MethodDeclarations[i][j].BindTypeParameters(this);
2805
public override string Namespace
2807
get { return type.Namespace; }
2810
public override string Name
2812
get { return type.Name; }
2815
public override string FullName
2819
if (!this.__ContainsMissingType && this.ContainsGenericParameters)
2823
StringBuilder sb = new StringBuilder(this.type.FullName);
2826
foreach (Type type in args)
2828
sb.Append(sep).Append('[').Append(type.FullName).Append(", ").Append(type.Assembly.FullName.Replace("]", "\\]")).Append(']');
2832
return sb.ToString();
2836
public override string ToString()
2838
StringBuilder sb = new StringBuilder(type.FullName);
2841
foreach (Type arg in args)
2848
return sb.ToString();
2851
public override Module Module
2853
get { return type.Module; }
2856
public override bool IsGenericType
2858
get { return true; }
2861
public override bool IsConstructedGenericType
2863
get { return true; }
2866
public override Type GetGenericTypeDefinition()
2871
public override Type[] GetGenericArguments()
2873
return Util.Copy(args);
2876
public override CustomModifiers[] __GetGenericArgumentsCustomModifiers()
2878
return mods != null ? (CustomModifiers[])mods.Clone() : new CustomModifiers[args.Length];
2881
internal override Type GetGenericTypeArgument(int index)
2886
public override bool ContainsGenericParameters
2890
foreach (Type type in args)
2892
if (type.ContainsGenericParameters)
2901
public override bool __ContainsMissingType
2905
foreach (Type type in args)
2907
if (type.__ContainsMissingType)
2912
return this.type.__IsMissing;
2916
public override StructLayoutAttribute StructLayoutAttribute
2918
get { return type.StructLayoutAttribute; }
2921
internal override int GetModuleBuilderToken()
2925
token = ((ModuleBuilder)type.Module).ImportType(this);
2930
internal override Type BindTypeParameters(IGenericBinder binder)
2932
for (int i = 0; i < args.Length; i++)
2934
Type xarg = args[i].BindTypeParameters(binder);
2935
if (!ReferenceEquals(xarg, args[i]))
2937
Type[] xargs = new Type[args.Length];
2938
Array.Copy(args, xargs, i);
2940
for (; i < args.Length; i++)
2942
xargs[i] = args[i].BindTypeParameters(binder);
2944
return Make(type, xargs, null);
2950
internal override int GetCurrentToken()
2952
return type.GetCurrentToken();
2955
internal override bool IsBaked
2957
get { return type.IsBaked; }
2961
sealed class FunctionPointerType : TypeInfo
2963
private readonly Universe universe;
2964
private readonly __StandAloneMethodSig sig;
2966
internal static Type Make(Universe universe, __StandAloneMethodSig sig)
2968
return universe.CanonicalizeType(new FunctionPointerType(universe, sig));
2971
private FunctionPointerType(Universe universe, __StandAloneMethodSig sig)
2973
this.universe = universe;
2977
public override bool Equals(object obj)
2979
FunctionPointerType other = obj as FunctionPointerType;
2980
return other != null
2981
&& other.universe == universe
2982
&& other.sig.Equals(sig);
2985
public override int GetHashCode()
2987
return sig.GetHashCode();
2990
public override bool __IsFunctionPointer
2992
get { return true; }
2995
public override __StandAloneMethodSig __MethodSignature
3000
public override Type BaseType
3002
get { return null; }
3005
public override TypeAttributes Attributes
3010
public override string Name
3012
get { throw new InvalidOperationException(); }
3015
public override string FullName
3017
get { throw new InvalidOperationException(); }
3020
public override Module Module
3022
get { throw new InvalidOperationException(); }
3025
internal override Universe Universe
3027
get { return universe; }
3030
public override string ToString()
3032
return "<FunctionPtr>";
3035
internal override bool IsBaked
3037
get { return true; }
3041
sealed class MarkerType : Type
3043
// used by CustomModifiers and SignatureHelper
3044
internal static readonly Type ModOpt = new MarkerType();
3045
internal static readonly Type ModReq = new MarkerType();
3046
// used by SignatureHelper
3047
internal static readonly Type Sentinel = new MarkerType();
3048
internal static readonly Type Pinned = new MarkerType();
3050
private MarkerType() { }
3052
public override Type BaseType
3054
get { throw new InvalidOperationException(); }
3057
public override TypeAttributes Attributes
3059
get { throw new InvalidOperationException(); }
3062
public override string Name
3064
get { throw new InvalidOperationException(); }
3067
public override string FullName
3069
get { throw new InvalidOperationException(); }
3072
public override Module Module
3074
get { throw new InvalidOperationException(); }
3077
internal override bool IsBaked
3079
get { throw new InvalidOperationException(); }
3082
public override bool __IsMissing
3084
get { throw new InvalidOperationException(); }