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
/out:Functions.dllFunctions.dll。通常,输出名与命令行上的第一个 C# 源文件(本示例中为 Factorial)的名称相同。
Factorial.cs DigitCounter.cs

编译客户程序

若要编译该程序,请将 FunctionTest 作为当前目录,并在命令提示处键入下列内容:

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

其中:

/out:FunctionTest.exeFunctionTest.exe。
/R:Functions.DLLFunctions.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 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.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 必须位于当前目录、某子目录或“全局程序集缓存”中。有关更多信息,请参见 全局程序集缓存

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924165/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924165/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值