FrostNova.Core.NativeAOT.Generators
1.2.2
dotnet add package FrostNova.Core.NativeAOT.Generators --version 1.2.2
NuGet\Install-Package FrostNova.Core.NativeAOT.Generators -Version 1.2.2
<PackageReference Include="FrostNova.Core.NativeAOT.Generators" Version="1.2.2" />
<PackageVersion Include="FrostNova.Core.NativeAOT.Generators" Version="1.2.2" />
<PackageReference Include="FrostNova.Core.NativeAOT.Generators" />
paket add FrostNova.Core.NativeAOT.Generators --version 1.2.2
#r "nuget: FrostNova.Core.NativeAOT.Generators, 1.2.2"
#:package FrostNova.Core.NativeAOT.Generators@1.2.2
#addin nuget:?package=FrostNova.Core.NativeAOT.Generators&version=1.2.2
#tool nuget:?package=FrostNova.Core.NativeAOT.Generators&version=1.2.2
FrostNova.Core.NativeAOT.Generators
A collection of high-performance C# Source Generators specifically optimized for NativeAOT environments. This package provides automatic implementation of deep cloning logic and strongly-typed JSON settings management without relying on heavy runtime reflection.
Features
- DeepCloneGenerator: Automatically generates efficient deep-copy logic for complex object graphs, including collections, dictionaries, and tuples.
- JsonSettingsGenerator: Generates boilerplate for JSON serialization and file I/O (Load/Save), designed to work seamlessly with
System.Text.Jsonsource generators.
1. DeepCloneGenerator
This generator implements the IDeepCloneable<T> interface and provides a DeepClone() method. It avoids reflection by generating explicit assignment code for every property.
How to use
- Mark your class as
partial. - Annotate it with the
[GenerateDeepClone]attribute.
using FrostNova.Core.NativeAOT;
[GenerateDeepClone]
public partial class PlayerProfile
{
public string Name { get; set; }
public List<int> Scores { get; set; }
public Dictionary<string, Item> Inventory { get; set; }
}
// Usage
var cloned = original.DeepClone();
Supported Types
- Collections: Automatically handles
List<T>,Dictionary<TKey, TValue>, and Arrays (including multi-dimensional). - Tuples: Supports recursive cloning of
ValueTupleelements. - Reference Preservation: Provides
CopyTo(target)to copy values into an existing instance, allowing for object reuse. - Ignore Property: Use
[DeepCloneIgnore]to skip specific properties.
2. JsonSettingsGenerator
Simplifies the creation of configuration and settings classes by generating methods for serialization and file I/O.
How to use
using FrostNova.Core.NativeAOT;
[GenerateJsonSettings]
public partial class AppSettings
{
public string Theme { get; set; } = "Dark";
public int FontSize { get; set; } = 14;
// Optional: Define a default save path
[JsonSettingPath]
public static string GetFilePath() => "settings.json";
}
// Usage
var settings = AppSettings.LoadSettings();
settings.Theme = "Light";
settings.SaveSettings();
NativeAOT & JsonSerializerContext
To ensure full NativeAOT compatibility, you can associate a JsonSerializerContext via the attribute:
[GenerateJsonSettings(typeof(MyJsonContext))]
public partial class AppSettings { ... }
Generated Methods
FromJson(string json)/ToJson(bool indented).LoadFromFile(string path)/SaveToFile(string path).LoadFromFileAsync(string path)/SaveToFileAsync(string path).- Collection Extensions: Generates
ToClassNameList()andToClassNameArray()for JSON strings.
Interface Support (for .NET 8+)
The generated classes automatically implement the IJsonSerializable<T> interface when targeting .NET 8.0 or greater. This allows you to handle various settings classes generically:
public void ProcessConfig<T>(string path) where T : IJsonSerializable<T>
{
T? settings = T.LoadFromFile(path);
if (settings != null)
{
Console.WriteLine(settings.ToJson());
}
}
Lifecycle Hooks
The JsonSettingsGenerator supports partial methods to inject custom logic:
partial void BeforeSerialize(): Called before the serialization process starts.partial void AfterDeserialize(): Called after the object is populated from JSON.
Requirements
- .NET 7.0 or higher (Optimized for .NET 8/9+ NativeAOT).
- C# 11.0+ (Required for
static virtualmembers andpartialmethod features).
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- FrostNova.Core.NativeAOT.Abstractions (>= 1.1.1)
- Microsoft.CodeAnalysis.Analyzers (>= 3.11.0)
- Microsoft.CodeAnalysis.CSharp (>= 4.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.