简体   繁体   English

使用相同的C#源构建完整的.net,PCL和dotnet核心程序集的单个源项目?

[英]Create a single source project with full .net, PCL and dotnet core assemblies build from same C# source?

Update: This refers to an obsolete set of .net core tools built around project.json, this question and its answers are of very limited utility in current .net core toolsets like Visual Studio 2017+. 更新:这是指围绕project.json构建的一套过时的.net核心工具,这个问题及其答案在当前.net核心工具集(如Visual Studio 2017+)中的实用性非常有限。

Using Visual Studio 2015, update 3, I am trying to find out the current limitations that exist that would prevent me from doing the following: 使用Visual Studio 2015,更新3,我试图找出阻止我执行以下操作的当前限制:

  1. For a single C# codebase, compile a full .net framework version and a PCL version, and a .net core class library from the same sources. 对于单个C#代码库,从相同的源代码编译完整的.net框架版本和PCL版本以及.net核心类库。

  2. Solution dir solutiondir\\ will contain a src\\ dir which contains a projectdir\\ ( Solutiondir\\src\\projectdir ). 解决方案dir solutiondir\\将包含一个src\\ dir,其中包含一个projectdir\\Solutiondir\\src\\projectdir )。

  3. Projectdir will contain a HelloWorldPCL class library which is built as a classic .net PCL project, and with other framework targets as well. Projectdir将包含一个HelloWorldPCL类库,它构建为classic .net PCL项目,以及其他框架目标。

  4. From the same Projectdir the same HelloWorldPCL will also be compiled for .net 4.6 project. 从同一个Projectdir ,同样的HelloWorldPCL也将被编译为.net 4.6项目。

  5. From the same Projectdir the same HelloWorldPCL will also be compiled for .net core . 在同一个Projectdir ,同样的HelloWorldPCL也将被编译为.net core

  6. All of the output assemblies above will share access to a single set of C# code which may have some #if DOTNET and #if NET46 and #if PCL conditional defines to handle things which are not available in various frameworks/environments. 上面的所有输出程序集将共享对一组C#代码的访问,这些代码可能有一些#if DOTNET#if NET46以及#if PCL条件定义来处理各种框架/环境中不可用的内容。

So far I can do most of the above, but when I try to have the Projectdir contain a .net core artifact like project.json or HelloWorldCore.xproj , the IDE gives me many errors if I try to create separate .csproj and .xproj projects in the same folder, so that appears not to be the right way to go. 到目前为止,我可以完成上述大部分工作,但是当我尝试让Projectdir包含.net core工件(如project.jsonHelloWorldCore.xproj ,如果我尝试创建单独的.csproj.xproj ,IDE会给我带来很多错误。项目在同一个文件夹中,因此看起来不是正确的方法。

With a working .csproj project when I introduce an .xproj and a project.json on disk, but we have not added these to the solution itself, they are just sitting there on the disk beside the Real Project Sources, I get a Nuget package restore error, which can only be made to go away by deleting the project.json file, so clearly it is not possible to have a .csproj in a project directory and also have a project.json folder. 当我在磁盘上引入.xprojproject.json时,有一个工作的.csproj项目,但是我们还没有将它们添加到解决方案本身,它们只是坐在Real Project Sources旁边的磁盘上,我得到了一个N​​uget包恢复错误,只能通过删除project.json文件来消除,所以显然不可能在项目目录中有.csproj并且还有一个project.json文件夹。 One must choose between project.json mode ( .net core project style) and .csproj mode (classic C# project style). 必须在project.json模式( .net core项目样式)和.csproj模式(经典C#项目样式)之间进行选择。

I have built a "broken" codebase that demonstrates the impossibility of using a folder containing both a .csproj and a project.json and then trying to build the csproj and project.json project without them interfering: 我已经构建了一个“破损”的代码库,它证明了使用包含.csprojproject.json的文件夹的不可能性,然后尝试构建csprojproject.json项目而不会干扰它们:

https://bitbucket.org/wpostma/helloworldcoreandpcl https://bitbucket.org/wpostma/helloworldcoreandpcl

Question : is there ANY way to build a project from a single set of C# source files, and have as your targets, a full .net 4.6 assembly, and also a native .net core class library, and also a PCL assembly which might target any particular .net profile? 问题 :是否有任何方法可以从一组C#源文件构建项目,并且具有完整的.net 4.6程序集,以及本机.net core类库以及可能定位的PCL程序集作为目标任何特定的.net个人资料?

Objection: You may rightly ask "shouldn't you just use one PCL and have done with it, and forget targeting full .net or creating a separate .net core class library dll?". 异议:你可以正确地问:“你不应该只使用一个PCL并完成它,并忘记定位完整.net或创建一个单独的.net core类库dll?”。 Maybe if the code could be built once, from a single source, without any conditional defines, and without any differences in functionality and if the limitations imposed by a lowest-common-denominator PCL " netstandard " profile approach were sufficient, you could. 也许如果代码可以从单一来源构建一次,没有任何条件定义,并且没有任何功能上的差异,并且如果最低公分母PCL“ netstandard ”配置文件方法所施加的限制足够,那么你可以。 But what about the cases where that's not even possible? 但是那些甚至不可能的案例呢?

Do we have to have some elaborate build process where we either copy project files or move source code around, in order to achieve single .cs source file target multiple .net profile nerdvana? 我们是否必须有一些复杂的构建过程,我们要么复制项目文件,要么移动源代码,以实现单个.cs源文件目标多个.net profile nerdvana? Or am I overlooking alternative solutions? 还是我忽略了其他解决方案?

Update : It appears there is a problem with the Visual Studio IDE that prevents you from directly adding a reference to a class library in your solution, when you build it with a project.json instead of using a .csproj . 更新 :当您使用project.json而不是使用.csproj构建它时,Visual Studio IDE可能会出现问题,导致无法直接向解决方案中的类库添加引用。 However the solution below specified by Jon indicates that you should build your project, then build a nuget package with dotnet pack . 但是Jon指定的下面的解决方案表明你应该构建你的项目,然后使用dotnet pack构建一个nuget包。 If you look at my modified demo here you can the pack.cmd file contains the steps I used to pack up a nuget package, and then locally "publish" it to a file (local directory) nuget feed for my own personal use. 如果你在这里查看我修改过的demo,你可以将pack.cmd文件包含我用来打包nuget包的步骤,然后在本地“发布”到一个文件(本地目录)nuget feed供我个人使用。

I would try to go for a pure project.json approach. 我会尝试采用纯粹的project.json方法。 For example, here's a project.json file which will build a single package which supports netstandard1.0 , PCL profile 328, and .NET 4.6 - with each configuration defining its own symbols so you can have conditional code. 例如,这是一个project.json文件,它将构建一个支持netstandard1.0 ,PCL配置文件328和.NET 4.6的软件包 - 每个配置都定义了自己的符号,因此您可以拥有条件代码。

{
  "version": "1.0.0",

  "frameworks": {
    "net46": {
      "buildOptions": {
        "define": [ "NET46" ]
      }
    },

    "netstandard1.0": {
      "buildOptions": {
        "define": [ "CORE" ]
      },
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    },

    ".NETPortable,Version=v4.0,Profile=Profile328": {
      "buildOptions": {
        "define": [ "PCL" ]
      },
      "frameworkAssemblies": {
         "mscorlib": "",
         "System": "",
       }
    }
  }
}

Then just use dotnet pack to create the nuget package. 然后使用dotnet pack创建nuget包。 (After adding in all the other metadata etc.) (在添加所有其他元数据等之后)

You can use xproj and csproj together with different project.json files. 您可以将xproj和csproj与不同的project.json文件一起使用。 I did a blog post on the topic you should check out: http://stackify.com/using-both-xproj-and-csproj-with-net-core/ 我做了一篇关于你应该查看的主题的博客文章: http//stackify.com/using-both-xproj-and-csproj-with-net-core/

Essentially, you create a projectname.project.json that csproj picks up and then project.json that xproj uses. 基本上,你创建一个csproj拾取的projectname.project.json,然后创建xproj使用的project.json。 Seems to work! 似乎工作!

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

相关问题 .Net程序集反编译器,可将程序集转换为C#源代码 - .Net Assemblies Decompiler that Convert Assemblies into C# Source Code C# - 不能在同一源中的 WPF 项目中使用 .NET 框架项目中的类 - C# - Can't use classes from a .NET Framework project in a WPF project in the same Source 是否可以从使用 dotnet build 创建的 .NET 程序集中删除完整路径? - Is it possible to remove the full-paths from .NET assemblies created with dotnet build? 如何使用c#代码创建dotnet核心项目? - How to create dotnet core project using c# code? 从源代码使用C#创建文件 - Create file in C# from source code VC 2019 c# 如何包含在调试中编译的 DLL 的源代码:来自其他项目的完整以启用完整的源代码导航 - VC 2019 c# how include sources of DLL compiled in debug:full from other project to enable full source code navigation .Net Core dotnet run 没有运行最后修改的源代码 - .Net Core dotnet run is not running the last modified source code 如何在c#.net中创建动态程序集? - how to create dynamic assemblies in c# .net? C# ASP.Net 中基于开源论坛的项目 - Open Source Forum based project in C# ASP.Net 如何在C#中创建与源相同的目录复制文件 - How to create a same directory as source copying a file in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM