Link to C # code from several projects

I have a file .csfull of C # code. I continue to reuse in several projects.

Now I include it in these projects, copying and pasting the code into a new file in each project directory. This is the wrong way to do this.

What is the right way to do this?

The correct path should:

  • save the shared code in only one place on my computer.
  • and keep the connection fresh --- therefore, when the general code changes, every project knows about it, the next time I recompile this project.

By chance guessing, I would expect some sort of directive like using mycode = C:\common\mycode.cs, but I'm sure this is not a .NET way of doing things.

(I use C # 2010, .NET 4.0 and only compile this code locally on one computer.)

+5
source share
4 answers

Create a class library, add a file, create a project and refer to the DLL created from the assembly. Add a using statement to each file that will reference it. Also, if these are errors and the DLL is in the project, you right-click on the object → "Allow" and it will add you usage.

+6
source

Put the code in a separate class library project and specify it in other projects.

+8
source

.cs , "" " "

, , , , .

, , , MyClassLibrary, SomeOtherProject. dll, , - , Windows Phone. Me.Common - , , , .

, .

+6

, , , . https://github.com/CADbloke/CodeLinker

XML .csproj. , ...

<Compile Include="$(Codez)\z.Libraries\diff-match-patch\DiffMatchPatch\**\*.cs"
Exclude="NotThisOne.cs;**\NotThisFolderWith\This*.cs">
<Link>Libs\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

... # .

  • $(Codez) - Windows, .
  • *.* *.cs.
  • , Visual Studio , , , . . .
0

All Articles