-
c#调用动态链接库
动态链接库的调用是使用动态链接库的重要部分,下面主要通过一个示例来进行讲解。
创建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;
}
}
}
}