Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include source files of one project in another project?

I'm writing a class library in C#/.NET.

I need to compile it for two different frameworks:

  • 4.0 (using for debug myself)
  • 3.5 (using on client).

I want to have one set source files for the two projects, so I can make corrections in 1 copy of files and they are included in the other project automatically.

Now, if I even use "add existing item", VS 2010 creates copies; and I need to copy the latest versions every time.

I can't just change a target in project, because I'm using different versions of .dll references, and because ms vs has some quirks.

like image 931
Kirill Golikov Avatar asked Dec 09 '13 06:12

Kirill Golikov


3 Answers

You can use the Add as Link feature.

It goes like this:

  • Right-click where you need your (existing) file to be
  • "Add" -> "Existing Item"
  • Select your file then click the arrow on the "Add" button and choose "Add As link" (see screenshot below)
  • A link to the file will be added to the project instead of a copy

Screenshot

like image 177
Alex Avatar answered Nov 08 '22 22:11

Alex


One option is to use the Add As Link options mentioned by the others already, but you have more options than that:

  1. Portable Class Libraries are a special kind of project that allow you to specify which versions of .NET you want to target. The compiler then outputs the respective assemblies for you. The advantage of this technique is that you have one version of the source that compiles to both frameworks. The disadvantage is that you can't use features that the lowest common denominator .NET framework doesn't support.
  2. Source control branching & merging allow you to actually maintain 2 similar but different source files. You have one version that is the master version, then after applying a change to it, you merge it to your projects that produce the actual output. The advantage of this technique is that you can have two completely separate files, so you have a lot of freedom. The disadvantage is that you have two completely separate files, which can be hard to manage.
  3. Do better MsBuild trickery. Using the <choose>/<when> construct you can conditionally include references to a specific assembly version depending on a condition. The target framework version and other fancy settings can also be managed through MsBuild, but you can't always edit these through the UI. You can use this in combination with #if MY_CONSTANT to create conditionally compiled parts of the application
  4. You can create a .NET Assembly that you reference from both projects. You set the .NET version to the lowest version, 3.5 in your case. Visual Studio 2010 and later have multi-targeting support and you can mix and match .NET framework versions in one solution.

What kind of qircks are you running into, if you share (part of) your project files, we might be able to resolve those for you.

like image 31
jessehouwing Avatar answered Nov 08 '22 23:11

jessehouwing


When adding file to project choose "Add as link" not just add.

Add as link

like image 26
Jernej Novak Avatar answered Nov 08 '22 21:11

Jernej Novak