Tedd.ReadOnlyStructGenerator
1.0.8
dotnet add package Tedd.ReadOnlyStructGenerator --version 1.0.8
NuGet\Install-Package Tedd.ReadOnlyStructGenerator -Version 1.0.8
<PackageReference Include="Tedd.ReadOnlyStructGenerator" Version="1.0.8" />
<PackageVersion Include="Tedd.ReadOnlyStructGenerator" Version="1.0.8" />
<PackageReference Include="Tedd.ReadOnlyStructGenerator" />
paket add Tedd.ReadOnlyStructGenerator --version 1.0.8
#r "nuget: Tedd.ReadOnlyStructGenerator, 1.0.8"
#:package Tedd.ReadOnlyStructGenerator@1.0.8
#addin nuget:?package=Tedd.ReadOnlyStructGenerator&version=1.0.8
#tool nuget:?package=Tedd.ReadOnlyStructGenerator&version=1.0.8
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 | Versions 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. |
-
.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.