Reputation: 461
I'm kinda new to c# and I'm trying to figure out a good solution for keeping common code somewhere that is used by several projects.
First I tried keeping it in a .cs file and add it to the projects that needs the code, but this COPIES the file meaning if I do any changes to the common code it will not be reflected in the various projects using this code.
Then I tried creating a class library instead and reference the DLL within the projects using the code. First of all I can't get this to work even though I am following the tutorials, Even when I reference the DLL I still cannot use it somehow, but even if this worked it still needs an extra DLL to be bundled with my app for some common code and that is not what I want.
Is there any way to keep common code that is REFERENCED without needing to ship a DLL w
Upvotes: 9
Views: 8451
Reputation: 25799
You can add a link to a common file:
Upvotes: 3
Reputation: 1374
In visual studio you can add a link to an existing item:
In the solution explorer, right click and choose Add -> Existing Item... Browse to the file and instead of clicking Add, choose Add As Link.
But I would advise to use a separate project for common code.
Upvotes: 0
Reputation: 13690
The best solution is to have a common library that you reference in each of your projects. However you say you don't want to do that as it requires bundling additional dlls, is that really that big a issue? You also mention that you can't get that to work, post the details of errors etc and we will be able to help.
Another option, assuming you are using Visual Studio is to link the common file into your project rather than add it. This will keep the common file in its current location and not move it into your project directory.
To do this, go to the add existing file dialog but on the 'Add' button click the down arrow and select 'Link' instead.
As mentioned above using a common library is the best solution so if bundling this is a unsurmountable issue you could look into using ilmerge. This will merge all your dlls/exes into a single file that you can then deploy.
Upvotes: 12
Reputation: 499002
You can add a linked file to a project - this does not create a copy but simply references it.
See the "Adding an Existing Item as a Link" section in the linked page:
To create a link to an existing item:
Upvotes: 0
Reputation:
You were almost there. You can add your existing file, but note that you can expand the "add" button and choose to link to it instead.
Upvotes: 1