Tedd.ReadOnlyStructGenerator
1.0.6
dotnet add package Tedd.ReadOnlyStructGenerator --version 1.0.6
NuGet\Install-Package Tedd.ReadOnlyStructGenerator -Version 1.0.6
<PackageReference Include="Tedd.ReadOnlyStructGenerator" Version="1.0.6" />
paket add Tedd.ReadOnlyStructGenerator --version 1.0.6
#r "nuget: Tedd.ReadOnlyStructGenerator, 1.0.6"
// Install Tedd.ReadOnlyStructGenerator as a Cake Addin #addin nuget:?package=Tedd.ReadOnlyStructGenerator&version=1.0.6 // Install Tedd.ReadOnlyStructGenerator as a Cake Tool #tool nuget:?package=Tedd.ReadOnlyStructGenerator&version=1.0.6
Tedd.ReadOnlyStructGenerator
.NET Source Generator for duplicating structs to read-only copies.
Adding [GenerateReadOnlyStruct] to a struct like this
[GenerateReadOnlyStruct]
public struct Test
{
public float X;
public float Y;
public float Z;
}
will inject this code into your project
#define RO_GEN
public readonly struct ReadOnlyTest
{
public readonly float X;
public readonly float Y;
public readonly float Z;
public ReadOnlyTest(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public ReadOnlyTest(Test value)
{
X = value.X;
Y = value.Y;
Z = value.Z;
}
}
[GenerateReadOnlyStruct]
takes two boolean parameters:
- GenerateConstructor
- GenerateCopyConstructor
This allows you to prevent constructors from being generated, in case you already have that implemented.
[GenerateReadOnlyStruct(GenerateConstructor: false, GenerateCopyConstructor: false)]
// Or just [GenerateReadOnlyStruct(false, false)]
public struct Test
{
public float X;
public float Y;
public float Z;
public Test(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}
will inject this code into your project
#define RO_GEN
public readonly struct ReadOnlyTest
{
public readonly float X;
public readonly float Y;
public readonly float Z;
public ReadOnlyTest(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
}
The constant RO_GEN is defined for every copy. It can be used for customizing code.
[GenerateReadOnlyStruct(false, false)]
public struct Test
{
public float X;
public float Y;
public float Z;
public Test(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public Vector3 Vector3 {
get => new Vector3(X, Y, Z);
#if !RO_GEN
// This section is only applicable for the mutable version of this struct
set {
X = value.X;
Z = value.Y;
Z = value.Z;
}
#endif
}
}
will essentially inject this code into your project
public readonly struct ReadOnlyTest
{
public readonly float X;
public readonly float Y;
public readonly float Z;
public ReadOnlyTest(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public Vector3 Vector3
{
get => new Vector3(X, Y, Z);
}
}
will inject this code into your project.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Include comments for attribute.