隨風設計

          隨風設計

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks

          本教程展示如何在 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:library
          using System; 
          
          // Declares a namespace. You need to package your libraries according
          // 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.dll
          using 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 10
          using 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 必須位于當前目錄、某子目錄或“全局程序集緩存”中。有關更多信息,請參見全局程序集緩存

          posted on 2006-08-17 11:34 曹賢 閱讀(239) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 金寨县| 北安市| 福建省| 濮阳县| 新乡县| 来宾市| 康乐县| 锡林浩特市| 定州市| 邵武市| 金门县| 靖西县| 巴东县| 资阳市| 安仁县| 保山市| 若尔盖县| 当雄县| 通山县| 梁山县| 原平市| 山阴县| 普兰县| 全州县| 铜陵市| 隆林| 灵宝市| 寻乌县| 杂多县| 宜宾市| 法库县| 华安县| 夹江县| 女性| 巩义市| 隆德县| 德令哈市| 元氏县| 青冈县| 龙井市| 上虞市|