How do you share code between projects/solutions i

2019-01-01 00:13发布

I have two solutions which have some common code, so I'd like to extract it out and share it between them. Furthermore, I'd like to be able to release that library independently because it might be useful to others.

  • What's the best way to do it with Visual Studio 2008?
  • Is a project present in more than one solution?
  • Do I have a separate solution for the separate piece of code?
  • Can a solution depend on another one?

16条回答
人气声优
2楼-- · 2019-01-01 00:20

A project can be referenced by multiple solutions.

Put your library or core code into one project, then reference that project in both solutions.

查看更多
骚的不知所云
3楼-- · 2019-01-01 00:20

You would simply create a separate Class Library project to contain the common code. It need not be part of any solution that uses it. Reference the class library from any project that needs it.

The only trick at all is that you will need to use a file reference to reference the project, since it will not be part of the solutions that refer to it. This means that the actual output assembly will have to be placed in a location that can be accessed by anyone building a project that references it. This can be done by placing the assembly on a share, for instance.

查看更多
初与友歌
4楼-- · 2019-01-01 00:21

It is a good idea to create a dll class library that contain all common functionality. Each solution can reference this dll indepently regardless of other solutions.

Infact , this is how our sources are organized in my work (and I believe in many other places).

By the way , Solution can't explicitly depend on another solution.

查看更多
浮光初槿花落
5楼-- · 2019-01-01 00:25

You can include a project in more than one solution. I don't think a project has a concept of which solution it's part of. However, another alternative is to make the first solution build to some well-known place, and reference the compiled binaries. This has the disadvantage that you'll need to do a bit of work if you want to reference different versions based on whether you're building in release or debug configurations.

I don't believe you can make one solution actually depend on another, but you can perform your automated builds in an appropriate order via custom scripts. Basically treat your common library as if it were another third party dependency like NUnit etc.

查看更多
呛了眼睛熬了心
6楼-- · 2019-01-01 00:26

You can host an internal NuGet Server and share the common libraries that will be shared in other projects internally and externally.

Further on this read

查看更多
浅入江南
7楼-- · 2019-01-01 00:28

You can "link" a code file between two projects. Right click your project, choose Add -> Existing item, and then click the down arrow next to the Add button:

Screengrab

In my experience linking is simpler than creating a library. Linked code results in a single executable with a single version.

查看更多
登录 后发表回答