Share c# class source code between several projects

Solution 1:

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:

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:

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'.)

Solution 4:

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:

Create a new assembly that contains this class.

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