1、新建一个FrequentlyUsedCalculator.cs文件
代码如下:
using System; namespace MyClassLib { public class FrequentlyUsedCalculator { public int Add(int a, int b) { return a + b; } public int Sub(int a, int b) { return a - b; } } }
2、对FrequentlyUsedCalculator.cs文件进行编译
csc.exe /t:module FrequentlyUsedCalculator.cs
3、新建一个RarelyUsedCalculator.cs文件
代码如下:
using System; namespace MyClassLib { public class RarelyUsedCalculator { public int Multiple(int a,int b) { return a * b; } public int Divide(int a, int b) { return a / b; } } }
4、对RarelyUsedCalculator.cs文件进行编译
csc.exe /t:module RarelyUsedCalculator.cs
5、将module文件合并成assembly文件
al.exe /out:MyClassLib.dll /t:library FrequentlyUsedCalculator.netmodule RarelyUsedCalculator.netmodule
6、新建一个Program.cs文件
using System; using MyClassLib; namespace MyConsoleApp { class Program { static void Main(string[] args) { Console.Write("请输入第一个整数:"); int number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("请输入第二个整数:"); int number2 = Convert.ToInt32(Console.ReadLine()); FrequentlyUsedCalculator cal1 = new FrequentlyUsedCalculator(); Console.WriteLine("{0} + {1} = {2}", number1, number2, cal1.Add(number1, number2)); Console.WriteLine("{0} - {1} = {2}", number1, number2, cal1.Sub(number1, number2)); RarelyUsedCalculator cal2 = new RarelyUsedCalculator(); Console.WriteLine("{0} * {1} = {2}", number1, number2, cal2.Multiple(number1, number2)); Console.WriteLine("{0} / {1} = {2}", number1, number2, cal2.Divide(number1, number2)); Console.ReadKey(); } } }
7、对Program.cs进行编译
csc.exe /out:Program.exe /t:exe /r:MyClassLib.dll Program.cs
8、运行结果
时间: 2024-12-25 15:40:12