作法:使用命令列建立和使用組件 (C#)How to: Create and Use Assemblies Using the Command Line (C#)

組件又稱為動態連結程式庫 (DLL),會在執行階段連結到您的程式。An assembly, or a dynamic linking library (DLL), is linked to your program at run time. 為了示範如何建立和使用 DLL,請考慮下列案例:To demonstrate building and using a DLL, consider the following scenario:

  • MathLibrary.DLL:程式庫檔案,其中包含要在執行階段呼叫的方法。MathLibrary.DLL: The library file that contains the methods to be called at run time. 在此範例中,DLL 包含兩個方法︰AddMultiplyIn this example, the DLL contains two methods, Add and Multiply.

  • Add:包含 Add 方法的原始程式檔。Add: The source file that contains the method Add. 它會傳回其參數的總和。It returns the sum of its parameters. 包含 Add 方法的 AddClass 類別是命名空間 UtilityMethods 的成員。The class AddClass that contains the method Add is a member of the namespace UtilityMethods.

  • Mult:包含 Multiply 方法的原始程式碼。Mult: The source code that contains the method Multiply. 它會傳回其參數的乘積。It returns the product of its parameters. 包含 Multiply 方法的 MultiplyClass 類別也是命名空間 UtilityMethods 的成員。The class MultiplyClass that contains the method Multiply is also a member of the namespace UtilityMethods.

  • TestCode:包含 Main 方法的檔案。TestCode: The file that contains the Main method. 它使用 DLL 檔案中的方法來計算執行階段引數的總和與乘積。It uses the methods in the DLL file to calculate the sum and the product of the run-time arguments.

範例Example

// File: Add.cs   
namespace UtilityMethods  
{  
    public class AddClass   
    {  
        public static long Add(long i, long j)   
        {   
            return (i + j);  
        }  
    }  
}  
// File: Mult.cs  
namespace UtilityMethods   
{  
    public class MultiplyClass  
    {  
        public static long Multiply(long x, long y)   
        {  
            return (x * y);   
        }  
    }  
}  
// File: TestCode.cs  
  
using UtilityMethods;  
  
class TestCode  
{  
    static void Main(string[] args)   
    {  
        System.Console.WriteLine("Calling methods from MathLibrary.DLL:");  
  
        if (args.Length != 2)  
        {  
            System.Console.WriteLine("Usage: TestCode <num1> <num2>");  
            return;  
        }  
  
        long num1 = long.Parse(args[0]);  
        long num2 = long.Parse(args[1]);  
  
        long sum = AddClass.Add(num1, num2);  
        long product = MultiplyClass.Multiply(num1, num2);  
  
        System.Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);  
        System.Console.WriteLine("{0} * {1} = {2}", num1, num2, product);  
    }  
}  
/* Output (assuming 1234 and 5678 are entered as command-line arguments):  
    Calling methods from MathLibrary.DLL:  
    1234 + 5678 = 6912  
    1234 * 5678 = 7006652          
*/  

此檔案包含使用 DLL 方法 AddMultiply 的演算法。This file contains the algorithm that uses the DLL methods, Add and Multiply. 一開始會剖析從命令列輸入的引數 num1num2It starts with parsing the arguments entered from the command line, num1 and num2. 然後會使用 AddClass 類別中的 Add 方法計算總和,並使用 MultiplyClass 類別中的 Multiply 方法計算乘積。Then it calculates the sum by using the Add method on the AddClass class, and the product by using the Multiply method on the MultiplyClass class.

請注意,檔案開頭的 using 指示詞可讓您使用未限定的類別名稱,在編譯時期參考 DLL 方法,如下所示:Notice that the using directive at the beginning of the file enables you to use the unqualified class names to reference the DLL methods at compile time, as follows:

MultiplyClass.Multiply(num1, num2);  

否則,您必須使用完整名稱,如下所示:Otherwise, you have to use the fully qualified names, as follows:

UtilityMethods.MultiplyClass.Multiply(num1, num2);  

執行Execution

若要執行此程式,請輸入 EXE 檔案的名稱,後面接著兩個數字,如下所示:To run the program, enter the name of the EXE file, followed by two numbers, as follows:

TestCode 1234 5678

編譯程式碼Compiling the Code

若要建立 MathLibrary.DLL 檔案,請使用下列命令列編譯 AddMult 這兩個檔案。To build the file MathLibrary.DLL, compile the two files Add and Mult by using the following command line.

csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs  

/target:library 編譯器選項會指示編譯器輸出 DLL,而不是 EXE 檔案。The /target:library compiler option tells the compiler to output a DLL instead of an EXE file. 使用 /out 編譯器選項,後面接著檔案名稱,即可指定 DLL 檔案名稱。The /out compiler option followed by a file name is used to specify the DLL file name. 否則,編譯器會使用第一個檔案 (Add.cs) 作為 DLL 的名稱。Otherwise, the compiler uses the first file (Add.cs) as the name of the DLL.

若要建立可執行檔 TestCode.exe,請使用下列命令列:To build the executable file, TestCode.exe, use the following command line:

csc /out:TestCode.exe /reference:MathLibrary.DLL TestCode.cs  

/out 編譯器選項會指示編譯器輸出 EXE 檔案,並指定輸出檔案的名稱 (TestCode.exe)。The /out compiler option tells the compiler to output an EXE file and specifies the name of the output file (TestCode.exe). 這個編譯器選項是選擇性的。This compiler option is optional. /reference 編譯器選項會指定此程式所使用的一或多個 DLL 檔案。The /reference compiler option specifies the DLL file or files that this program uses. 如需詳細資訊,請參閱 /referenceFor more information, see /reference.

如需從命令列建立的詳細資訊,請參閱使用 csc.exe 建置命令列For more information about building from the command line, see Command-line Building With csc.exe.

另請參閱See also