Is it possible to compile a single C# code file with the .NET Core Roslyn compiler?

前端 未结 4 792
甜味超标
甜味超标 2020-12-08 06:18

In old .NET we used to be able to run the csc compiler to compile a single .cs file or several files.

With .NET Core we have dotnet build t

4条回答
  •  盖世英雄少女心
    2020-12-08 06:55

    The compiler can be directly invoked using

    $ /usr/local/share/dotnet/sdk/2.0.0/Roslyn/RunCsc.sh
    

    However, this particular command may not be very helpful without a supporting project infrastructure because you'd need to pass in all .NET Core or .NET Standard reference assemblies in manually, which is normally handled by the SDK and NuGet. You'll get errors like this:

    $ /usr/local/share/dotnet/sdk/2.0.0/Roslyn/RunCsc.sh Program.cs
    Microsoft (R) Visual C# Compiler version 2.3.2.61921 (ad0efbb6)
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Program.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
    Program.cs(5,11): error CS0518: Predefined type 'System.Object' is not defined or imported
    Program.cs(7,26): error CS0518: Predefined type 'System.String' is not defined or imported
    Program.cs(7,16): error CS0518: Predefined type 'System.Void' is not defined or imported
    

提交回复
热议问题