3

In .net framework i can use csc.exe to compile one cs file to a dll file.

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe /t:library /out:MyCScode.dll *.cs /debug /r:System.dll /r:System.Web.dll /r:System.Data.dll /r:System.Xml.dll

How can i do this in dotnetcore ?

yzd
  • 33
  • 5
  • Say goodbye to the old approach and learn MSBuild and dotnet CLI. – Lex Li Sep 19 '17 at 18:35
  • You can add few lines in your `~/.profile` to setup an alias on Unix-like OS and compile single file with `csc.dll`. See https://stackoverflow.com/a/56133028/863980 for details. – vulcan raven Dec 23 '19 at 23:04
  • Does this answer your question? [Is it possible to compile a single C# code file with the .NET Core Roslyn compiler?](https://stackoverflow.com/questions/46065777/is-it-possible-to-compile-a-single-c-sharp-code-file-with-the-net-core-roslyn-c) – vulcan raven Dec 23 '19 at 23:04

2 Answers2

5

At a minimum you'll need a .csproj file with this content:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

That is assuming you'll be targeting netstandard2.0 which would allow your library to run on .NET Framework 4.6.1, .NET Core 2.0 and any other runtime that implements the standard. And also have downloaded the .NET Core 2.0 SDK.

Unlike the old csproj, there's no need to add the individual .cs files to compile them.

So on a directory with such content:

  • Class1.cs
  • Class2.cs
  • Project.csproj (with the content mentioned above)

Simply running: dotnet build will output a dll at the bin folder as you'd expect.

It's worth mentioning that the .NET Core CLI have templates for ASP.NET Core, class library, xUnit test project, etc:

$ dotnet new classlib

Would give you the csproj and a first class file.

Bruno Garcia
  • 6,029
  • 3
  • 25
  • 38
1

You can use dotnet build to build your project for asp.net core

tym32167
  • 4,741
  • 2
  • 28
  • 32