https://github.com/lamparter/ideconstructable
Exposes the IDeconstructable interface and its source generator to allow "deconstructing" classes into their fields.
https://github.com/lamparter/ideconstructable
constructor csharp dependencyinjection dotnet riverside riversidevalley roslyn roslyn-generator source-generator
Last synced: 4 months ago
JSON representation
Exposes the IDeconstructable interface and its source generator to allow "deconstructing" classes into their fields.
- Host: GitHub
- URL: https://github.com/lamparter/ideconstructable
- Owner: Lamparter
- License: mit
- Created: 2025-04-02T13:54:25.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-04-02T14:09:14.000Z (6 months ago)
- Last Synced: 2025-04-02T15:23:56.700Z (6 months ago)
- Topics: constructor, csharp, dependencyinjection, dotnet, riverside, riversidevalley, roslyn, roslyn-generator, source-generator
- Homepage: https://nuget.org/packages/Riverside.DeconstructableInterfaces
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [`Riverside.DeconstructableInterfaces`](https://nuget.org/packages/Riverside.DeconstructableInterfaces)
> Exposes the IDeconstructable interface and its source generator to allow "deconstructing" classes into their fields.
---
This library exposes an `IDeconstructable` interface that lets you 'deconstruct' classes into their fields.
For example:
```cs
public partial class MyClass : IDeconstructable
{
public MyClass()
{
Field1 = string.Empty;
Field2 = "Hello World";
}
public string Field1;
public string Field2;
}
```This will then source generate a method in `MyClass`:
```cs
///
public partial class MyClass : IDeconstructable
{
public (string, string) Deconstruct()
=> (Field1, Field2)
}
```This has many use cases, including:
- JSON serialisation so containing serialisation types can be split
- This might be useful when moving from reflection-based serialisation with anonymous types to source-generated serialisation
- Reducing dependency on class references or 'saving' a class's volatile data into a constant variable..and just for fun! This project is a learning experience for making simple incremental source generators.
I came up with the idea for this when migrating [Taskie](https://github.com/shef3r/Taskie) to modern UWP with .NET 9 (on Native AOT).