@@ -47,12 +47,13 @@ public int GetHashCode((Type, Type[]) obj)
47
47
48
48
private static readonly object EmitLock = new object ( ) ;
49
49
private static readonly int NullableTypeMetadataToken = WellKnownTypes . NullableOfT . MetadataToken ;
50
- private static readonly int ValueTuple1 = typeof ( ValueTuple < > ) . MetadataToken ;
51
- private static readonly int ValueTuple8 = typeof ( ValueTuple < , , , , , , , > ) . MetadataToken ;
50
+ private static readonly int ValueTuple1MetadataToken = typeof ( ValueTuple < > ) . MetadataToken ;
51
+ private static readonly int ValueTuple8MetadataToken = typeof ( ValueTuple < , , , , , , , > ) . MetadataToken ;
52
52
private static readonly Module SystemCoreLibModule = WellKnownTypes . NullableOfT . Module ;
53
53
private static readonly Type CompilerGeneratedAttributeType = typeof ( CompilerGeneratedAttribute ) ;
54
54
private static readonly string TypeHelperNamespace = typeof ( TypeHelper ) . Namespace ;
55
55
56
+ #region Caches and cache items factories
56
57
#if NET8_0_OR_GREATER
57
58
private static readonly ConcurrentDictionary < ( Type , Type [ ] ) , ConstructorInvoker > ConstructorInvokerByTypes =
58
59
new ( new TypesEqualityComparer ( ) ) ;
@@ -71,16 +72,11 @@ public int GetHashCode((Type, Type[]) obj)
71
72
private static readonly ConcurrentDictionary < ( MethodInfo , Type ) , MethodInfo > GenericMethodInstances1 = new ( ) ;
72
73
73
74
private static readonly ConcurrentDictionary < ( MethodInfo , Type , Type ) , MethodInfo > GenericMethodInstances2 = new ( ) ;
74
-
75
75
#if NET8_0_OR_GREATER
76
- private static readonly ConcurrentDictionary < ( MethodInfo , Type ) , MethodInvoker > GenericMethodInvokers1 = new ( ) ;
77
- private static readonly ConcurrentDictionary < ( MethodInfo , Type , Type ) , MethodInvoker > GenericMethodInvokers2 = new ( ) ;
78
76
79
- private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument ) , MethodInvoker > GenericMethodInvokerFactory1 =
80
- key => MethodInvoker . Create ( key . genericDefinition . MakeGenericMethod ( key . typeArgument ) ) ;
77
+ private static readonly ConcurrentDictionary < ( MethodInfo , Type ) , MethodInvoker > GenericMethodInvokers1 = new ( ) ;
81
78
82
- private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) , MethodInvoker > GenericMethodInvokerFactory2 =
83
- key => MethodInvoker . Create ( key . genericDefinition . MakeGenericMethod ( key . typeArgument1 , key . typeArgument2 ) ) ;
79
+ private static readonly ConcurrentDictionary < ( MethodInfo , Type , Type ) , MethodInvoker > GenericMethodInvokers2 = new ( ) ;
84
80
#endif
85
81
86
82
private static readonly ConcurrentDictionary < ( Type , Type ) , Type > GenericTypeInstances1 = new ( ) ;
@@ -93,11 +89,21 @@ public int GetHashCode((Type, Type[]) obj)
93
89
private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) , MethodInfo > GenericMethodFactory2 =
94
90
key => key . genericDefinition . MakeGenericMethod ( key . typeArgument1 , key . typeArgument2 ) ;
95
91
96
- private static readonly Func < ( Type genericDefinition , Type typeArgument ) , Type > GenericTypeFactory1 = key =>
97
- key . genericDefinition . MakeGenericType ( key . typeArgument ) ;
92
+ private static readonly Func < ( Type genericDefinition , Type typeArgument ) , Type > GenericTypeFactory1 =
93
+ key => key . genericDefinition . MakeGenericType ( key . typeArgument ) ;
94
+
95
+ private static readonly Func < ( Type genericDefinition , Type typeArgument1 , Type typeArgument2 ) , Type > GenericTypeFactory2 =
96
+ key => key . genericDefinition . MakeGenericType ( key . typeArgument1 , key . typeArgument2 ) ;
97
+ #if NET8_0_OR_GREATER
98
98
99
- private static readonly Func < ( Type genericDefinition , Type typeArgument1 , Type typeArgument2 ) , Type > GenericTypeFactory2 = key =>
100
- key . genericDefinition . MakeGenericType ( key . typeArgument1 , key . typeArgument2 ) ;
99
+ private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument ) , MethodInvoker > GenericMethodInvokerFactory1 =
100
+ key => MethodInvoker . Create ( key . genericDefinition . MakeGenericMethod ( key . typeArgument ) ) ;
101
+
102
+ private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) , MethodInvoker > GenericMethodInvokerFactory2 =
103
+ key => MethodInvoker . Create ( key . genericDefinition . MakeGenericMethod ( key . typeArgument1 , key . typeArgument2 ) ) ;
104
+ #endif
105
+
106
+ #endregion
101
107
102
108
private static int createDummyTypeNumber = 0 ;
103
109
private static AssemblyBuilder assemblyBuilder ;
@@ -968,23 +974,68 @@ public static bool IsGenericMethodSpecificationOf(this MethodInfo method, Method
968
974
|| method . Module == genericMethodDefinition . Module )
969
975
&& method . IsGenericMethod && genericMethodDefinition . IsGenericMethodDefinition ;
970
976
977
+ /// <summary>
978
+ /// Makes generic <see cref="MethodInfo"/> for given definition and type argument
979
+ /// or returns already existing instance from cache.
980
+ /// </summary>
981
+ /// <param name="genericDefinition">Generic definition of method.</param>
982
+ /// <param name="typeArgument">Type argument for final generic method.</param>
983
+ /// <returns>Newly created instance or already existing one.</returns>
971
984
public static MethodInfo CachedMakeGenericMethod ( this MethodInfo genericDefinition , Type typeArgument ) =>
972
985
GenericMethodInstances1 . GetOrAdd ( ( genericDefinition , typeArgument ) , GenericMethodFactory1 ) ;
973
986
987
+ /// <summary>
988
+ /// Makes generic <see cref="MethodInfo"/> for given definition and type arguments
989
+ /// or returns already existing instance from cache.
990
+ /// </summary>
991
+ /// <param name="genericDefinition">Generic definition of method.</param>
992
+ /// <param name="typeArgument1">First type argument for final generic method.</param>
993
+ /// <param name="typeArgument2">Second type argument for final generic method.</param>
994
+ /// <returns>Newly created instance or already existing one.</returns>
974
995
public static MethodInfo CachedMakeGenericMethod ( this MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
975
996
GenericMethodInstances2 . GetOrAdd ( ( genericDefinition , typeArgument1 , typeArgument2 ) , GenericMethodFactory2 ) ;
976
997
977
998
#if NET8_0_OR_GREATER
999
+ /// <summary>
1000
+ /// Makes <see cref="MethodInvoker"/> for generic <see cref="MethodInfo"/> for given definition and type argument
1001
+ /// or returns already existing instance from cache.
1002
+ /// </summary>
1003
+ /// <param name="genericDefinition">Generic definition of method.</param>
1004
+ /// <param name="typeArgument">Type argument for final generic method.</param>
1005
+ /// <returns>Newly created instance or already existing one.</returns>
978
1006
public static MethodInvoker CachedMakeGenericMethodInvoker ( this MethodInfo genericDefinition , Type typeArgument ) =>
979
1007
GenericMethodInvokers1 . GetOrAdd ( ( genericDefinition , typeArgument ) , GenericMethodInvokerFactory1 ) ;
980
1008
1009
+ /// <summary>
1010
+ /// Makes <see cref="MethodInvoker"/> for generic <see cref="MethodInfo"/> for given definition and type arguments
1011
+ /// or returns already existing instance from cache.
1012
+ /// </summary>
1013
+ /// <param name="genericDefinition">Generic definition of method.</param>
1014
+ /// <param name="typeArgument1">First type argument for final generic method.</param>
1015
+ /// <param name="typeArgument2">Second type argument for final generic method.</param>
1016
+ /// <returns>Newly created instance or already existing one.</returns>
981
1017
public static MethodInvoker CachedMakeGenericMethodInvoker ( this MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
982
1018
GenericMethodInvokers2 . GetOrAdd ( ( genericDefinition , typeArgument1 , typeArgument2 ) , GenericMethodInvokerFactory2 ) ;
983
1019
#endif
984
1020
1021
+ /// <summary>
1022
+ /// Makes generic type of given type definition and type argument
1023
+ /// or returns already existing instance from cache.
1024
+ /// </summary>
1025
+ /// <param name="genericDefinition">Generic type definition.</param>
1026
+ /// <param name="typeArgument">Type argument for final generic type.</param>
1027
+ /// <returns>Newly created instance or already existing one.</returns>
985
1028
public static Type CachedMakeGenericType ( this Type genericDefinition , Type typeArgument ) =>
986
1029
GenericTypeInstances1 . GetOrAdd ( ( genericDefinition , typeArgument ) , GenericTypeFactory1 ) ;
987
1030
1031
+ /// <summary>
1032
+ /// Makes generic type of given type definition and type argument
1033
+ /// or returns already existing instance from cache.
1034
+ /// </summary>
1035
+ /// <param name="genericDefinition">Generic type definition.</param>
1036
+ /// <param name="typeArgument1">First type argument for final generic type.</param>
1037
+ /// <param name="typeArgument2">Second type argument for final generic type.</param>
1038
+ /// <returns>Newly created instance or already existing one.</returns>
988
1039
public static Type CachedMakeGenericType ( this Type genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
989
1040
GenericTypeInstances2 . GetOrAdd ( ( genericDefinition , typeArgument1 , typeArgument2 ) , GenericTypeFactory2 ) ;
990
1041
@@ -1204,7 +1255,7 @@ internal static bool IsValueTuple(this Type type)
1204
1255
// this stands on the theory that tokens for all generic versions of ValueTuple
1205
1256
// go one after another.
1206
1257
var currentToken = type . MetadataToken ;
1207
- return ( ( currentToken >= ValueTuple1 ) && currentToken <= ValueTuple8 )
1258
+ return ( ( currentToken >= ValueTuple1MetadataToken ) && currentToken <= ValueTuple8MetadataToken )
1208
1259
&& ReferenceEquals ( type . Module , SystemCoreLibModule ) ;
1209
1260
}
1210
1261
0 commit comments