GoldMeridian.KeyGen
1.0.0
See the version list below for details.
dotnet add package GoldMeridian.KeyGen --version 1.0.0
NuGet\Install-Package GoldMeridian.KeyGen -Version 1.0.0
<PackageReference Include="GoldMeridian.KeyGen" Version="1.0.0" />
<PackageVersion Include="GoldMeridian.KeyGen" Version="1.0.0" />
<PackageReference Include="GoldMeridian.KeyGen" />
paket add GoldMeridian.KeyGen --version 1.0.0
#r "nuget: GoldMeridian.KeyGen, 1.0.0"
#:package GoldMeridian.KeyGen@1.0.0
#addin nuget:?package=GoldMeridian.KeyGen&version=1.0.0
#tool nuget:?package=GoldMeridian.KeyGen&version=1.0.0
keygen
keygen is a source generator for automatically relating objects to extended data through ConditionalWeakTables using C# 14 extension members.
It is a purely compile-time (dev-time) dependency and only serves to produce workable code that may be trivially consumed within your codebase and by other API consumers.
example
Given the class:
public sealed class MyClass
{
public int SomeData { get; }
}
You may want to relate additional information to an instance of MyClass. This is a common use-case and one officially supported by .NET; you would use a ConditionalWeakTable<TKey, TValue> to bind additional data to a reference.
ConditionalWeakTable uses WeakReferences to point to a given instance without capturing a direct reference which would thereby prevent garbage collection.
This source generator automates the scaffolding necessary to conveniently access additional data by generating extension members.
A piece of extended data may look like:
public sealed class MyClassData
{
public string MoreData { get; }
}
Annotating the class MyClassData with [ExtensionDataFor<MyClass>] would then allow the source generator to automatically generate the ConditionalWeakTable reference.
// Generates an extension property for MyClass named "Data" (omits the start of
// the string if it matches the type name).
[ExtensionDataFor<MyClass>]
public sealed class MyClassData;
// A name may also be manually specified. This would generate a propery named
// "TheData".
[ExtensionDataFor<MyClass>("TheData")]
public sealed class MyClassData;
// If the type doesn't start with the class name, then it'll just use the entire
// type name. In this case, "Data".
[ExtensionDataFor<MyClass>]
public sealed class Data;
// It's also possible and explicitly intended for you to be able to link a single
// class to multiple types:
[ExtensionDataFor<MyClass1>]
[ExtensionDataFor<MyClass2>]
public sealed class Data;
The shape of the generated code would look something like this:
#nullable enable
namespace Namespace.Of.Extension.Type;
// Not always `public`; matches the accessibility of the Extension class.
public static class KeyNameDataNameExtensions
{
extension(KeyName value)
{
public ValueName? PropertyName { get; set; }
}
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.Analyzers (>= 3.3.3)
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.