Share c# class source code between several projects

≯℡__Kan透↙ 提交于 2019-11-28 08:54:22

You could create a library project that has this class this way all you have to do is add a reference to that project.

If that is no option you could use "Right click -> add existing item -> Add as link" this way you only have one copy of the code but it can exist in multiple projects.

A class library to share is the best solution.

But you can add a file to a VS project as a link rather than copying. To do this use the drop down on the Add button of the add existing item dialogue.

jorgen

If you actually need to share the source code, for instance if you have a simultaneous 32-bit and a 64-bit build of the same assembly (which is some times required if you depend on native code), you can use the answer given in this thread: How do you share code between projects/solutions in Visual Studio?

(Look for '"link" a code file between two projects'.)

Break your logging code to a seperate assembly.

You can then include that assembly in the projects that should use that logger.

It keeps everything nice and clean and you'll only have to maintain one set of code rather than having to worry about each copy.

Create a new assembly that contains this class.

Then all your other projects can refer to this assembly and use the class within.

Put the class in a unique namespace, then add the file as a link to all projects you need to use it. Make sure you place a using statement for that unique namespace where you will be using the class, or you will have to fully qualify every use of it. For situations like this, I think this is much preferable than an assemply, since it does not generate needless cross-assemply references, for something that is part of your own code that you just wish to share between your projects.

You can also use use your versioning system to do this.

We usually create Directory with Solution and Main Project and then commit this directory to SVN. Then all shared libraries are linked using svn:external - so all Projects exists in same solution directory.

After commit anyone can checkout working solution with all external dependencies.

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