2

This question refers specifically to CSC, not msbuild. I can not use msbuild for my project.

If I am using csc.exe from a .net5 SDK, can I use it to compile source code and enforce conformance to .net standard 2.0? As in, can I invoke csc.exe so that it is a compile error to use an API that is in the SDK for .net5 but not in the standard 2.0?

I want to ensure C# code that I have written conforms to .net standard 2.0 and that it will be shippable in a separate build system that also conforms to .NET Standard 2.0.

What would the options passed to csc.exe look like? Can csc.exe do this, if not what approach can you take to do this (without using msbuild)?

papplesharp
  • 272
  • 1
  • 9
  • 1
    [similar question](https://stackoverflow.com/questions/57484713/how-can-i-compile-a-net-standard-2-0-class-library-by-directly-invoking-the-c-s) was asked and judging by the lack of a resolution there, it's not looking good. –  Apr 29 '21 at 00:56
  • If you type `csc /?` from a command prompt, it tells you what you can and cannot do as far as switches and parameters it accepts. – Ken White Apr 29 '21 at 01:13
  • Anything you can do with compile in a VS project can be done with csc.exe. Sound like you just want to made net5 your target. The compiler uses the Target of the project as the compiler conformance. – jdweng Apr 29 '21 at 01:35
  • 1
    @jdweng: The poster wants to restrict to .Net 2.0, not 5. – Ken White Apr 29 '21 at 01:37
  • 1
    @KenWhite : So OP want to use the Net 5 library and target a Net 2.0 output. – jdweng Apr 29 '21 at 01:40
  • 1
    @jdweng: No, the OP wants to use the Net 5 csc.exe and restrict the target to only Net 2.0 output. That's the question being asked. – Ken White Apr 29 '21 at 01:44

1 Answers1

1

If I am using csc.exe from a .net5 SDK

csc.exe is not distributed by .net5 SDK, it's a component of Visual studio. it's installed in :

vs 2019

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe

old version used by net 40

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

.NET5 SDK is distributing csc.dll

You can pass the parameters of /reference of the netstandard.library : Example

/reference:E:\nuget\Packages\netstandard.library\2.0.3\build\netstandard2.0\ref\Microsoft.Win32.Primitives.dll 

/reference:E:\nuget\Packages\netstandard.library\2.0.3\build\netstandard2.0\ref\mscorlib.dll 

/reference:E:\nuget\Packages\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll 
    [..... other stuff ]

note: E:\nuget\packages is the path of my package configuration on my machine

M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • 1
    That's not true, you can download the .NET 5 SDK on the microsoft website and it includes csc: https://dotnet.microsoft.com/download/dotnet/5.0 – papplesharp Apr 29 '21 at 02:05
  • It looks like the .NET5 SDK also has an equivalent folder under 'packs\NETStandard.Library.Ref\2.1.0\ref\netstandard2.1' Referring to these libraries instead of the .NETCore5 dlls seem like a reasonable solution. .NET5 SDK does not seem to ship with Standard 2.0 reference dlls, only 2.1, but this seems like something I can find. I guess the answer really is to just use the reference DLLs for building and use the SDK dlls for distributing. – papplesharp Apr 29 '21 at 02:13
  • Can you point me to the path of csc for .NET 5 SDK on your machine?? – M.Hassan Apr 29 '21 at 02:13
  • @M. Hassan. On my machine dotnet has csc.dll @ C:\Program Files\dotnet\sdk\5.0.103\Roslyn\bincore\csc.dll If downloaded through the website as a package (not installer), you can install at any location. – papplesharp Apr 29 '21 at 02:14
  • Upvoting your answer, but you should remove the incorrect info about csc not being distributed by the .net5 sdk – papplesharp Apr 29 '21 at 02:16
  • 2
    You wrote "Only .NET5 SDK is distributing csc.dll which can not be used from commandline." That's not right. You can invoke the dll easily with "dotnet exec csc.dll" and it behaves just as the csc.exe – papplesharp Apr 29 '21 at 03:36