Janknerics 0.0.16
dotnet add package Janknerics --version 0.0.16
NuGet\Install-Package Janknerics -Version 0.0.16
<PackageReference Include="Janknerics" Version="0.0.16"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Janknerics" Version="0.0.16" />
<PackageReference Include="Janknerics"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Janknerics --version 0.0.16
#r "nuget: Janknerics, 0.0.16"
#:package Janknerics@0.0.16
#addin nuget:?package=Janknerics&version=0.0.16
#tool nuget:?package=Janknerics&version=0.0.16
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 havepublic string P4;
(just likeMyModelClass
)
MyClassAllInts
- will not have a
P4
at all.
- will not have a
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 aMyModelClass
argument.- each Property in
MyClassDoubles
will be constructed (and/or initialized?) with the corresponding property inMyModelClass
- each Property in
MyClassAllInts
gets a constructor that takes aMyClassDoubles
argument.- each Property in
MyClassAllInts
will be constructed (and/or initialized?) with the corresponding property inMyClassDoubles
- each Property in
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Janknerics.Attributes (>= 0.0.15)
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 |