Home > Software engineering >  C# Auto generating Class fill Property from enum
C# Auto generating Class fill Property from enum

Time:02-18

Is there away to Auto-generate a full Class Property from an enum or a list? All of my Property will be bool and set up like this:

  private bool _stop;

    public bool Stop
    {
        get => _stop;
        set
        {
            if (_stop == value) return;
            _stop = value;
            StopChanged?.Invoke(value);
        }
    }  
    public event VariableHasChangedEventHandler StopChanged;

I have a long list of these to do, it is so redundant that I feel like there should be a way to Auto generate this code.

I have all the names in an enum and in a static class.

Is there any way of Auto generating that code from a enum or a static class?

I am using visual studio 2022

CodePudding user response:

Yep, that's exactly what source generators are for.

CodePudding user response:

Depending on what you want to achieve you could also consider using code snippets. Thus you could mitigate the pain in creating boilerplate code.

  • Related