我们使用Visual Studio 2010创建c#dll文件。
因此,首先从文件菜单中选择项目选项,然后
选择一个类库,默认情况下,单击确定后,将其命名为您想要的类库,并在其中创建了classlibrary1。
在此类class1内,应放置要在其他项目文件中使用的方法。 创建类后,您应该构建它。要构建它,必须先保存文件,然后从构建菜单中选择
build> build classlibrary1.
成功构建后,请关闭此文件,然后打开一个您要使用此dll的新项目。 从项目菜单中选择添加引用,添加引用窗口将显示,然后选择浏览按钮并浏览存储它的dll文件,例如->
projects->classlibrary1->bin->debug
或bin->release
并找到dll文件。
现在,您可以在项目中使用它了。
您必须调用名称空间,才能使用dll中的方法。(使用namespace_name;)
在这里我们使用createdll dll文件
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using createdll;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
long a, b;
a = long.Parse(textBox1.Text);
b = long.Parse(textBox2.Text);
textBox3.Text = math.add(a, b).ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
这是dll文件中的类的简单示例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public static long add(long a, long b)
{
return (a + b);
}
}
}
注意:此类成功构建后,将创建一个dll文件(此处为ut ClassLibrary1.dll
From: https://bytes.com/topic/c-sharp/insights/923526-how-create-c-dll-file-using-visual-studio-2010-a