These samples are buildable projects whose source is used for code snippets in the guide for writing cross-platform libraries. They can be built and run using the .NET Core toolchain, and are intended to simply demonstrate how to target and build NuGet packages for different targets. They aren't examples of how you'd build a real, feature-complete library.
To build/use any of these (using new-library as an example):
-
Open your favorite Command Line Interface (for example, Cmd.exe or Terminal).
-
Navigate to the top-level directory:
$ cd new-library -
Restore packages by typing the following:
$ dotnet restore
[!INCLUDEDotNet Restore Note]
-
To build and package the library as a NuGet package, type the following:
$ cd src/Library$ dotnet build$ dotnet packCheck out the
/bin/Debugdirectory to see the generated artifacts and.nupkg. -
To run unit tests (only applicable to
new-library):$ cd ../../test/LibraryTests$ dotnet test
And that's it!
The project under /new-library targets only .NET Core. For that reason,
this project is stored under the core project directory, so our build server builds it on
all platforms. Look under https://github.com/dotnet/samples/tree/master/core/libraries/new-library/.
It demonstrates two other things: how to use multiple projects, and how to test.
The sample showcases two libraries. The first, DependencyLibrary, contains functionality that the second, Library uses. The LibraryTests test project takes a dependency on Library to test that project.
IMPORTANT: This project requires Windows and the .NET Framework installed on your machine.
The project under /frameworks-library demonstrates how to use the CLI tools to build a library that targets the .NET Framework. It does so with a simple project targeting the .NET Framework 4.0. You could extend this to target additional versions of the .NET Framework by adding new build targets in the library.csproj project file. Check out the section on cross-compiling in the CLI libraries article for more information.
IMPORTANT: This project requires Windows and the .NET Framework installed on your machine.
The project under /net45-compat-library targets any of the following:
- .NET Framework 4.5.1 and above
- Windows Phone 8.1
- Universal Windows Platform
- Xamarin
- Mono
It uses the netstandard1.2 Target Framework Moniker introduced with the .NET Standard.
IMPORTANT: This project requires Windows and the .NET Framework installed on your machine.
The project under /net40-library targets the .NET Framework 4.0 and above. It also demonstrates how to use #if directives to multi-target for a .NET 4.0 target.
IMPORTANT: This project requires Windows and the .NET Framework installed on your machine.
The project under /pcl-library shows how to target a supported PCL Profile (for example, 344). It shows how to structure the Library.csproj file to allow for targeting a PCL. It also demonstrates how to use #if directives and how to define a preprocessor constant, PORTABLE259 in the Library.csproj file.