Tedd.ReadOnlyStructGenerator 1.0.6

dotnet add package Tedd.ReadOnlyStructGenerator --version 1.0.6
NuGet\Install-Package Tedd.ReadOnlyStructGenerator -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tedd.ReadOnlyStructGenerator" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tedd.ReadOnlyStructGenerator --version 1.0.6
#r "nuget: Tedd.ReadOnlyStructGenerator, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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.

There are no supported framework assets in this package.

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.

Version Downloads Last updated
1.0.6 993 12/30/2022
1.0.5 982 12/30/2022
1.0.4 1,020 12/28/2022
1.0.3 1,046 12/27/2022
1.0.1 1,098 12/27/2022
1.0.0 909 12/26/2022

Include comments for attribute.