Search code examples
c#csc

Can csc.exe enforce conformance to a particular .net standard?


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


Solution

  • 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