Source analyzer to add compiler enforced readonly parameters (including for primary constructors)
$ dotnet add package Retro.ReadOnlyParamsCode analyzer to enable readonly semantics on method parameters (including primary constructors).
Install using Nuget: https://www.nuget.org/packages/AutoExceptionHandler/
dotnet add package Retro.ReadOnlyParams
All you need to do is annotate a method parameter with [ReadOnly] and the compiler will not permit a reassignment.
using System;
using Retro.ReadOnlyParams.Annotations;
namespace Retro.ReadOnlyParams.Sample;
public class ExampleClass([ReadOnly] IInjectedService injectedService) {
public void SomeMethod(IInjectedService serviceInstance) {
injectedService = serviceInstance; // This will trigger a compiler error
}
}