首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 類別庫建立與使用完整指南:從原始碼到 DLL 部署

     編輯:關於C語言

建立可重用的 .NET 類別庫 (DLL)

類別庫將功能封裝為獨立組件,供多個專案參考使用。本文示範命令列建立 DLL 與用戶端程式參考流程。

專案結構


/LibraryDemo
  ├── Factorial.cs       // 階乘計算
  ├── DigitCounter.cs    // 數字計數
  └── FunctionClient.cs  // 用戶端測試程式

步驟 1:建立類別庫原始碼

Factorial.cs


using System;

namespace MathUtils
{
    public class Factorial
    {
        public static int Calculate(int n)
        {
            return (n <= 1) ? 1 : n * Calculate(n - 1);
        }
    }
}

DigitCounter.cs


using System;

namespace MathUtils  // 相同命名空間,邏輯歸類
{
    public class DigitCounter
    {
        public static int CountDigits(string input)
        {
            int count = 0;
            foreach (char c in input)
                if (char.IsDigit(c)) count++;
            return count;
        }
    }
}

步驟 2:編譯為 DLL


// /target:library 指定輸出為 DLL
// /out:Functions.dll 指定輸出檔名
csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs

產出 Functions.dll,包含 MathUtils.Factorial 與 MathUtils.DigitCounter 兩個類別。

步驟 3:建立用戶端程式


using System;
using MathUtils;  // 導入類別庫命名空間

class FunctionClient
{
    static void Main(string[] args)
    {
        Console.WriteLine("Function Client Demo");
        
        if (args.Length == 0)
        {
            Console.WriteLine("用法:FunctionClient.exe <數字1> <數字2> ...");
            return;
        }
        
        foreach (string arg in args)
        {
            if (int.TryParse(arg, out int number))
            {
                int digitCount = DigitCounter.CountDigits(arg);
                int factorial = Factorial.Calculate(number);
                
                Console.WriteLine($"輸入:[{arg}]  數字個數:[{digitCount}]  階乘:[{factorial}]");
            }
            else
            {
                Console.WriteLine($"忽略非數字輸入:{arg}");
            }
        }
    }
}

步驟 4:編譯用戶端並參考 DLL


// /R:Functions.dll 參考類別庫 (若不在當前目錄需完整路徑)
// /out:FunctionClient.exe 指定輸出執行檔
csc /out:FunctionClient.exe /R:Functions.dll FunctionClient.cs

步驟 5:執行與驗證


> FunctionClient.exe 3 5 10
Function Client Demo
輸入:[3]  數字個數:[1]  階乘:[6]
輸入:[5]  數字個數:[1]  階乘:[120]
輸入:[10]  數字個數:[2]  階乘:[3628800]

DLL 搜尋順序 (執行階段)

  1. 應用程式目錄 (同 .exe 目錄)
  2. 子目錄 (依 probing 設定)
  3. 全域組件快取 (GAC) - 強式名稱組件
  4. CORPATH 環境變數指定路徑 (類似 Java CLASSPATH)

Visual Studio 整合開發流程

  1. 建立 Class Library 專案 → 撰寫程式碼 → Build 產出 DLL
  2. 建立 Console App 專案 → 參考 → 專案 或 瀏覽 選取 DLL
  3. using Namespace; 導入 → 撰寫呼叫程式碼 → 執行偵錯

進階:強式名稱與版本控制


// 產生金鑰組
sn -k MyKey.snk

// 組件資訊加入強式名稱 (AssemblyInfo.cs 或專案屬性)
[assembly: AssemblyKeyFile("MyKey.snk")]
[assembly: AssemblyVersion("1.0.0.0")]

// 編譯帶強式名稱的 DLL
csc /target:library /out:Functions.dll /keyfile:MyKey.snk Factorial.cs DigitCounter.cs

強式名稱組件可安裝至 GAC,支援多版本並存、版本原則導向。

本章小結

  • csc /target:library 建立 DLL,/out 指定檔名
  • 命名空間 (namespace) 組織類別,邏輯分組而非物理部署單位
  • 用戶端 csc /R:Library.dll 參考 DLL,using 導入命名空間
  • 執行階段依特定順序尋找 DLL,CORPATH 可擴展搜尋路徑
  • 生產環境建議:強式名稱、版本號、GAC 部署、NuGet 套件管理
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved