RecordGenerator Analyzer + CodeFix!

Hiya all, I’ve written myself an analyzer with a codefix using Roslyn API 🙂

First off a demo of how it works:

When writing this analyzer and codefix (which is available under MIT license on GitHub: I’ve learned a whole lot about that API and how it works, and that knowledge will definitely help me down the road with GodMode, and also the analyzer itself is very useful.

I’ve been inspired by two similar projects: RecordConstructorGenerator and CSharpRecords — neither of which offered what I expected: mutator methods. That’s the added value of my codefix. The latter of two actually generates one mutator, but with a very weird syntax which doesn’t allow setting values to null.

I expect to add a warning diagnostic for cases where there already is a generated partial but outdated and offer to update with a codefix.

Also in future I hope to support the RecordAttribute properties that already are generated and honor them:

  • PrimaryCtorAccess (string) — default “public”, valid: “public”, “protected”, “private” (only on sealed). Sets access modifier on generated constructor.
  • GenerateMutators (bool) — default true, if false generation skips mutator methods.
  • GenerateCollectionMutators (bool) — default true, if false skips collection convenience mutators.

The last one is for a future addition – for an immutable collection property it could generate Add- method and maybe something else (probably just Add), e.g. for ImmutableArray<string> Names there’d be AddNames(params string[] newNames) mutator.


Leave a Reply

Your email address will not be published. Required fields are marked *