Myclient. CS: file containing the main method. It uses methods in the DLL file to calculate the sum and product of runtime parameters.
Source File File: Add. CS // Add two numbers using system; namespace mymethods {public class addclass {public static long add (long I, long J) {return (I + J );}}}
File: mult. CS // Multiply two numbers using system; namespace mymethods {public class multiplyclass {public static long multiply (long X, long y) {return (x * Y );}}}
File: myclient. CS // Calling methods from a DLL file using system; using mymethods; Class myclient {public static void main (string [] ARGs) {console. writeline ("calling methods from mylibrary. DLL: "); If (ARGs. length! = 2) {console. writeline ("Usage: myclient <num1> <num2>"); return;} Long num1 = long. parse (ARGs [0]); long num2 = long. parse (ARGs [1]); long sum = addclass. add (num1, num2); Long Product = multiplyclass. multiply (num1, num2); console. writeline ("the sum of {0} and {1} is {2}", num1, num2, sum); console. writeline ("the product of {0} and {1} is {2}", num1, num2, product );}}
This file contains the DLL MethodAddAndMultiplyOfAlgorithm. It first analyzes the parameters input from the command lineNum1AndNum2. Then useAddclassClassAddMethod calculation and, useMultiplyclassClassMultiplyMethod Calculation product.
Note thatUsingCommand allows you to reference the DLL method by using an undefined class name during compilation. For example:
Multiplyclass. Multiply (num1, num2 );
Otherwise, you must use a fully qualified name, for example:
Mymethods. multiplyclass. Multiply (num1, num2 );
Compile
To generate a fileMylibrary. dll, Use the following command line to compile the fileAdd. CSAnd filesMult. CS:
CSC/Target: Library/out: mylibrary. DLL add. CS mult. CS
/Target: LibraryThe compiler option notifies the compiler to output the DLL file instead of the EXE file. Followed by the file name/OutThe compiler option 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 fileMyclient.exe, Use the following command line:
CSC/out: myclient.exe/reference: mylibrary. dll myclient. CS
/OutThe compiler option notifies the compiler to output the EXE file and specify the output file name (Myclient.exe). This compiler option is optional./ReferenceThe compiler option specifies the DLL file used by the program.
Run
To run the program, enter the name of the EXE file, followed by two numbers. For example:
Myclient 1234 5678
Output Calling methods from mylibrary. dll: The sum of 1234 and 5678 is 6912 the product of 1234 and 5678 is 7006652