【发布时间】: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。你只需要一个而不是全部三个。