AutoDeconstruct by Jason Bock
NuGet / site data
Details
Info
Name: AutoDeconstruct
Generates deconstruction methods for type definitions.
Author: Jason Bock
NuGet: https://www.nuget.org/packages/AutoDeconstruct
You can find more details at https://github.com/JasonBock/AutoDeconstruct/blob/main/docs/Overview.md
Original Readme
AutoDeconstruct
A library that automatically adds support for object deconstruction in C#.
Overview
The idea started with this tweet - specifically, this reply. I thought...how automatic can I make object deconstruction in C#? That's what this source generator is all about.
Read the overview document for further details.
About
Automatically add deconstruct for all types in an assembly
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references AutoDeconstruct
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoDeconstruct" Version="1.0.0" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of AutoDeconstruct in Program.cs
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var p = new Person();
p.FirstName = "Test";
p.LastName = "Ignat";
var (_, l, _ ) = p;
Console.WriteLine($"Last name is {l}");
This is the use of AutoDeconstruct in Person.cs
// See https://aka.ms/new-console-template for more information
using AutoDeconstruct;
public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Title { get; set; }
}
[NoAutoDeconstruct]
public class TestPerson
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Title { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- AutoDeconstruct.g.cs
#nullable enable
public static partial class PersonExtensions
{
public static void Deconstruct(this global::Person @self, out string? @firstName, out string? @lastName, out string? @title)
{
global::System.ArgumentNullException.ThrowIfNull(@self);
(@firstName, @lastName, @title) =
(@self.FirstName, @self.LastName, @self.Title);
}
}
Useful
Download Example (.NET C#)
Share AutoDeconstruct
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoDeconstruct
aaa
Category "Constructor" has the following generators:
2 AutoCtor