在 Visual Studio 2017 中使用 C# 和 .NET Core SDK 來建置 .NET Standard 程式庫Build a .NET Standard library with C# and the .NET Core SDK in Visual Studio 2017

「類別庫」會定義應用程式所呼叫的類型和方法。A class library defines types and methods that are called by an application. 以 .NET Standard 2.0 為目標的類別庫,允許支援該 .NET Standard 版本的任何 .NET 實作呼叫您的類別庫。A class library that targets the .NET Standard 2.0 allows your library to be called by any .NET implementation that supports that version of the .NET Standard. 當您完成類別庫時,您可以決定要將它散發為協力廠商元件,還是要將它併入作為一或多個應用程式隨附的元件。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 bundled component with one or more applications.

注意

如需 .NET 標準版本與所支援平台的清單,請參閱 .NET 標準For a list of the .NET Standard versions and the platforms they support, see .NET Standard.

在本主題中,您將建立含有單一字串處理方法的簡單公用程式類別庫。In this topic, you'll create a simple utility library that contains a single string-handling method. 您將它實作為擴充方法,以便可以如同 String 類別的成員一般來進行呼叫。You'll implement it as an extension method so that you can call it as if it were a member of the String class.

建立類別庫方案Creating a class library solution

請開始建立類別庫專案和其相關專案的方案。Start by creating a solution for your class library project and its related projects. Visual Studio 方案只能做為一個或多個專案的容器。A Visual Studio Solution just serves as a container for one or more projects. 建立方案:To create the solution:

  1. 在 Visual Studio 功能表列上,選擇 [檔案] > [新增] > [專案]。On the Visual Studio menu bar, choose File > New > Project.

  2. [新增專案] 對話方塊中,展開 [其他專案類型] 節點,並選取 [Visual Studio 方案]In the New Project dialog, expand the Other Project Types node, and select Visual Studio Solutions. 將方案命名為 "ClassLibraryProjects",然後選取 [確定] 按鈕。Name the solution "ClassLibraryProjects" and select the OK button.

    [新增專案] 對話方塊,其中醒目提示新的空白方案

建立類別庫專案Creating the class library project

建立您的類別庫專案:Create your class library project:

  1. 在方案總管 中,以滑鼠右鍵按一下 ClassLibraryProjects 方案檔,然後從內容功能表中,選取 [新增] > [新增專案]。In Solution Explorer, right-click on the ClassLibraryProjects solution file and from the context menu, select Add > New Project.

  2. [新增專案] 對話方塊中,展開 [Visual C#] 節點,然後選取後面跟著 [類別庫 (.NET Standard)] 專案範本的 [.NET Standard] 節點。In the Add New Project dialog, expand the Visual C# node, then select the .NET Standard node followed by the Class Library (.NET Standard) project template. [名稱] 文字方塊中,輸入 "StringLibrary" 作為專案名稱。In the Name text box, enter "StringLibrary" as the name of the project. 選取 [確定] 以建立類別庫專案。Select OK to create the class library project.

    新增程式庫專案對話方塊

    然後在 Visual Studio 開發環境中開啟程式碼視窗。The code window then opens in the Visual Studio development environment.

    Visual Studio 應用程式視窗顯示預設的類別庫範本程式碼

  3. 請檢查以確定程式庫以正確的 .NET Standard 版本為目標。Check to make sure that our library targets the correct version of the .NET Standard. 方案總管視窗中,以滑鼠右鍵按一下程式庫專案,然後選取 [屬性]。Right-click on the library project in the Solution Explorer windows, then select Properties. [目標 Framework] 文字方塊顯示我們的目標是 .NET Standard 2.0。The Target Framework text box shows that we're targeting .NET Standard 2.0.

    類別庫的專案屬性

  4. 以下列程式碼取代程式碼視窗中的程式碼,並儲存檔案:Replace the code in the code window with the following code and save the file:

    using System;
    
    namespace UtilityLibraries
    {
        public static class StringLibrary
        {
            public static bool StartsWithUpper(this String str)
            {
                if (String.IsNullOrWhiteSpace(str))
                    return false;
    
                Char ch = str[0];
                return Char.IsUpper(ch);
            }
        }
    }
    

    類別庫 UtilityLibraries.StringLibrary 包含一個名為 StartsWithUpper 的方法,它會傳回 Boolean 值,指出目前的字串執行個體是否以大寫字元開頭。The class library, UtilityLibraries.StringLibrary, contains a method named StartsWithUpper, which returns a Boolean value that indicates whether the current string instance begins with an uppercase character. Unicode 標準會區別大寫和小寫字元。The Unicode standard distinguishes uppercase characters from lowercase characters. 如果是大寫字元,Char.IsUpper(Char) 方法會傳回 trueThe Char.IsUpper(Char) method returns true if a character is uppercase.

  5. 在功能表列中,選取 [組建] > [組建方案]。On the menu bar, select Build > Build Solution. 專案應該會編譯而不會發生錯誤。The project should compile without error.

    輸出窗格顯示組建成功

後續步驟Next step

您已成功組建類別庫。You've successfully built the library. 因為您尚未呼叫它的任何方法,所以它能否正常運作還不得而知。Because you haven't called any of its methods, you don't know whether it works as expected. 開發程式庫的下一個步驟是使用單元測試專案來測試它。The next step in developing your library is to test it by using a Unit Test Project.