简体   繁体   English

保持项目在多个.NET版本之间同步

[英]Keeping projects in sync across multiple .NET versions

I need to create some core libraries within my application which will be available in both .NET 3.5 and .NET 4.0. 我需要在我的应用程序中创建一些核心库,这些库将在.NET 3.5和.NET 4.0中都可用。 I'm happy creating multiple projects, creating the required defines and using #ifdef to control which code make it into which output assembly. 我很高兴创建多个项目,创建所需的定义并使用#ifdef来控制哪些代码使其成为哪个输出程序集。

However, what I would like to know is if there is a way of keeping those projects in sync? 但是,我想知道的是,是否有办法让这些项目保持同步? When I am developing under XNA, I have a Windows build and a Windows Phone build - and XNA injects a property into the project file called XnaCrossPlatformGroupID . 当我在XNA下开发时,我有一个Windows构建和一个Windows Phone构建 - 并且XNA将一个属性注入到名为XnaCrossPlatformGroupID的项目文件中。 What it does with that is enable Visual Studio to automagically make sure that when a file is added to a project, it is added to the corresponding project. 它的作用是使Visual Studio能够自动确保在将文件添加到项目时将其添加到相应的项目中。 So, for example, if I add a file called Foo.cs to the Windows copy of my project, then that same file is added to the Windows Phone copy of the project. 因此,例如,如果我将名为Foo.cs的文件添加到项目的Windows副本中,则会将相同的文件添加到项目的Windows Phone副本中。

Is there any way of replicating that sort of behaviour for a normal set of projects within Visual Studio? 有没有办法在Visual Studio中为一组正常的项目复制那种行为? I would prefer not to use build configurations, as I would like to be able to compile all targeted platforms in a single step, without resorting to tools outside of the IDE (something like TeamCity, for instance). 我宁愿不使用构建配置,因为我希望能够在一个步骤中编译所有目标平台,而无需借助IDE之外的工具(例如TeamCity)。 Or is there another method that allows a project to be built against multiple targets without peppering the solution with 20 different build configurations? 或者是否有另一种方法允许针对多个目标构建项目,而无需使用20种不同的构建配置来解决问题?

What I do is: 我所做的是:

  • have one "regular" csproj that I use for most of my dev, adding files etc - but remembering that really all files are "in" 有一个“正规”的csproj,我使用的大部分我开发的,添加文件等-但是记住, 真正的所有文件“在”
  • for the other csproj, use something like: 对于其他csproj,请使用以下内容:

     <ItemGroup> <Compile Include="..\\Foo\\**\\*.cs" /> </ItemGroup> 

(that is basically the same as "add as link", but without needing to be done on a per-file basis) (这与“添加为链接”基本相同,但不需要在每个文件的基础上完成)

This is a recursive include of all *.cs files, so everything is in. No file maintenance needed. 这是所有* .cs文件的递归包含,所以一切都在。不需要文件维护。


If you were trying to target multiple frameworks (rather than multiple versions ), then another option might be: use a Portable Class Library . 如果您尝试定位多个框架 (而不是多个版本 ),则另一个选项可能是:使用可移植类库 This is a single project that works on multiple frameworks, but is limited to the strict intersection of features from the platforms you target. 这是一个适用于多个框架的单个项目, 仅限于您所定位的平台的严格功能交集。 In VS2010 this is an add-in feature; 在VS2010中,这是一个插件功能; it is included by default in VS 11. 它默认包含在VS 11中。

What I do with my cross platform projects is to have different slns, each for one platform. 我对我的跨平台项目所做的是拥有不同的slns,每个slns用于一个平台。

The shared code lives in a common location and is added "as a link" into the correct csproj's. 共享代码位于一个公共位置,并作为链接添加到正确的csproj中。 This csproj also contain any platform specific files. 此csproj还包含任何特定于平台的文件。 If a "common code" file needs to be conditional based on current platform you can choose to replace that with two platform-specific files or put a #ifdef in it (depends on how much of it is different). 如果“公共代码”文件需要基于当前平台的条件,您可以选择用两个特定于平台的文件替换它,或者在其中放入#ifdef(取决于它有多少不同)。

Note that you can also add one solution to another inside Visual Studio. 请注意,您还可以在Visual Studio中向另一个添加一个解决方案。 This "combined" solution can be used for automated builds for all platforms. 这种“组合”解决方案可用于所有平台的自动构建。

Example: https://github.com/ananthonline/graffiti 示例: https//github.com/ananthonline/graffiti

This works great for me, YMMV. 对我来说这很有用,YMMV。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM