Janknerics 0.0.16

dotnet add package Janknerics --version 0.0.16
                    
NuGet\Install-Package Janknerics -Version 0.0.16
                    
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="Janknerics" Version="0.0.16">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Janknerics" Version="0.0.16" />
                    
Directory.Packages.props
<PackageReference Include="Janknerics">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Janknerics --version 0.0.16
                    
#r "nuget: Janknerics, 0.0.16"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Janknerics@0.0.16
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Janknerics&version=0.0.16
                    
Install as a Cake Addin
#tool nuget:?package=Janknerics&version=0.0.16
                    
Install as a Cake Tool

Janknerics

An alternative to built in generics. With a couple bonus features.

Basics

Instead of writing:

class MyGeneric<T1, T2, T3, T4>
{
    public T1 P1;
    public required T2 P2 { get; init; }
    public T3 P3;
    public T4 P4;
}

class MyClassFloats: MyGeneric<float, float, int, string>;
class MyClassDoubles: MyGeneric<double, double, long, string>;
class MyClassAllInts : MyGeneric<int, int, int, int>;

Which is simple enough when there are not many types but...

With Janknerics we can write:

partial class MyClassDoubles;
partial class MyClassAllInts;

class MyModelClass
{
    [Jankneric(typeof(MyClassDoubles), NewType = typeof(double))]
    [Jankneric(typeof(MyClassAllInts), NewType = typeof(int))]
    public float P1;
    
    [Jankneric(typeof(MyClassDoubles), NewType = typeof(double))]
    [Jankneric(typeof(MyClassAllInts), NewType = typeof(int))]
    public float P2 { get; }
    
    [Jankneric(typeof(MyClassDoubles), NewType = typeof(long))]
    [Jankneric(typeof(MyClassAllInts), NewType = typeof(int))]
    public int P3;
    
    [Jankneric(typeof(MyClassDoubles), NewType = typeof(string))]
    [Jankneric(typeof(MyClassAllInts), NewType = typeof(int))]
    public string P4;
}

This takes more lines of code BUT it's much easier to follow when the number of type parameters gets large.

Bonus Features

Since Janknerics is a Source Generator we can make it a little more useful than plain Generics too.

Properties can be omitted entirely or "passed through" exactly.

In the example above for the P4 field we could have written:

    [Jankneric(typeof(MyClassDoubles))]
    public string P4;

In this case:

  • MyClassDoubles will have
    • public string P4; (just like MyModelClass)
  • MyClassAllInts
    • will not have a P4 at all.

Constructors can be optionally autogenerated

In the example above the attributes on MyModelClass could have been specified this way:

[JanknericConstructor(typeof(MyClassDoubles))]
[JanknericConstructor(typeof(MyClassAllInts))]
class MyModelClass

In this case:

  • MyClassDoubles gets a constructor that takes a MyModelClass argument.
    • each Property in MyClassDoubles will be constructed (and/or initialized?) with the corresponding property in MyModelClass
  • MyClassAllInts gets a constructor that takes a MyClassDoubles argument.
    • each Property in MyClassAllInts will be constructed (and/or initialized?) with the corresponding property in MyClassDoubles
There are no supported framework assets in this package.

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
0.0.16 196 5/16/2025