• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C# 程序員參考--庫教程

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    C# 程序員 參考--庫教程: 本教程展示如何在 C# 中創建和使用庫。 教程 本教程演示如何使用必要的編譯器選項創建托管 DLL 文件,以及如何通過客戶程序使用該庫。 示例 本示例使用下列模塊: DLL 庫 ( Functions.dll ),從以下源文件生成: Factorial.cs :計
    C# 程序員參考--庫教程:

    本教程展示如何在 C# 中創建和使用庫。
    教程

    本教程演示如何使用必要的編譯器選項創建托管 DLL 文件,以及如何通過客戶程序使用該庫。

    示例

    本示例使用下列模塊:

    • DLL 庫 (Functions.dll),從以下源文件生成:

      Factorial.cs:計算并返回某數的階乘。

      DigitCounter.cs:計算所傳遞的字符串中的數字位數。

    • 使用該 DLL 的客戶程序 (FunctionTest.exe),從源文件 FunctionClient.cs 進行編譯。本程序顯示輸入參數的階乘。

    生成庫

    若要生成該庫,請將 Functions 作為當前目錄,并在命令提示處鍵入下列內容:

    csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs

    其中:

    /target:library指定輸入文件是個 DLL,而不是可執行文件(這還將阻止編譯器查找默認入口點)。
    /out:Functions.dll指定輸出文件名是 Functions.dll。通常,輸出名與命令行上的第一個 C# 源文件(本示例中為 Factorial)的名稱相同。
    Factorial.cs DigitCounter.cs指定要編譯并放入 DLL 的文件。

    編譯客戶程序

    若要編譯該程序,請將 FunctionTest 作為當前目錄,并在命令提示處鍵入下列內容:

    copy ..\Functions\Functions.dll .csc /out:FunctionTest.exe /R:Functions.DLL FunctionClient.cs 

    其中:

    /out:FunctionTest.exe指定輸出文件名為 FunctionTest.exe。
    /R:Functions.DLL指定解析引用時必須包括 Functions.DLL。該 DLL 必須位于當前目錄中,或具有完全限定的路徑。
    FunctionClient.cs指定客戶源代碼。

    這將創建可執行文件 FunctionTest.exe。

    文件 1:Factorial.cs

    以下代碼計算傳遞給此方法(與“庫”示例不同,請將此方法編譯到庫中)的整數的階乘。

    // Factorial.cs// compile with: /target:libraryusing System; // Declares a namespace. You need to package your libraries aclearcase/" target="_blank" >ccording// to their namespace so the .NET runtime can correctly load the classes.namespace Functions {     public class Factorial     { // The "Calc" static method calculates the factorial value for the// specified integer passed in:        public static int Calc(int i)         {             return((i <= 1) ? 1 : (i * Calc(i-1)));         }     }}

    文件 2:DigitCounter.cs

    以下代碼用于計算所傳遞的字符串中的數字字符個數:

    // DigitCounter.cs// compile with: /target:library /out:Functions.dll /reference:Factorial.dllusing System; // Declare the same namespace as the one in Factorial.cs. This simply // allows types to be added to the same namespace.namespace Functions {    public class DigitCount     {        // The NumberOfDigits static method calculates the number of        // digit characters in the passed string:        public static int NumberOfDigits(string theString)         {            int count = 0;             for ( int i = 0; i < theString.Length; i++ )             {                if ( Char.IsDigit(theString[i]) )                 {                    count++;                 }            }            return count;        }    }}

    文件 3:FunctionClient.cs

    生成庫后,其他程序便可以使用該庫。下面的客戶程序使用該庫中定義的類。本程序的基本功能是獲得每個命令行參數,并嘗試計算每個參數的階乘值。

    // FunctionClient.cs// compile with: /reference:Functions.dll,Factorial.dll /out:FunctionTest.exe// arguments: 3 5 10using System; // The following using directive makes the types defined in the Functions// namespace available in this compilation unit:using Functions;class FunctionClient {     public static void Main(string[] args)     {         Console.WriteLine("Function Client");         if ( args.Length == 0 )         {            Console.WriteLine("Usage: FunctionTest ... ");             return;         }         for ( int i = 0; i < args.Length; i++ )         {             int num = Int32.Parse(args[i]);             Console.WriteLine(               "The Digit Count for String [{0}] is [{1}]",                args[i],                // Invoke the NumberOfDigits static method in the               // DigitCount class:               DigitCount.NumberOfDigits(args[i]));             Console.WriteLine(               "The Factorial for [{0}] is [{1}]",                 num,               // Invoke the Calc static method in the Factorial class:                Factorial.Calc(num) );         }     } }

    輸出

    命令行 FunctionTest 3 5 10 使用程序 FunctionTest 計算 3、5 和 10 這三個整數的階乘。它還顯示每個參數的數字位數。

    運行后得到以下輸出:

        Function Client    The Digit Count for String [3] is [1]    The Factorial for [3] is [6]    The Digit Count for String [5] is [1]    The Factorial for [5] is [120]    The Digit Count for String [10] is [2]    The Factorial for [10] is [3628800]
    注意   為運行客戶可執行文件 (FunctionTest.exe),文件 Functions.DLL 必須位于當前目錄、某子目錄或“全局程序集緩存”中。有關更多信息,請參見全局程序集緩存。

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>