56

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.

hultqvist
  • 17,451
  • 15
  • 64
  • 101
  • 4
    Do you guys really want to tell me that source-level sharing is not possible in C#? – Pavel Radzivilovsky Apr 26 '10 at 21:03
  • @Pavel: This problem is not really c# specific although I was looking for a c# specific solution here. I have already encountered situations where single files in a project were missing because they were located in another folder not sent with the rest of the files. – hultqvist Apr 27 '10 at 07:02
  • An exact answer to what you are looking for can be seen in jorgen's post below. – user912447 Apr 12 '13 at 17:08
  • I find I get a severe slowdown of visual studio if the same source file is opened in more than 1 instance of VS. – rollsch Oct 08 '18 at 04:58

7 Answers7

51

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.

Peter
  • 37,042
  • 39
  • 142
  • 198
  • can you please have a look at it here http://stackoverflow.com/questions/21301360/share-properties-from-child-app-to-base-project – Goofy Jan 23 '14 at 07:05
  • 1
    There are situations where supporting .DLL files are not available, and therefore the registered component must contain its dependent classes – Sean Munson Sep 21 '18 at 14:55
  • 3
    When you press "Add existing item", the "Add as link" option is available under dropdown of "Add" button in file selection dialog, in case you couldn't find it – p0358 May 18 '19 at 20:59
  • 1
    I have found this solution to be the best as opposed to using a Shared Project. Shared projects are very troublesome to design since you can't add resources (amongst numerous other limitations). The problem, however is that an extra DLL causes more deployment complexity and also obfuscation is a problem since all methods usually need to be made public. As a solution, you can use an obfuscation tool to merge the assembly and internalize methods. Now you have the best of both worlds where your code is obfuscated and is contained within the calling application. – Simple Guy Sep 27 '21 at 23:11
12

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.

Richard
  • 106,783
  • 21
  • 203
  • 265
7

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

Community
  • 1
  • 1
jorgen
  • 535
  • 1
  • 5
  • 12
5

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

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.

ThunderGr
  • 2,297
  • 29
  • 20
1

Create a new assembly that contains this class.

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

Mongus Pong
  • 11,337
  • 9
  • 44
  • 72
0

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.

kwesolowski
  • 695
  • 8
  • 18