简体   繁体   中英

Preprocessor directives and unnecessary Using directives

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?

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
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM