Tedd.ReadOnlyStructGenerator 1.0.8

dotnet add package Tedd.ReadOnlyStructGenerator --version 1.0.8
                    
NuGet\Install-Package Tedd.ReadOnlyStructGenerator -Version 1.0.8
                    
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.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tedd.ReadOnlyStructGenerator" Version="1.0.8" />
                    
Directory.Packages.props
<PackageReference Include="Tedd.ReadOnlyStructGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tedd.ReadOnlyStructGenerator --version 1.0.8
                    
#r "nuget: Tedd.ReadOnlyStructGenerator, 1.0.8"
                    
#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.
#:package Tedd.ReadOnlyStructGenerator@1.0.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tedd.ReadOnlyStructGenerator&version=1.0.8
                    
Install as a Cake Addin
#tool nuget:?package=Tedd.ReadOnlyStructGenerator&version=1.0.8
                    
Install as a Cake Tool

Tedd.ReadOnlyStructGenerator

.NET Source Generator for duplicating structs to read-only copies.

Adding [GenerateReadOnlyStruct] to a struct like this

namespace Example;

[GenerateReadOnlyStruct]
public struct Test
{
    public float X;
    public float Y;
    public float Z;
}

will inject this code into your project

#define RO_GEN

// This file was generated by Tedd.StructCopyGenerator
namespace Example
{
    [GenerateReadOnlyStruct]
    public readonly struct ReadOnlyTest
    {
        public readonly float X;
        public readonly float Y;
        public readonly float Z;
        public ReadOnlyTest(Test value)
        {
            X = value.X;
            Y = value.Y;
            Z = value.Z;
        }

        public ReadOnlyTest(float x, float y, float z)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
        }
    }
}

[GenerateReadOnlyStruct] takes two boolean parameters:

  • GenerateConstructor
  • GenerateCopyConstructor

This allows you to prevent constructors from being generated, in case you already have that implemented.

namespace Example;

[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

// This file was generated by Tedd.StructCopyGenerator
namespace Example
{
    [GenerateReadOnlyStruct(GenerateConstructor: false, GenerateCopyConstructor: false)]
    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.

namespace Example;

using System.Numerics;

[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;
            Y = value.Y;
            Z = value.Z;
        }
#endif
    }
}

will essentially inject this code into your project. Note that preprocessor directives such as #if !RO_GEN are retained in the generated code, but since #define RO_GEN is added to the top of the file, the enclosed code is excluded from compilation.

#define RO_GEN

// This file was generated by Tedd.StructCopyGenerator
namespace Example
{
    using System.Numerics;

    [GenerateReadOnlyStruct(false, false)]
    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);
#if !RO_GEN
            // This section is only applicable for the mutable version of this struct
            set {
                X = value.X;
                Y = value.Y;
                Z = value.Z;
            }
#endif
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in 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.8 95 6/22/2026
1.0.6 1,233 12/30/2022
1.0.5 1,162 12/30/2022
1.0.4 1,242 12/28/2022
1.0.3 1,225 12/27/2022
1.0.1 1,281 12/27/2022
1.0.0 1,085 12/26/2022

Include comments for attribute.