0

Consider the following Enum:

public enum MyEnum
{
    TypeOneEnum0,
    TypeOneEnum1,
    TypeOneEnum2,
    .
    .
    .
    // From this line only TypeTwo enums are allowed,
    // otherwise a runtime error will be thrown from another place
    TypeTwoEnum0,
    TypeTwoEnum1,
    TypeTwoEnum2,
}

I also have two lists, one that holds the names of TypeOne enums, and another which holds names of TypeTwo enums.

I want to make sure at compile-time that other programmers won't add TypeOne enums below the comment that indicates that only TypeTwo enums should be inserted, and vice-versa.

While I can split it to two enums, this enum is used in so many places it will be a major headache.

This does not answer my question - It says "Is it possible to specify that a enum property can only have a range of values?" I don't need to have only a range of values, I simply want to be able to identify if two "types" are mixed in the enum definition.

What are my options?

TheBlueSky
  • 5,526
  • 7
  • 35
  • 65
  • I could be wrong, but it seems to me that you'd have to customise the compiler... – ADyson Sep 16 '20 at 12:56
  • I would look into writing custom roslyn analyzer or custom build task. Or you can just write an unittest which will do the same (if you have mandatory unit tests in you pipeline). – Guru Stron Sep 16 '20 at 12:56
  • I think you can't do that with the current compiler. You of-course can do some kind of check on the start of the program by calling a method (and using reflections). – Efthymios Kalyviotis Sep 16 '20 at 12:57
  • "This does not answer my question." because...? You already got the answer to your question: **this is not possible, unless you write your own compiler**. However for a runtime-check you may use the mentioned link. – MakePeaceGreatAgain Sep 16 '20 at 13:10
  • @HimBromBeere because the other question says "Is it possible to specify that a enum property can only have a range of values?" I don't need to have only a range of values, I simply want to be able to identify if two "types" are mixed in the enum definition. I'll appreciate re-opening my question to get other suggestions as well. – FriendOfFriend Sep 16 '20 at 13:12

0 Answers0