AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > .NET技术

C# 程序员参考--库教程

51自学网 http://www.51zixue.net

本教程展示如何在 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 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 必须位于当前目录、某子目录或“全局程序集缓存”中。有关更多信息,请参见全局程序集缓存

 

 

 
上一篇:C#&nbsp;程序员参考--版本控制教程  下一篇:C#&nbsp;程序员参考--属性教程