隨風(fēng)設(shè)計(jì)

          隨風(fēng)設(shè)計(jì)

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 0 評(píng)論 :: 0 Trackbacks

          本教程展示如何在 C# 中創(chuàng)建和使用庫。
          教程

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

          示例

          本示例使用下列模塊:

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

            Factorial.cs:計(jì)算并返回某數(shù)的階乘。

            DigitCounter.cs:計(jì)算所傳遞的字符串中的數(shù)字位數(shù)。

          • 使用該 DLL 的客戶程序 (FunctionTest.exe),從源文件 FunctionClient.cs 進(jìn)行編譯。本程序顯示輸入?yún)?shù)的階乘。

          生成庫

          若要生成該庫,請(qǐng)將 Functions 作為當(dāng)前目錄,并在命令提示處鍵入下列內(nèi)容:

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

          其中:

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

          編譯客戶程序

          若要編譯該程序,請(qǐng)將 FunctionTest 作為當(dāng)前目錄,并在命令提示處鍵入下列內(nèi)容:

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

          其中:

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

          這將創(chuàng)建可執(zhí)行文件 FunctionTest.exe

          文件 1:Factorial.cs

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

          // 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

          以下代碼用于計(jì)算所傳遞的字符串中的數(shù)字字符個(gè)數(shù):

          // 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

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

          // 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 計(jì)算 3、5 和 10 這三個(gè)整數(shù)的階乘。它還顯示每個(gè)參數(shù)的數(shù)字位數(shù)。

          運(yùn)行后得到以下輸出:

              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]
          注意???為運(yùn)行客戶可執(zhí)行文件 (FunctionTest.exe),文件 Functions.DLL 必須位于當(dāng)前目錄、某子目錄或“全局程序集緩存”中。有關(guān)更多信息,請(qǐng)參見全局程序集緩存

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 淮安市| 临沧市| 邢台市| 六安市| 闸北区| 花莲县| 枣阳市| 来宾市| 淳安县| 新干县| 金阳县| 涟源市| 渝北区| 深水埗区| 启东市| 阿巴嘎旗| 闸北区| 新平| 南华县| 石首市| 泽州县| 同心县| 双峰县| 乌拉特后旗| 双鸭山市| 阿坝| 嫩江县| 古丈县| 马尔康县| 崇明县| 莱阳市| 扶风县| 河东区| 朝阳市| 扶绥县| 雅安市| 佛冈县| 鹤山市| 万盛区| 潮安县| 五华县|