Duplicated using directives in multiple files

ε祈祈猫儿з 提交于 2019-12-18 08:27:13

问题


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?


回答1:


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.




回答2:


Simple answer. If your code files are using 20 common using statements, they should not be separated into 20 statements. Also, I wouldn't worry that much over that kind of code duplication. It's not hitting your runtime performance in any way, so it's not really related to DRY paradigm.



来源:https://stackoverflow.com/questions/24512894/duplicated-using-directives-in-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!