@@ -53,7 +53,11 @@ public int GetHashCode((Type, Type[]) obj)
5353 private static readonly Type CompilerGeneratedAttributeType = typeof ( CompilerGeneratedAttribute ) ;
5454 private static readonly string TypeHelperNamespace = typeof ( TypeHelper ) . Namespace ;
5555
56+ #if NET8_0_OR_GREATER
57+ private static readonly ConcurrentDictionary < ( Type , Type [ ] ) , ConstructorInvoker > ConstructorInfoByTypes =
58+ #else
5659 private static readonly ConcurrentDictionary < ( Type , Type [ ] ) , ConstructorInfo > ConstructorInfoByTypes =
60+ #endif
5761 new ( new TypesEqualityComparer ( ) ) ;
5862
5963 private static readonly ConcurrentDictionary< Type, Type[ ] > OrderedInterfaces = new ( ) ;
@@ -68,6 +72,17 @@ public int GetHashCode((Type, Type[]) obj)
6872
6973 private static readonly ConcurrentDictionary < ( MethodInfo , Type , Type ) , MethodInfo > GenericMethodInstances2 = new ( ) ;
7074
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+
7186 private static readonly ConcurrentDictionary< ( Type , Type ) , Type > GenericTypeInstances1 = new ( ) ;
7287
7388 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
644659 /// The <paramref name="type"/> has no constructors suitable for <paramref name="argumentTypes"/>
645660 /// -or- more than one such constructor.
646661 /// </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
647669 public static ConstructorInfo GetSingleConstructor ( this Type type , Type [ ] argumentTypes ) =>
648670 ConstructorInfoByTypes . GetOrAdd ( ( type , argumentTypes ) , ConstructorExtractor )
649671 ?? throw new InvalidOperationException ( Strings . ExGivenTypeHasNoOrMoreThanOneCtorWithGivenParameters ) ;
672+ #endif
650673
651674 /// <summary>
652675 /// Gets the public constructor of type <paramref name="type"/>
@@ -659,8 +682,14 @@ public static ConstructorInfo GetSingleConstructor(this Type type, Type[] argume
659682 /// otherwise, <see langword="null"/>.
660683 /// </returns>
661684 [ 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
662690 public static ConstructorInfo GetSingleConstructorOrDefault ( this Type type , Type [ ] argumentTypes ) =>
663691 ConstructorInfoByTypes . GetOrAdd ( ( type , argumentTypes ) , ConstructorExtractor ) ;
692+ #endif
664693
665694 private static readonly Func < ( Type , Type [ ] ) , ConstructorInfo > ConstructorExtractor = t => {
666695 ( var type , var argumentTypes ) = t ;
@@ -685,6 +714,7 @@ from pair in zipped
685714 return constructors . SingleOrDefault ( ) ;
686715 } ;
687716
717+
688718 /// <summary>
689719 /// Orders the specified <paramref name="types"/> by their inheritance
690720 /// (very base go first).
@@ -920,6 +950,20 @@ public static MethodInfo CachedMakeGenericMethod(this MethodInfo genericDefiniti
920950 public static MethodInfo CachedMakeGenericMethod ( this MethodInfo genericDefinition , Type typeArgument1 , Type typeArgument2 ) =>
921951 GenericMethodInstances2 . GetOrAdd ( ( genericDefinition , typeArgument1 , typeArgument2 ) , GenericMethodFactory2 ) ;
922952
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+
923967 public static Type CachedMakeGenericType ( this Type genericDefinition , Type typeArgument ) =>
924968 GenericTypeInstances1 . GetOrAdd ( ( genericDefinition , typeArgument ) , GenericTypeFactory1 ) ;
925969
0 commit comments