Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can use csc.exe to compile one cs file to a dll in .net framework...but how can i do this in dotnet core by dotnet cli?

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 ?

like image 485
yzd Avatar asked Dec 02 '25 07:12

yzd


2 Answers

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.

like image 76
Bruno Garcia Avatar answered Dec 04 '25 22:12

Bruno Garcia


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

like image 23
tym32167 Avatar answered Dec 04 '25 22:12

tym32167