Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

c# - Duplicated using directives in multiple files

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...