How to scaffold a Class Library targeting .NET Core?

1.4k views Asked by At

I know that using Visual Studio there is a template to create a Class Library targeting .NET Core, but I'm looking for a way to do it from the command line.

I know that using dotnet new you can create a new .NET core project, but it is a console application that has a few more dependencies than the class library created by Visual Studio. Is there a command or way to create a class library project from the command line?

Eg: enter image description here

2

There are 2 answers

0
Luis Palacios On BEST ANSWER

dotnet new now supports arguments one of them is --type -t where you can specify: console, web, lib and xunittest.

So now you can do:

dotnet new --type lib

Source: https://learn.microsoft.com/en-us/dotnet/articles/core/tools/dotnet-new

3
Nate Barbettini On

The Writing Libraries with .NET Core article has a lot of great detail that is non-Visual Studio specific.

There isn't currently a way to scaffold a new class library via dotnet new, but you can do it yourself. At a very minimum, you need a folder with a project.json file containing:

{
  "dependencies": {
    "NETStandard.Library": "1.5.0-rc2-24027"
  },
  "frameworks": {
    "netstandard1.3": { }
  },
  "version": "1.0.0"
}

When you compile using dotnet build, this will produce a class library targeting .NET Platform Standard 1.3.

It's my understanding that the dotnet-cli tool will be updated to include this template eventually.

Edit: As @svick pointed out, the Yeoman generator for ASP.NET Core can do this! You can scaffold a library with yo aspnet classlibrary in the latest version.