Skip to content

Repository files navigation

GenerateReadOnly

GitHubNugetNugetUnit testsCoverage Status

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.

So instead, I opted to solve this via a source generator, which keeps things much simpler.

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:

usingreadOnly;[GenerateReadOnly]publicpartialclassFoo{publicintA{get;set;}// This will be ignoredpublicintB(intc){
...}}

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:

publicpartialinterfaceIReadOnlyFoo{intA{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:

usingreadOnly;[GenerateReadOnly]publicpartialclassFoo{[Const]publicintB(intc){
...}}

Now, the generated read only interface will also include this const method:

Generated code:

publicpartialinterfaceIReadOnlyFoo{intB(intc);}

Note: This generator does not verify that const methods are pure.

Deep readonly

If your type refers to another type with its own IReadOnly interface, it will automatically use that instead in the generated type:

User code:

usingreadOnly;[GenerateReadOnly]publicpartialclassFoo;[GenerateReadOnly]publicpartialclassBar{publicFooValue{get;set;}}

Generated code:

publicpartialinterfaceIReadOnlyFoo;publicpartialinterfaceIReadOnlyBar{IReadOnlyFooValue{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:

usingreadOnly;[GenerateReadOnly]publicpartialclassNode{publicNodeParent{get;set;}publicList<Node>Children{get;}}

Generated code:

publicpartialinterfaceIReadOnlyNode{IReadOnlyNodeChild{get;}IReadOnlyList<IReadOnlyNode>Children{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:

usingreadOnly;[GenerateReadOnly]publicpartialclassFoo;[GenerateReadOnly]publicpartialclassBar{[Const][KeepMutableType]publicFooValue{get;set;}[Const][KeepMutableType]publicFooKeepInReturn(){
...}[Const]publicvoidKeepInParam([KeepMutableType]Foovalue){
...}}

Generated code:

publicpartialinterfaceIReadOnlyFoo;publicpartialinterfaceIReadOnlyBar{FooValue{get;}FooKeepInReturn();voidKeepInParam(Foovalue);}

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:

usingreadOnly;[GenerateReadOnly]publicpartialclassParent;[GenerateReadOnly]publicpartialclassChild:Parent;

Generated code:

publicpartialinterfaceIReadOnlyParent;publicpartialinterfaceIReadOnlyChild:IReadOnlyParent;

About

Roslyn generator that automatically sets up ReadOnly interfaces for annotated types.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Contributors

Languages