-2

I need a solution that works like an enum but with the possibilty to add and remove elements from it. We have a base solution that should offer some default values, which is common for all projects, but all the projects (which extends the base) should also be able to add and remove their own.

Example:

Base product:

public static class Constants
{
    public enum Food
    {
        Carrot,
        Meat, 
        Cheese
    }
}

Project extension:

public static class ProjectConstants
{
    // Remove meat from Food
    // Add Fish to Food
}

So right now I am considering just using a list:

    public static List<string> Food = new List<string>(new string[]
    {
        "Carrot",
        "Meat",
        "Cheese",
    });

This would allow me to add and remove elements in the projects. However, a list of strings feels more vunerable due to case-sensitive and spelling mistakes, and furthermore, I then miss the opportunity to use:

if (Meat < Carrot)

Is there any good solution? Someway to combine the strong and robust way of enum, with the generic way of adding and deleting elements?

Niklas
  • 131
  • 1
  • 10

1 Answers1

-2

This may help you Dynamic enum in C#.
The dictionary option is ruled out there because you lose compile time checks.

Edit: Changed the link to permalink.

David Lesner
  • 11
  • 2
  • 6