简体   繁体   中英

VS2017 C# default assemblies

For testing Semaphore Class was created sample:

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

But I am not able to compile Its giving me this error (CS0246)

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

I found solution ( "added reference 'System' again" ) to decide this problem from other issue , but the Question was born - which default assemblies in the C# standard project VS2017 include without additional references in project?

Because by .Net documentation Semaphore Class

Definition Namespace: System.Threading

Assemblies: System.Threading.dll, System.dll, netstandard.dll

But without ( "added reference 'System' again" ) the Thread Class and SemaphoreSlim Class worked normally (without compiler error CS0246), by .Net documentation for these classes:

Assemblies: System.Threading.dll, mscorlib.dll, netstandard.dll

Difference is System.dll vs mscorlib.dll only (It's expected), but when i try, at command prompt, again compile program by csc.exe and msbuild. The msbuild given the same result as VS IDE (It's expected) - compile error CS0246, but

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

are compiling without error and after that program is runned & normally worked.

If I right understood (.Net docs) by default "csc.exe" must include only mscorlib.dll, all other assemblies must be include explicitly by options "-lib" or/and "-reference"?

Why program normally was compiled without explicit reference to "System.Threading.dll, System.dll, netstandard.dll" ?

By help from PetSerAl , I could find answers on my questions:

  1. If you compile C# program by csc.exe, the default assemblies (without any explicitly specified by option "-reference" & "-lib") will be included:
    • mscorlib.dll (can be suppressed by option "-nostdlib")
    • *.dll from csc.rsp file (can be suppressed by option "-noconfig")

If you want compire without any implicit assemblies, use csc.exe <namefile>.cs -nostdlib -noconfig

  1. If you compile C# program by VS IDE or msbuild (VS IDE using "implicitly" msbuild), the default assemblies (without any explicitly specified by "Add reference" in IDE or by editing the corresponding ".csproj" file) will be include (in case using template VS2017 "Empty Project (.Net Framework)):

    • mscorlib.dll
    • System.Core.dll

You can check it by use "View/Object Browser" in VS IDE or by run msbuild with show build data

msbuild <NameProject>.proj -v:diag

where you can find information regarding assemblies, which was included in compilation process.

You can't simply suppress including of these assemblies in compilation process, It's demanding changing configuration files of msbuild, that must be doing very gently. Additional information you can find in other issue

If you will decide to examine configuration files of msbuild and how it work, very helpful will be receive working environment of msbuild (value of main variables). I can recommend simple project (you can write by notepad),

<?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>

if you run build it at command prompt msbuild test.csproj -t:ShowVar you can see value of main variables, which using msbuild (or you can run msbuild with show build data, as it write foregoing).

PS> And as you understood the Semaphore Class (System.dll) by default doesn't include, but namespaces System and System.Threading, and the most commonly using Classes are included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM