简体   繁体   English

多个平台项目和NuGet

[英]Multiple platform projects and NuGet

I have created a library that I need to be able to use in a Portable Class Library as well as a regular .NET application. 我创建了一个库,需要在可移植类库以及常规.NET应用程序中使用。 The way I accomplished this is by creating multiple solutions / projects that point to the same files: 我完成此操作的方式是通过创建指向相同文件的多个解决方案/项目:

Src/
    Dismissile.sln
    Dismissile.Portable.sln
    Dismissile/
        Dismissile.csproj
        Dismissile.Portable.csproj
        Class1.cs

Dismissile.sln includes Dismissile.csproj and targets .NET Framework 4.5.2 Dismissile.Portable.sln includes Dismissile.Portable.csproj and is a portable class library project that targets .NET Framework 4.5, Xamarin.Android and Xamarin.iOS. Dismissile.sln包括Dismissile.csproj并以.NET Framework 4.5.2为目标。Dismissile.Portable.sln包括Dismissile.Portable.csproj,并且是一个可移植的类库项目,目标是.NET Framework 4.5,Xamarin.Android和Xamarin.iOS。

Each project includes Class1.cs. 每个项目都包含Class1.cs。 I have created some conditional compilation symbols in each project such as PORTABLE and NET452. 我在每个项目中都创建了一些条件编译符号,例如PORTABLE和NET452。

This seems to work so fine but now I need to add a NuGet package for JSON.NET into my projects. 这似乎工作得很好,但是现在我需要在项目中添加JSON.NET的NuGet包。

If I add a NuGet package in my Portable project it will create the following in my packages.config: 如果我在可移植项目中添加NuGet程序包,它将在我的packages.config中创建以下内容:

<package id="Newtonsoft.Json" version="9.0.1" targetFramework="portable40-net40+sl5+win8+wp8+wpa81" />

However, if I add it in my other project it will create the following in packages.config: 但是,如果我在其他项目中添加它,它将在packages.config中创建以下内容:

<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net40" />

Is there any way to have a separate packages.config so each project includes the correct reference to my NuGet dependencies? 有没有办法拥有单独的packages.config,以便每个项目都包含对我的NuGet依赖项的正确引用?

The easiest solution for this scenario is to create distinct folders for every project and include the .cs -files via a link. 对于这种情况,最简单的解决方案是通过一个链接创建为每个项目不同的文件夹和包含的.cs -files。 (see MSDN - Add as Link ) (请参阅MSDN-添加为链接

Doing so enables you to reference different packages (and versions) for different platforms (projects), as you have distinct packages.config -files. 这样做使您可以为不同平台(项目)引用不同的软件包(和版本),因为您具有不同的packages.config -files。

Additionally, I should mention that you may encounter incompatible framework methods (such as Task.Factory.StartNew for NET40 and Task.Run for NET45), which can be solved in two different ways: 此外,我应该提到,您可能会遇到不兼容的框架方法(例如Task.Factory.StartNew的Task.Factory.StartNew和Task.Run的Task.Run),可以通过两种不同的方法解决:

Working with compiler constants 使用编译器常量

Define a framework contstant in your projects, and query them with preprocessor-directives , like: 在项目中定义一个恒定的框架,并使用preprocessor-directives查询它们,例如:

#if NET40
Task.Factory.StartNew(...)
#elif NET45
Task.Run(...)
#endif

Working with partial classes and methods 使用局部类和方法

Dismissile.NET40\\FooClass.cs Dismissile.NET40 \\ FooClass.cs

public partial class Foo
{
  public void Bar()
  {
    // ...
    this.RunTask(task);
  }

  partial void RunTask(Task task);
}

Dismissile.NET40\\Partial.FooClass.cs Dismissile.NET40 \\ Partial.FooClass.cs

public partial class Foo
{
  partial RunTask(Task task)
  {
    Task.Factory.StartNew(task);
  }
}

Dismissile.NET45\\Partial.FooClass.cs Dismissile.NET45 \\ Partial.FooClass.cs

public partial class Foo
{
  partial RunTask(Task task)
  {
    Task.Run(task);
  }
}

Summary 摘要

My preferred solution is partial classes and methods , as they don't mess up your source with nestings (due to #if -statements), stepping while debugging is way more natural, and finally this approach is more flexible for complicated scenarios. 我的首选解决方案是局部类和方法 ,因为它们不会使嵌套混乱(由于#if -statements),在调试时逐步执行更为自然,最后这种方法对于复杂的场景更灵活。

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

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