[英]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,我试图找出阻止我执行以下操作的当前限制:
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核心类库。
Solution dir solutiondir\\
will contain a src\\
dir which contains a projectdir\\
( Solutiondir\\src\\projectdir
). 解决方案dir
solutiondir\\
将包含一个src\\
dir,其中包含一个projectdir\\
( Solutiondir\\src\\projectdir
)。
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
项目,以及其他框架目标。
From the same Projectdir
the same HelloWorldPCL will also be compiled for .net 4.6
project. 从同一个
Projectdir
,同样的HelloWorldPCL也将被编译为.net 4.6
项目。
From the same Projectdir
the same HelloWorldPCL will also be compiled for .net core
. 在同一个
Projectdir
,同样的HelloWorldPCL也将被编译为.net core
。
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.json
或HelloWorldCore.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. 当我在磁盘上引入
.xproj
和project.json
时,有一个工作的.csproj
项目,但是我们还没有将它们添加到解决方案本身,它们只是坐在Real Project Sources旁边的磁盘上,我得到了一个Nuget包恢复错误,只能通过删除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: 我已经构建了一个“破损”的代码库,它证明了使用包含
.csproj
和project.json
的文件夹的不可能性,然后尝试构建csproj
和project.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.