GenerateReadOnly 0.0.2
dotnet add package GenerateReadOnly --version 0.0.2
NuGet\Install-Package GenerateReadOnly -Version 0.0.2
<PackageReference Include="GenerateReadOnly" Version="0.0.2" />
<PackageVersion Include="GenerateReadOnly" Version="0.0.2" />
<PackageReference Include="GenerateReadOnly" />
paket add GenerateReadOnly --version 0.0.2
#r "nuget: GenerateReadOnly, 0.0.2"
#:package GenerateReadOnly@0.0.2
#addin nuget:?package=GenerateReadOnly&version=0.0.2
#tool nuget:?package=GenerateReadOnly&version=0.0.2
GenerateReadOnly
Overview
Roslyn generator that automatically sets up ReadOnly interfaces for annotated types.
Background
I found myself really wanting to have IReadOnly versions of my types for type-safety, similar to what C# provides with IReadOnlyList, IReadOnlyDictionary, ReadOnlySpan, etc., and similar to what is possible in C++ with const. I started setting up these types manually, but this quickly grew out of hand and was hard to manage.
This library aims to provide this functionality without too much extra boilerplate.
Usage
Simple cases, purely read only
To use this generator, simply annotate a type with readOnly.GenerateReadOnlyAttribute
and mark the type as partial, like so:
User code:
using readOnly;
[GenerateReadOnly]
public partial class Foo {
public int A { get; set; }
// This will be ignored
public int B(int c) {
...
}
}
This will generate a read only version of the type that your type will implement automatically. This new type exposes only getters for each property by default:
Generated code:
public partial interface IReadOnlyFoo {
int A { get; }
}
Const methods
If you have certain methods that don't change state, you can mark them as const similar to C++ by annotating them with readOnly.ConstAttribute
:
User code:
using readOnly;
[GenerateReadOnly]
public partial class Foo {
[Const]
public int B(int c) {
...
}
}
Now, the generated read only interface will also include this const method:
Generated code:
public partial interface IReadOnlyFoo {
int B(int c);
}
Note: This generator does not verify that const methods are pure.
Interplay with other readonly types
If your type refers to another type with its own IReadOnly interface, it will automatically use that instead in the generated type:
User code:
using readOnly;
[GenerateReadOnly]
public partial class Foo;
[GenerateReadOnly]
public partial class Bar {
public Foo Value { get; set; }
}
Generated code:
public partial interface IReadOnlyFoo;
public partial interface IReadOnlyBar {
IReadOnlyFoo Value { get; }
}
This works internally by generating overrides for these read only parent properties/methods that cast the mutable version to the read only version.
This also supports recursion:
User code:
using readOnly;
[GenerateReadOnly]
public partial class Node {
public Node Child { get; set; }
}
Generated code:
public partial interface IReadOnlyNode {
IReadOnlyNode Child { get; }
}
Forcing mutability
If you don't want a type to be swapped out for its IReadOnly counterpart, you can force it to be kept by annotating the type with readOnly.KeepMutableTypeAttribute
:
User code:
using readOnly;
[GenerateReadOnly]
public partial class Foo;
[GenerateReadOnly]
public partial class Bar {
[Const]
[KeepMutableType]
public Foo Value { get; set; }
[Const]
[KeepMutableType]
public Foo KeepInReturn() {
...
}
[Const]
public void KeepInParam([KeepMutableType] Foo value) {
...
}
}
Generated code:
public partial interface IReadOnlyFoo;
public partial interface IReadOnlyBar {
Foo Value { get; }
Foo KeepInReturn();
void KeepInParam(Foo value);
}
Inheritance
If a type with a read only interface inherits from another class with a read only interface, the read only interfaces will also have the same inheritance:
User code:
using readOnly;
[GenerateReadOnly]
public partial class Parent;
[GenerateReadOnly]
public partial class Child : Parent;
Generated code:
public partial interface IReadOnlyParent;
public partial interface IReadOnlyChild : IReadOnlyParent;
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
- Encoding.SpanExtensions (>= 1.0.0)
- Microsoft.CodeAnalysis.Common (>= 4.0.1)
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.