Search code examples
c#precompiled

C# Switch enum at runtime


I have two projects which reference the same precompiled (C#) dll. This .dll contains a public enum in its namespace but: Both projects have to use different values in this enum.

Is there a possibility to define something like that? (Pseudo Code)

namespace module
{

    #if ConfigurationManager.AppSettings["project"] == "Extern"

    public enum Roles
    {
        Admin = 0,
        User = 1,
        Vip = 2
    }

    #else /* "Intern" */

    public enum Roles
    {
        Admin = 0,
        Staff = 1,
        User = 2
    }

    #end
}

Important: This code has to be precompiled, so a preprocessor directive is not possible.


Solution

  • This will be likely to confuse people but if you really want something in that direction, with a light modification, what about:

        public enum Roles
        {
            Admin = 0,
            Staff = 1,
            User = 1,
            Vip = 2,
            UserInternal = 2,
        }
    

    Note I had to rename one of your names.

    You can compare the integer value and even with the names with will be true (Roles.Staff == Roles.User is true.

    But like in the comments I would not highly recommend it.