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

C# 建立與呼叫 DLL 完整教學 (2/3):命令列編譯與參考 DLL

     編輯:關於C語言

原始碼檔案結構

兩個類別檔 (MySwap.cs、MyMaxCD.cs) 置於同一命名空間 MyMethods,便於呼叫端單一 using 引用。

步驟 1:編譯為 DLL

# 進入 .NET Framework 目錄 (含 csc.exe)
csc /target:library /out:MyDLL.dll MySwap.cs MyMaxCD.cs
  • /target:library:輸出 DLL 而非 EXE。
  • /out:MyDLL.dll:指定輸出檔名 (省略則以第一個原始檔名為預設)。

步驟 2:撰寫呼叫端程式

// MyClient.cs
using System;
using MyMethods; // 引用 DLL 命名空間

class MyClient
{
    static void Main(string[] args)
    {
        if (args.Length != 2)
        {
            Console.WriteLine("Usage: MyClient  ");
            return;
        }
        long num1 = long.Parse(args[0]);
        long num2 = long.Parse(args[1]);

        SwapClass.Swap(ref num1, ref num2);
        Console.WriteLine("The result of swap is num1 = {0} and num2 = {1}", num1, num2);

        long maxcd = MaxCDClass.MaxCD(num1, num2);
        Console.WriteLine("The MaxCD of {0} and {1} is {2}", num1, num2, maxcd);
    }
}

步驟 3:編譯呼叫端並參考 DLL

csc /out:MyClient.exe /reference:MyDLL.dll MyClient.cs
  • /reference:MyDLL.dll:指定相依組件,編譯器會讀取其中的中繼資料。

步驟 4:執行驗證

MyClient 123 456

輸出應與預期一致 (見下篇)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved