The dynamic link library (DLL) is linked to the program at runtime. For instructions on how to generate and use DLL, see the following solution:
Mathlibrary. dll: Indicates the library file, which contains the methods to be called during running. In this example, dll contains two methods:AddAndMultiply.
Add. CS: Is the source file, which containsAdd (long I, long J)Method. This method returns the sum of parameters. IncludeAddMethodAddclassClass is namespaceUtilitymethods.
Mult. CS: Is the source file, which containsMultiply (long X, long y)Method. This method returns the product of parameters. IncludeMultiplyMethodMultiplyclassClass is also a namespaceUtilitymethods.
Testcode. CS: IncludeMainMethod file. It uses methods in the DLL file to calculate the sum and product of runtime parameters.
Add. CS
1 // File: Add.cs
2 namespace UtilityMethods
3 {
4 public class AddClass
5 {
6 public static long Add(long i, long j)
7 {
8 return (i + j);
9 }
10 }
11 }
Mult. CS
1 // File: Mult.cs
2 namespace UtilityMethods
3 {
4 public class MultiplyClass
5 {
6 public static long Multiply(long x, long y)
7 {
8 return (x * y);
9 }
10 }
11 }
Testcode. CS
1 // File: TestCode.cs
2
3 using UtilityMethods;
4
5 class TestCode
6 {
7 static void Main(string[] args)
8 {
9 System.Console.WriteLine("Calling methods from MathLibrary.DLL:");
10
11 if (args.Length != 2)
12 {
13 System.Console.WriteLine("Usage: TestCode <num1> <num2>");
14 return;
15 }
16
17 long num1 = long.Parse(args[0]);
18 long num2 = long.Parse(args[1]);
19
20 long sum = AddClass.Add(num1, num2);
21 long product = MultiplyClass.Multiply(num1, num2);
22
23 System.Console.WriteLine("{0} + {1} = {2}", num1, num2, sum);
24 System.Console.WriteLine("{0} * {1} = {2}", num1, num2, product);
25 }
26 }
Run cmd.
To run the program, enter the name of the EXE file, followed by two numbers:
Testcode 1234 5678
Output
Calling methods from MathLibrary.DLL:1234 + 5678 = 69121234 * 5678 = 7006652
Compile code
To generate a fileMathlibrary. dll, Use the following command line to compile the fileAdd. CSAnd filesMult. CS:
CSC/Target: Library/out: mathlibrary. DLL add. CS mult. CS
/Target: the Library compiler option notifies the compiler to output the DLL file instead of the EXE file. The/out compiler option followed by the file name is used to specify the DLL file name. Otherwise, the compiler uses the first file (Add. CS) As the DLL file name.
To generate an executable fileTestcode.exe, Use the following command line:
CSC/out: testcode.exe/reference: mathlibrary. dll testcode. CS
/OutThe compiler option notifies the compiler to output the EXE file and specify the output file name (Testcode.exe). This compiler option is optional. /Reference the compiler option to specify the DLL file used by the program.
Http://msdn.microsoft.com/zh-cn/library/3707x96z (vs.80). aspx