Raiqub.ImplicitGenerics 1.0.0

.NET 6.0 .NET Standard 1.0
dotnet add package Raiqub.ImplicitGenerics --version 1.0.0
NuGet\Install-Package Raiqub.ImplicitGenerics -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Raiqub.ImplicitGenerics" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Raiqub.ImplicitGenerics --version 1.0.0
#r "nuget: Raiqub.ImplicitGenerics, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Raiqub.ImplicitGenerics as a Cake Addin
#addin nuget:?package=Raiqub.ImplicitGenerics&version=1.0.0

// Install Raiqub.ImplicitGenerics as a Cake Tool
#tool nuget:?package=Raiqub.ImplicitGenerics&version=1.0.0

Implicit Generics Nuget

Provides 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 IOutParam<out T> interface and OutParam class to help developers create method parameters that lets the compiler infer the type of the generic parameter.

Compatibility

Raiqub.ImplicitGenerics is currently compatible with the following frameworks:

  • .NET Standard >= 1.0
  • .NET 6.0

Quickstart

  1. Reference the package <br/> Add the package to your project, for example via:

    Install-Package Raiqub.ImplicitGenerics
    
    --or--
    
    dotnet add package Raiqub.ImplicitGenerics
    
  2. Add using <br/> To use the types provided by this library add using on top of .cs file:

    using Raiqub.ImplicitGenerics
    

You should now be ready to use the library types on your project.

Guide

To understand the scenarios covered by this library, some examples are given.

Registering adapter on the dependency injector

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>())

Down casting dictionary values

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 IOutParam<out T> interface:

public static IDictionary<TKey, TOther> DownCastValues<TKey, TValue, TOther>(
    this IDictionary<TKey, TValue> dictionary,
    IOutParam<TOther> outParam)
    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(OutParam.Of<string>());

More examples

You can find more examples going to the test project: ParamTest.cs.

Contributing

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!

License

See the LICENSE file for details.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp1.0 netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard1.0 netstandard1.1 netstandard1.2 netstandard1.3 netstandard1.4 netstandard1.5 netstandard1.6 netstandard2.0 netstandard2.1
.NET Framework net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen30 tizen40 tizen60
Universal Windows Platform uap uap10.0
Windows Phone wp8 wp81 wpa81
Windows Store netcore netcore45 netcore451
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 88 7/26/2022