How to stop VS from removing "unused" namespaces if they are actually used in preprocessor directives?
Asked Answered
S

1

8

I'm using Visual Studios Code Cleanup on Save to automatically clean my code when i save. It also removes unused namespaces. But now i noticed that it even removes namespaces when they are actually in use in preprocessor directives. What can i do to avoid it?

enter image description here

Code is here:

    private void RegisterTracking()
    {
#if DEBUG
        IResourceFilePostProcessor processor = null;
#else
        IResourceFilePostProcessor processor = new ResourceFilePostProcessor();
#endif
        For<ITrackingResourceFileService>().Add(new TrackingResourceFileService(processor));
    }    

Actually i think this is a visual studio bug because it should not be seen as unused.

Sashasashay answered 26/3, 2021 at 8:57 Comment(0)
V
7

Putting the using in a preprocessor directive seems to work.

#if !DEBUG
using xyz.EpiServer.Core.Tracking;
#endif
Valetudinarian answered 26/3, 2021 at 9:25 Comment(1)
Yes, this works so the namespace is not removed anymore. One drawback is that you need to know that issue and you have to fix it beforehand. You or someone else might walk into that trap somewhere else again. Would be great if visual studio would recognize that this namespace is used. You think it's a bug?Sashasashay

© 2022 - 2024 — McMap. All rights reserved.