@@ -53,7 +53,11 @@ public int GetHashCode((Type, Type[]) obj)
53
53
private static readonly Type CompilerGeneratedAttributeType = typeof ( CompilerGeneratedAttribute ) ;
54
54
private static readonly string TypeHelperNamespace = typeof ( TypeHelper ) . Namespace ;
55
55
56
+ #if NET8_0_OR_GREATER
57
+ private static readonly ConcurrentDictionary < ( Type , Type [ ] ) , ConstructorInvoker > ConstructorInfoByTypes =
58
+ #else
56
59
private static readonly ConcurrentDictionary < ( Type , Type [ ] ) , ConstructorInfo > ConstructorInfoByTypes =
60
+ #endif
57
61
new ( new TypesEqualityComparer ( ) ) ;
58
62
59
63
private static readonly ConcurrentDictionary< Type, Type[ ] > OrderedInterfaces = new ( ) ;
@@ -68,6 +72,17 @@ public int GetHashCode((Type, Type[]) obj)
68
72
69
73
private static readonly ConcurrentDictionary < ( MethodInfo , Type , Type ) , MethodInfo > GenericMethodInstances2 = new ( ) ;
70
74
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
+
79
+ private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument ) , MethodInvoker > GenericMethodInvokerFactory1 =
80
+ key => MethodInvoker . Create ( key . genericDefinition . MakeGenericMethod ( key . typeArgument ) ) ;
81
+
82
+ private static readonly Func < ( MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) , MethodInvoker > GenericMethodInvokerFactory2 =
83
+ key => MethodInvoker . Create ( key . genericDefinition . MakeGenericMethod ( key . typeArgument1 , key . typeArgument2 ) ) ;
84
+ #endif
85
+
71
86
private static readonly ConcurrentDictionary< ( Type , Type ) , Type > GenericTypeInstances1 = new ( ) ;
72
87
73
88
private static readonly ConcurrentDictionary < ( Type , Type , Type ) , Type > GenericTypeInstances2 = new ( ) ;
@@ -644,9 +659,17 @@ public static object Activate(this Type type, Type[] genericArguments, params ob
644
659
/// The <paramref name="type"/> has no constructors suitable for <paramref name="argumentTypes"/>
645
660
/// -or- more than one such constructor.
646
661
/// </exception>
662
+ #if NET8_0_OR_GREATER
663
+ public static ConstructorInvoker GetSingleConstructorInvoker ( this Type type , Type [ ] argumentTypes ) =>
664
+ ConstructorInfoByTypes . GetOrAdd ( ( type , argumentTypes ) ,
665
+ static t => ConstructorExtractor ( t ) is ConstructorInfo ctor
666
+ ? ConstructorInvoker . Create ( ctor )
667
+ : throw new InvalidOperationException ( Strings . ExGivenTypeHasNoOrMoreThanOneCtorWithGivenParameters ) ) ;
668
+ #else
647
669
public static ConstructorInfo GetSingleConstructor ( this Type type , Type [ ] argumentTypes ) =>
648
670
ConstructorInfoByTypes . GetOrAdd ( ( type , argumentTypes ) , ConstructorExtractor )
649
671
?? throw new InvalidOperationException ( Strings . ExGivenTypeHasNoOrMoreThanOneCtorWithGivenParameters ) ;
672
+ #endif
650
673
651
674
/// <summary>
652
675
/// Gets the public constructor of type <paramref name="type"/>
@@ -659,8 +682,14 @@ public static ConstructorInfo GetSingleConstructor(this Type type, Type[] argume
659
682
/// otherwise, <see langword="null"/>.
660
683
/// </returns>
661
684
[ CanBeNull ]
685
+ #if NET8_0_OR_GREATER
686
+ public static ConstructorInvoker GetSingleConstructorInvokerOrDefault ( this Type type , Type [ ] argumentTypes ) =>
687
+ ConstructorInfoByTypes . GetOrAdd ( ( type , argumentTypes ) ,
688
+ static t => ConstructorExtractor ( t ) is ConstructorInfo ctor ? ConstructorInvoker . Create ( ctor ) : null ) ;
689
+ #else
662
690
public static ConstructorInfo GetSingleConstructorOrDefault ( this Type type , Type [ ] argumentTypes ) =>
663
691
ConstructorInfoByTypes . GetOrAdd ( ( type , argumentTypes ) , ConstructorExtractor ) ;
692
+ #endif
664
693
665
694
private static readonly Func < ( Type , Type [ ] ) , ConstructorInfo > ConstructorExtractor = t => {
666
695
( var type , var argumentTypes ) = t ;
@@ -685,6 +714,7 @@ from pair in zipped
685
714
return constructors . SingleOrDefault ( ) ;
686
715
} ;
687
716
717
+
688
718
/// <summary>
689
719
/// Orders the specified <paramref name="types"/> by their inheritance
690
720
/// (very base go first).
@@ -920,6 +950,20 @@ public static MethodInfo CachedMakeGenericMethod(this MethodInfo genericDefiniti
920
950
public static MethodInfo CachedMakeGenericMethod ( this MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
921
951
GenericMethodInstances2 . GetOrAdd ( ( genericDefinition , typeArgument1 , typeArgument2 ) , GenericMethodFactory2 ) ;
922
952
953
+ #if NET8_0_OR_GREATER
954
+ public static MethodInvoker CachedMakeGenericMethodInvoker ( this MethodInfo genericDefinition , Type typeArgument ) =>
955
+ GenericMethodInvokers1 . GetOrAdd ( ( genericDefinition , typeArgument ) , GenericMethodInvokerFactory1 ) ;
956
+
957
+ public static MethodInvoker CachedMakeGenericMethodInvoker ( this MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
958
+ GenericMethodInvokers2 . GetOrAdd ( ( genericDefinition , typeArgument1 , typeArgument2 ) , GenericMethodInvokerFactory2 ) ;
959
+ #else
960
+ public static MethodInfo CachedMakeGenericMethodInvoker ( this MethodInfo genericDefinition , Type typeArgument ) =>
961
+ CachedMakeGenericMethod ( genericDefinition , typeArgument ) ;
962
+
963
+ public static MethodInfo CachedMakeGenericMethodInvoker ( this MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
964
+ CachedMakeGenericMethod ( genericDefinition , typeArgument1 , typeArgument2 ) ;
965
+ #endif
966
+
923
967
public static Type CachedMakeGenericType ( this Type genericDefinition , Type typeArgument ) =>
924
968
GenericTypeInstances1 . GetOrAdd ( ( genericDefinition , typeArgument ) , GenericTypeFactory1 ) ;
925
969
0 commit comments