如何仅使用 .NET Core SDK 直接调用 C# 编译器来编译 .NET Standard 2.0 类库? [英] How can I compile a .NET Standard 2.0 class library by directly invoking the C# complier using only the .NET Core SDK?

查看:27
本文介绍了如何仅使用 .NET Core SDK 直接调用 C# 编译器来编译 .NET Standard 2.0 类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2019 中编译 .NET Standard 2.0 类库项目时,会调用 C# 编译器 (csc.exe) 来编译项目.如果我检查构建日志,使用的命令行看起来像这样(为了可读性添加了换行符):

When compiling a .NET Standard 2.0 class library project in Visual Studio 2019, the C# compiler (csc.exe) gets invoked to compile the project. If I examine the build log, the command line used looks something like this (line breaks added for readability):

C:Program Files (x86)Microsoft Visual Studio2019ProfessionalMSBuildCurrentBinRoslyncsc.exe
/noconfig
/unsafe-
/checked-
/nowarn:1701,1702,1701,1702,2008
/nostdlib+
/errorreport:prompt
/warn:4
/define:TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0
/errorendlocation
/preferreduilang:en-US

<
A whole slew of /reference: switches that point to:
    %PROGRAMFILES%dotnetsdkNuGetFallbackFolder
etstandard.library
    2.0.3uild
etstandard2.0
ef
>

/debug+
/debug:portable
/filealign:512
/optimize-
/out:objDebug
etstandard2.0ClassLibrary1.dll
/target:library
/warnaserror-
/utf8output
/deterministic+
Class1.cs
/warnaserror+:NU1605

现在,假设我在一台没有任何 Visual Studio 版本的机器上.进一步假设我已将 .NET Core SDK 解压缩到该机器上的目录中.如何使用正确的引用程序集列表重新创建上述 /reference: 开关列表?我可以阅读的神奇清单在哪里可以让我组装该列表?

Now, assume that I am on a machine without any version of Visual Studio. Further assume that I have the .NET Core SDK unzipped into a directory on that machine. How can I recreate the above list of /reference: switches with the proper list of referenced assemblies? Where is the magic manifest that I can read that will let me assemble that list?

生成上述命令行的 .csproj 具有以下内容:

The .csproj from which the above command line was generated has the following contents:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

如您所见,项目本身中没有程序集引用标记,因此引用列表似乎只是位于上述目录中的文件列表.我想知道的是,该文件列表是如何组合在一起放置在 VS 的该目录中的,以及如何仅使用 .NET Core SDK 来模拟它.

As one can see, there are no assembly reference tags in the project itself, so the list of references appears to be just the list of files located in the aforementioned directory. What I want to know is how that list of files was assembled to be placed in that directory for VS, and how to mimic it using only the .NET Core SDK.

请注意,由于技术原因,我需要直接调用 C# 编译器;以任何方式使用 MSBuild 都不是解决此特定问题的可接受的解决方案.这意味着对 .csproj 文件使用 dotnet build 不是这个特定项目的选项.

Please note that there are technical reasons I need to invoke the C# compiler directly; using MSBuild in any way is not an acceptable solution to this particular problem. This means that using dotnet build against a .csproj file is not an option for this particular project.

推荐答案

在 .NET Core 中,依赖项是项目或 NuGet 包.有几个 NuGet 包被隐式引用.例如,如果您的目标是 netstandard2.0,您将引用 NETStandard.Library 的 1.6.1 版.

In .NET Core, dependencies are either projects or NuGet packages. There are a couple of NuGet packages which are referenced implicitly. For example, if your target netstandard2.0, you will reference version 1.6.1 of the NETStandard.Library.

所有这些规则都在 dotnet/sdk 存储库中编码.此存储库包含在 .NET CLI 和 Visual Studio 之间共享的代码,因为这两种产品都需要有关 .NET Core 构建过程的初步知识.例如,对 NETStandard.Library 1.6.1 的依赖被编码在 Microsoft.NET.Sdk.DefaultItems.targets

All these rules are encoded in the dotnet/sdk repository. This repository contains code which is shared across the .NET CLI and Visual Studio, because both products require inimate knowledge about the .NET Core build process. For example, the dependency on NETStandard.Library 1.6.1 is encoded in Microsoft.NET.Sdk.DefaultItems.targets

您正在寻找的魔法命令是 dotnet restore.这将解析所有引用(NuGet 和项目)并创建诸如 objprojects.assets.json 之类的文件,其中列出了项目的所有编译时程序集引用.还有其他文件,例如{projectname}.nuget.*,其中包含相关信息.

The magic command you are looking for is dotnet restore. This will resolve all references (NuGet and project) and create files such as objprojects.assets.json, which lists, all compile time assembly references for your project. There are other files, such as {projectname}.nuget.*, which contain related information.

因此,对于 Bazel,您可以采取以下几种方式:

So for Bazel, there are a couple of paths you could take:

  • 运行dotnet restore,并解析project.assets.json的输出.
  • 尝试使用 Bazel 的 dotnet/sdk 中的逻辑,就像 Visual Studio、MSBuild 和 .NET CLI 一样.这将很难,因为 dotnet/sdk 是用 C# 和 MSBuild 编写的,而 Bazel 可能没有这个概念.
  • 尝试在 Bazel 中复制 dotnet/sdk 中的逻辑.这将是一项艰巨的任务.
  • Run dotnet restore, and parse the output of project.assets.json.
  • Try consume the logic in dotnet/sdk from Bazel, just like Visual Studio, MSBuild and the .NET CLI do. This will be hard, since dotnet/sdk is written in C# and MSBuild, and Bazel probably has no notion of that.
  • Try to duplicate the logic in dotnet/sdk in Bazel. This will be an sisyphean task.

这篇关于如何仅使用 .NET Core SDK 直接调用 C# 编译器来编译 .NET Standard 2.0 类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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