動態鏈接庫的調用是使用動態鏈接庫的重要部分,下面主要通過一個示例來進行講解。
創建DllDemo動態鏈接庫代碼如下。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Text;
- namespaceDllDemo
- {
- publicclassOperation
- {
- publicdoubleAdd(doublea,doubleb)
- {
- returna+b;
- }
- publicdoubleMinus(doublea,doubleb)
- {
- returna-b;
- }
- publicdoubleMultiplication(doublea,doubleb)
- {
- returna*b;
- }
- publicdoubleDivision(doublea,doubleb)
- {
- returna/b;
- }
- }
- }
示例 通過調用類庫實現簡單計算器程序
(1)在菜單欄中選擇“項目”/“添加引用”,彈出“添加引用”對話框,在“添加引用”對話框中選擇“瀏覽”選項卡。
(2)通過瀏覽找到上一節中創建的DLL類庫,成功找到後,單擊【確定】按鈕,將類庫添加到項目中。
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.ComponentModel;
- usingSystem.Data;
- usingSystem.Drawing;
- usingSystem.Text;
- usingSystem.Windows.Forms;
- usingDllDemo;
- namespaceCCDll
- {
- publicpartialclassForm1:Form
- {
- publicForm1()
- {
- InitializeComponent();
- }
- Operationoper=newOperation();
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- switch(comboBox1.Text)
- {
- case"+":
- textBox3.Text=oper.Add(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
- break;
- case"-":
- textBox3.Text=oper.Minus(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
- break;
- case"*":
- textBox3.Text=oper.Multiplication(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
- break;
- case"/":
- textBox3.Text=oper.Division(Convert.ToDouble(textBox1.Text),Convert.ToDouble(textBox2.Text)).ToString();
- break;
- }
- }
- }
- }