0

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.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
serious
  • 525
  • 10
  • 21
  • 1
    Just as a word of advice, all `enum`s are inherently static, so there is no need to "redeclare" them as such. http://stackoverflow.com/questions/4567868/troubles-declaring-static-enum-c-sharp – krillgar Jun 27 '14 at 12:19
  • 2
    Seems really misleading and I am not sure even if I would want to know if it is possible.. – Konrad Kokosa Jun 27 '14 at 12:20
  • 1
    I don't think the `static` in `public static enum` is doing anything for you or is not going to work - [troubles declaring static enum, C#](http://stackoverflow.com/questions/4567868/troubles-declaring-static-enum-c-sharp) – crashmstr Jun 27 '14 at 12:20
  • Yeah they don't need to be static :) (Edited Question) – serious Jun 27 '14 at 12:26
  • Why you cannot have two separate enums? What's the problem you're trying to solve? Maybe there are others solutions, than have two enums (and as @KonradKokosa points out, it's really misleading). – Alessandro D'Andria Jun 27 '14 at 12:57
  • The .dll is precompiled and does not know in which of the two projects it is running. – serious Jun 27 '14 at 13:06
  • 2
    You can't have a single enum that's different depending on what (or who) is using it. That doesn't make sense. – Kyle Jun 27 '14 at 13:44
  • @Kyle I tought so, but a little spark in me wanted to ask here if there is not "trick" to do such a thing. – serious Jun 27 '14 at 17:29

2 Answers2

1

You can have that, but they both can't be in the same namespace. You could have one be module.Extern.Roles, and the other be module.Intern.Roles, but if you want them in the same namespace, you'd have to combine the values into a single enum or change the name of one of them.

krillgar
  • 12,596
  • 6
  • 50
  • 86
1

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.

ZoolWay
  • 5,411
  • 6
  • 42
  • 76