如何在另一个项目中包含一个项目的源文件? [英] How to include source files of one project in another project?

查看:66
本文介绍了如何在另一个项目中包含一个项目的源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C#/.NET 编写一个类库.

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

我需要为两个不同的框架编译它:

I need to compile it for two different frameworks:

  • 4.0(用于自己调试)
  • 3.5(在客户端上使用).

我想为这两个项目提供一套源文件,这样我可以在一份文件中进行更正,然后它们会自动包含在另一个项目中.

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.

现在,如果我什至使用添加现有项目",VS 2010 都会创建副本;我每次都需要复制最新版本.

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

我不能只更改项目中的目标,因为我使用了不同版本的 .dll 引用,并且因为 ms vs 有一些怪癖.

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.

推荐答案

一种选择是使用其他人已经提到的 Add As Link 选项,但您还有更多选择:

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

  1. 可移植类库是一种特殊类型的项目,允许您指定要定位的 .NET 版本.然后编译器为您输出相应的程序集.这种技术的优点是您拥有一个可以编译到两个框架的源代码版本.缺点是您无法使用最低公分母 .NET 框架不支持的功能.
  2. 源代码控制分支和合并允许您实际维护 2 个相似但不同的源文件.您有一个作为主版本的版本,然后在对其应用更改后,将其合并到生成实际输出的项目中.这种技术的优点是您可以拥有两个完全独立的文件,因此您有很大的自由度.缺点是您有两个完全独立的文件,难以管理.
  3. 做更好的 MsBuild 技巧.使用 / 构造您可以根据条件有条件地包含对特定程序集版本的引用.目标框架版本和其他花哨的设置也可以通过 MsBuild 进行管理,但您不能总是通过 UI 编辑这些.您可以将其与 #if MY_CONSTANT 结合使用以创建应用程序的条件编译部分
  4. 您可以创建一个从两个项目中引用的 .NET 程序集.您将 .NET 版本设置为最低版本,在您的情况下为 3.5.Visual Studio 2010 及更高版本具有多目标支持,您可以在一个解决方案中混合和匹配 .NET 框架版本.
  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.

这篇关于如何在另一个项目中包含一个项目的源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆