cja
cja

Reputation: 10026

How to avoid conditionally using the same namespace in many files

I have code like the following in many files. The code is the same in every file.

#if SOMETHING
    using namespace1;
#else
    using namespace2;
#endif

However I don't want to have to maintain these same five lines in many files.

Ideally I would like to do something like this in a separate file:

#if SOMETHING
    var namespace = namespace1;
#else
    var namespace = namespace2;
#endif

And then in all the other files:

using namespace

How can I achieve something like this?

Upvotes: 1

Views: 566

Answers (2)

Trevor Pilley
Trevor Pilley

Reputation: 16403

You're going about it the wrong way. Don't have 2 references, just have one to the test service and then in the production build of your application, change the URL in the app/web config to point to the production URL.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500953

I would avoid having the same code in different namespaces twice to start with.

Just generate the code once, but then wherever you create an instance of the web service, specify the URL you want to connect to. (You probably want to centralize that code, mind you.)

Look at the properties in the web service - I'm sure one of them will be the base URL. Just because it defaults to one particular URL (ideally something invalid, so that you never end up making accidental calls to the wrong environment due to not setting it) doesn't mean you can't change it programmatically at execution time.

Upvotes: 2

Related Questions