Goddess Guard Goddess Guard - 1 month ago 9
C# Question

Dynamically change namespace in C#

I want to do something like this. If DEBUG is defined then the namespace is Test, or the namespace is TestB. See the sample code bellow. Can I do that or you have better ideas to achieve this? Thanks in advance!

# if DEBUG
[SomekindofAttribute(Namespace = "Test")]
#endif
namespace TestB
{

public class Program
{}
}

Rob Rob
Answer

You can do this:

#if DEBUG
namespace TestB
#else
namespace Test
#endif
{
    public class Program { }
}

Though this looks like a very bad idea. Everything using Program would have to do the same preprocessor directives for their using declarations, too.

Comments