1

I have some code that i need only to be run in a deployed environment (stage/test/production).

The code genereates some usings to other namespaces that appear at the top of the .cs file.

However, when i put this code inside my preprocessor directive #IF !DEBUG the usings at the top of the .cs file now claims

Using directive is unnecessary.

This feels dangerous. Since this is a multiple personnell project, i fear that someone is going to enter this file at some point and just auto-remove the usings since they're flagged as unnecessary.

My current solution is to just comment the code at the top of the file to not remove them.

So, how can i make my unnecessary usings directives non-unnecessary?

8
  • 1
    Are you using directives also in a #IF !DEBUG Commented Aug 29, 2019 at 11:52
  • Are you using directives also in a #IF !DEBUG Commented Aug 29, 2019 at 11:52
  • @MurrayFoxcroft no, is that a thing? I was not aware that was a thing. Commented Aug 29, 2019 at 11:53
  • I haven't tested, but it should be a thing. Does it work? Commented Aug 29, 2019 at 11:56
  • Either fully qualify the types used in the ifdef section, or place the using directives inside #ifdef's as well. Commented Aug 29, 2019 at 11:59

1 Answer 1

3

Place your usings a #IF too:

#IF !DEBUG using MySpecialStuff #endif public class MyClass { static void Main() { #IF !DEBUG var msc = new MySpecialClass(); #endif } } 

Or, fully qualify the class you need in the declaration.

public class MyClass { static void Main() { #IF !DEBUG var msc = new MySpecialStuff.MySpecialClass(); #endif } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.