1

I have an enum, NewEnum, which needs to be exactly equal to another enum, OldEnum. It seems I can't define an alias for an enum (or something equivalent) so I decided it would be easiest to simply re-define all the states.

Let's say OldEnum is defined as:

public enum OldEnum {
    a,
    b,
    c,
}

Currently, I have NewEnum defined as:

public enum NewEnum {
    a = OldEnum.a,
    b = OldEnum.b,
    c = OldEnum.c,
}

Since it's annoying to have to both update OldEnum and NewEnum when I want to add a new state, I was wondering if there was a way to dynamically have the same states in NewEnum as OldEnum.

Note that this doesn't need to be done at runtime (reflection shouldn't be used), just at compile-time

Wolf
  • 11
  • 1
  • 3
    You could use a Source Generator... Otherwise, no – canton7 Jul 19 '21 at 16:36
  • 1
    Even if you do generate the code for this, how would you be using it? I don't understand why you need two identical enums at all, just use the same one in both places. – DavidG Jul 19 '21 at 16:46
  • You know how to code. You know how to write an app that reads and writes text files. C# files are just text files; use a filesystemwatcher to detect changes in some.cs, read it and write someother.cs. Doesn't even need a FSW if you can tolerate it being a just-at-compile-time thing; run your app from the pre build event – Caius Jard Jul 19 '21 at 16:50
  • Or, if you don't really care that theyre enums, and classes with public int fields will do, make one inherit the other. See https://stackoverflow.com/questions/757684/enum-inheritance. Or just keep them in the same .cs file so theyre easy to edit.. It happens what.. once a month? Less? – Caius Jard Jul 19 '21 at 16:54
  • 2
    "It seems I can't define an alias for an enum": [Why not?](https://stackoverflow.com/a/9734393/3791245) `using NewEnum = OldEnum;` If this can't work, consider updating your question to explain why you have to redefine it. – Sean Skelly Jul 19 '21 at 16:58

0 Answers0