Share c# class source code between several projects

C#Visual StudioCode Sharing

C# Problem Overview


I have written a class that will handle internal logging in my application. Now I want to use this class in another new and totally separate project.

I could simply copy the file to the new project folder, but I would like to only have one copy of it to maintain so that all changes in it will apply to both projects over time.

I can use the "add existing file", but where do I put the file so that the next developer knows that it is required. I have once had a "shared" folder for this but one time that folder was not brought into the next development computer.

What is the best way to organize this so that it makes most sense for new maintainers and minimizes the risk for broken links in projects.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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: https://stackoverflow.com/questions/1116465/how-do-you-share-code-between-projects-solutions-in-visual-studio

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

Solution 4 - C#

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.

Solution 5 - C#

Create a new assembly that contains this class.

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

Solution 6 - C#

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.

Solution 7 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhultqvistView Question on Stackoverflow
Solution 1 - C#PeterView Answer on Stackoverflow
Solution 2 - C#RichardView Answer on Stackoverflow
Solution 3 - C#jorgenView Answer on Stackoverflow
Solution 4 - C#Justin NiessnerView Answer on Stackoverflow
Solution 5 - C#Mongus PongView Answer on Stackoverflow
Solution 6 - C#ThunderGrView Answer on Stackoverflow
Solution 7 - C#kwesolowskiView Answer on Stackoverflow