Sometimes when developing generic methods extensions it needs additional generic parameter types, and when any of types can not be inferred then all generic parameter types must be specified. This library brings IOutParam<out T>, IInParam<in T> and ITypeParam<T> interfaces and corresponding factories to help developers create method parameters that lets the compiler infer the type of the generic parameter.
$ dotnet add package Raiqub.ImplicitGenericsProvides a mechanism that allows generic type parameter to be inferred implicitly.
🏃 Quickstart | 📗 Guide | 📦 NuGet
<hr />Sometimes when developing generic methods extensions it needs additional generic parameter types, and when any of types can not be inferred then all generic parameter types must be specified.
This library brings interfaces and factories listed below IOutParam<out T> interface and OutParam class to help developers create method
parameters that lets the compiler infer the type of the generic parameter.
IOutParam<out T> interface and OutParam factory class for covariant parameters;IInParam<out T> interface and InParam factory class for contravariant parameters;ITypeParam<out T> interface and TypeParam factory class for non-variant parameters;Raiqub.ImplicitGenerics is currently compatible with the following frameworks:
Reference the package <br/> Add the package to your project, for example via:
Install-Package Raiqub.ImplicitGenerics
--or--
dotnet add package Raiqub.ImplicitGenerics
<br/> To use the types provided by this library add using on top of file:
.csusing Raiqub.ImplicitGenerics
You should now be ready to use the library types on your project.
To understand the scenarios covered by this library, some examples are given.
Take an example of a method that registers an adapter:
public static IServiceCollection AddAdapter<TIn, TOut, TAdapter>(
this IServiceCollection services)
where TAdapter : class, IAdapter<TIn, TOut> =>
services.AddSingleton<IAdapter<TIn, TOut>, TAdapter>();
To call this method all three parameters must be specified:
services.AddAdapter<int, float, Int32ToFloatAdapter>()
But, using the type provided by this library:
public static IServiceCollection AddAdapter<TIn, TOut>(
this IServiceCollection services,
IOutParam<IAdapter<TIn, TOut>> outParam) =>
services.AddSingleton(typeof(IAdapter<TIn, TOut>), outParam.Type);
Then the call is just:
using Raiqub.ImplicitGenerics;
services.AddAdapter(OutParam.Of<Int32ToFloatAdapter>())
Take another example of a method that down-cast the values of a dictionary:
public static IDictionary<TKey, TOther> DownCastValues<TKey, TValue, TOther>(
this IDictionary<TKey, TValue> dictionary)
where TKey : notnull
where TValue : class
where TOther : class, TValue =>
dictionary
.Select(it => (it.Key, Value: (TOther)it.Value))
.ToDictionary(it => it.Key, it => it.Value);
And to call this method:
IDictionary<string, string> result = dict.DownCastValues<string, object, string>();
But, using the provided ITypeParam<out T> interface:
public static IDictionary<TKey, TOther> DownCastValues<TKey, TValue, TOther>(
this IDictionary<TKey, TValue> dictionary,
ITypeParam<TOther> typeParam)
where TKey : notnull
where TValue : class
where TOther : class, TValue =>
dictionary
.Select(it => (it.Key, Value: (TOther)it.Value))
.ToDictionary(it => it.Key, it => it.Value);
Finally, the call is:
using Raiqub.ImplicitGenerics;
IDictionary<string, string> result = dict.DownCastValues(TypeParam.Of<string>());
You can find more examples going to the test project: ParamTest.cs.
If something is not working for you or if you think that the source file should change, feel free to create an issue or Pull Request. I will be happy to discuss and potentially integrate your ideas!
See the LICENSE file for details.