Dotnet Core 简明教程

.NET Core - Adding References to Library

在本节中,我们将探讨如何向您的库添加引用。向库添加引用就像向其他项目(如控制台项目和 UWP 项目)添加引用一样。

In this chapter, we will discuss how to add references to your library. Adding references to library is like adding references to your other projects, like console project and UWP project.

uwp project

您现在可以看到,PCL 项目默认会带有一些引用。您也可以根据需要为应用程序添加其他引用。

You can now see that the PCL project has some references by default. You can also add other references as per your application need.

在 PCL 库中,您还可以看到 project.json 文件。

In the PCL library, you can also see the project.json file.

{
   "supports": {},
   "dependencies": {
      "NETStandard.Library": "1.6.0",
      "Microsoft.NETCore.Portable.Compatibility": "1.0.1"
   },
   "frameworks": {
      "netstandard1.3": {}
   }
}

向您的库添加引用的一个方法是直接在 project.json 文件中键入。正如您可以看到的,我们在依赖项部分中添加了一些引用,如下面的代码所示。

One method of adding references to your library is by typing it directly in the project.json file. As you can see that we have added some references under the dependencies section as shown in the following code.

{
   "supports": {},
   "dependencies": {
      "NETStandard.Library": "1.6.0",
      "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
      "System.Runtime.Serialization.Json": "4.0.3",
      "Microsoft.EntityFrameworkCore": "1.1.0"
   },
   "frameworks": {
      "netstandard1.3": {}
   }
}

我们现在保存此文件,您会看到已将引用添加到您的库中。

Let us now save this file and you will see that references are added to your library now.

references added

向您的库添加引用的另一个方法是使用 NuGet 包管理器。现在,让我们右键单击 StringLibrary (Portable) 项目并选择 Mange NuGet Packages…

The other method of adding references to your library is the NuGet Package Manager. Let us now right-click on the StringLibrary (Portable) project and select Mange NuGet Packages…

portable

在“浏览”选项卡上,您可以搜索任何 NuGet 包;就我们而言,我们希望添加“System.Runtime.Serialization.Primitives”包。

On the Browse tab, you can search any NuGet package; let us say we want to add “System.Runtime.Serialization.Primitives” package.

browse tab

单击 Install 按钮,它将显示以下屏幕。

Click the Install button, which will display the following screen.

press install

现在,单击 OK 按钮。

Now, click the OK button.

button

最后,单击 I Accept 按钮以开始安装此 NuGet 包。安装完成后,您会看到“System.Runtime.Serialization.Primitives”NuGet 包已添加到您的库中。

Finally, click the I Accept button to start installation of this NuGet package. Once installation is finished, then you will see that the “System.Runtime.Serialization.Primitives” NuGet package is added to your library.

installation