Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have got 5 C# files that have 20 using directives in common. I want to get rid of this code duplication, especially because these 20 using directives belong logically together. In C or C++ I would have created an extra header file containing these 20 include files. This extra header file acts as a layer then to include the 20 other files in one go.

Unfortunately I don't know how to do this in C#. Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
162 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can't do this in C# since C# does not have a preprocessor, and the C# compiler, although it supports some syntax that looks like preprocessor syntax, is handled by the compiler itself, and the compiler doesn't support include directives.

Also, if even one file actually requires 20 using statements it sounds to me as though you have put too much responsibility into one class. Perhaps this would be better served by refactoring and restructuring the codebase, thus reducing the number of required using directives instead?

Or, you could look at it from a different angle. You say that these 20 using directives all "belong logically together", why then 20 namespaces? Sounds to me as you've spread out some functionality into too many namespaces.

So either way I would look at restructuring the code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...