【问题标题】:VS2017 C# default assembliesVS2017 C# 默认程序集
【发布时间】:2019-03-08 23:32:31
【问题描述】:

为测试 Semaphore Class 创建了示例:

using System;
using System.Threading;
class MyThread
{
    public Thread Thrd;
    static Semaphore sem = new Semaphore(2, 2);
....

但我无法编译它给我这个错误 (CS0246)

The type or namespace name 'Semaphore' could not be found 
(are you missing a using directive or an assembly reference?)

我从other issue 找到解决方案(“再次添加引用'System'”)来解决这个问题,但问题诞生了 - C# 标准项目 VS2017 中的默认程序集包括没有额外的项目中的引用?

因为 .Net 文档 Semaphore Class

定义命名空间:System.Threading

程序集:System.Threading.dll、System.dll、netstandard.dll

但没有(“再次添加引用'System'”)Thread Class和SemaphoreSlim Class工作正常(没有编译器错误CS0246) ,由这些类的 .Net 文档提供:

程序集:System.Threading.dll、mscorlib.dll、netstandard.dll

区别只是System.dll vs mscorlib.dll(这是预期的),但是当我尝试时,在命令提示符下,再次通过 csc.exe 和 msbuild 编译程序。 msbuild 给出与 VS IDE 相同的结果(这是预期的) - 编译错误 CS0246,但是

csc.exe sem.cs -out:sem.exe

正在无错误地编译,并且在该程序运行并正常工作之后。

如果我理解正确(.Net 文档),默认情况下“csc.exe”必须仅包含 mscorlib.dll,所有其他程序集必须通过选项“-lib”或/和“-reference”显式包含?

为什么程序正常编译时没有明确引用“System.Threading.dll, System.dll, netstandard.dll”?

【问题讨论】:

  • 你使用-noconfig吗?
  • @PetSerAl 你太棒了 :) 你是对的 -noconfig 选项(没有 csc.rsp 文件)它的工作原理(csc.exe)与 msbuild 相同
  • 但是对于“Semaphore Class, Thread Class and SemaphoreSlim Class”的其他程序集“System.Threading.dll, netstandard.dll”呢? csc.rsp 文件中没有这些 Dll?
  • 程序集取决于您的目标框架。 .NET Core 为 System.Threading.dll,.NET Framework 为 System.dll,.NET Standard 为 netstandard.dll。你只需要一个而不是全部三个。

标签: c# .net msbuild csc


【解决方案1】:

在PetSerAl 的帮助下,我可以找到问题的答案:

  1. 如果您通过 csc.exe 编译 C# 程序,将包含默认程序集(没有任何由选项“-reference”和“-lib”明确指定):
    • mscorlib.dll(可以通过选项“-nostdlib”抑制)
    • *.dll 来自 csc.rsp 文件(可以通过选项“-noconfig”抑制)

如果你想编译没有任何隐式程序集,使用 csc.exe <namefile>.cs -nostdlib -noconfig

  1. 如果您通过 VS IDE 或 msbuild(VS IDE 使用“隐式”msbuild)编译 C# 程序,默认程序集(没有通过 IDE 中的“添加引用”或通过编辑相应的“.csproj”文件明确指定)将被包含(如果使用模板VS2017“空项目(.Net Framework)):

    • mscorlib.dll
    • System.Core.dll

您可以通过在 VS IDE 中使用“视图/对象浏览器”或通过运行 msbuild 并显示构建数据来检查它

msbuild <NameProject>.proj -v:diag

您可以在哪里找到有关程序集的信息,这些信息包含在编译过程中。

你不能简单地禁止在编译过程中包含这些程序集,它要求更改 msbuild 的配置文件,必须非常温和地进行。您可以在other issue中找到更多信息

如果您决定检查 msbuild 的配置文件 以及它是如何工作的,那么接收 msbuild 的工作环境(主要变量的值)将非常有帮助。我可以推荐简单的项目(你可以用记事本写),

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Target Name="ShowVar">
  <Message Text="Configuration is $(Configuration)" />
  <Message Text="MSBuildToolsPath is $(MSBuildToolsPath)" />
  <Message Text="MSBuildExtensionsPath is $(MSBuildExtensionsPath)" />
  <Message Text="MSBuildToolsVersion is $(MSBuildToolsVersion)" />
  <Message Text="FrameworkPathOverride is $(FrameworkPathOverride)" />
  <Message Text="MSBuildUserExtensionsPath is $(MSBuildUserExtensionsPath)" />
  <Message Text="AdditionalExplicitAssemblyReferences is $(AdditionalExplicitAssemblyReferences)" />
  </Target>
</Project>

如果你在命令提示符下运行构建它msbuild test.csproj -t:ShowVar 您可以看到使用 msbuild 的主要变量的值(或者您可以使用显示构建数据运行 msbuild,如前所述)。

附注> 正如您所了解的,Semaphore Class (System.dll) 默认不包含,但名称空间 System 和 System.Threading 以及最常用的类都包含在内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多