Dotnet Core 简明教程

.NET Core - Create .NET Standard Library

一个类库定义类型和方法,这些类型和方法可以从任何应用程序调用。

A class library defines the types and methods that can be called from any application.

  1. A class library developed using .NET Core supports the .NET Standard Library, which allows your library to be called by any .NET platform that supports that version of the .NET Standard Library.

  2. When you finish your class library, you can decide whether you want to distribute it as a third-party component, or whether you want to include it as a component that is bundled with one or more applications.

让我们以在我们的控制台应用程序中添加一个类库项目作为开始,在解决方案资源管理器中右键单击 src 文件夹并且选择 Add → New Project…

Let us start by adding a class library project in our Console application; right-click on the src folder in Solution Explorer and select Add → New Project…

new project

Add New Project 对话框中选择 .NET Core 节点,然后选择类库(.NET Core)项目模板。

In the Add New Project dialog box, choose the .NET Core node, then choose the Class Library (.NET Core) project template.

在“名称”文本框中,输入“UtilityLibrary”作为项目名称,如下图所示。

In the Name text box, enter "UtilityLibrary" as the name of the project, as the following figure shows.

utilitylibrary

单击“确定”创建一个类库项目。在创建项目之后,我们添加一个新类。右键单击“解决方案资源管理器”中的 project 并选择 Add → Class…​

Click OK to create the class library project. Once the project is created, let us add a new class. Right-click on project in Solution Explorer and select Add → Class…​

class

在中间窗格中选择类并在“名称”和“字段”中输入StringLib.cs,然后单击 Add 。一旦添加了类,则替换StringLib.cs文件中的以下代码。

Select class in the middle pane and enter StringLib.cs in the name and field and then click Add. Once the class is added, then replace the following code in StringLib.cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace UtilityLibrary {
   public static class StringLib {
      public static bool StartsWithUpper(this String str) {
         if (String.IsNullOrWhiteSpace(str))
         return false;
         Char ch = str[0];
         return Char.IsUpper(ch);
      }
      public static bool StartsWithLower(this String str) {
         if (String.IsNullOrWhiteSpace(str))
         return false;
         Char ch = str[0];
         return Char.IsLower(ch);
      }
      public static bool StartsWithNumber(this String str) {
         if (String.IsNullOrWhiteSpace(str))
         return false;
         Char ch = str[0];
         return Char.IsNumber(ch);
      }
   }
}
  1. The class library, UtilityLibrary.StringLib, contains some methods like, StartsWithUpper, StartsWithLower, and StartsWithNumber which returns a Boolean value that indicates whether the current string instance begins with an uppercase, lowercase and number respectively.

  2. In .NET Core, the Char.IsUpper method returns true if a character is in uppercase, the Char.IsLower method returns true if a character is in lowercase, and similarly the Char.IsNumber method returns true if a character is a numeric.

  3. On the menu bar, choose Build, Build Solution. The project should compile without error.

  4. Our .NET Core console project doesn’t have access to our class library.

  5. Now to consume this class library we need to add reference of this class library in our console project.

为此,请展开FirstApp,然后右键单击“引用”,最后选择 Add Reference…

To do so, expand FirstApp and right-click on References and select Add Reference…

firstapp

在“引用管理器”对话框中,选择我们的类库项目UtilityLibrary,然后单击 OK

In the Reference Manager dialog box, select UtilityLibrary, our class library project, and then click OK.

现在让我们打开控制台项目的Program.cs文件,并将所有代码替换为以下代码。

Let us now open the Program.cs file of the console project and replace all of the code with the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UtilityLibrary;

namespace FirstApp {
   public class Program {
      public static void Main(string[] args) {
         int rows = Console.WindowHeight;
         Console.Clear();
         do {
            if (Console.CursorTop >= rows || Console.CursorTop == 0) {
               Console.Clear();
               Console.WriteLine("\nPress <Enter> only to exit; otherwise, enter a string and press <Enter>:\n");
            }
            string input = Console.ReadLine();

            if (String.IsNullOrEmpty(input)) break;
            Console.WriteLine("Input: {0} {1,30}: {2}\n", input, "Begins with uppercase? ",
            input.StartsWithUpper() ? "Yes" : "No");
         } while (true);
      }
   }
}

现在让我们运行应用程序,您将看到以下输出。

Let us now run your application and you will see the following output.

application

为了更好地理解,让我们在您的项目中使用您的类库的其他扩展方法。

For better understanding, let us make use of the other extension methods of your class library in your project.