通过以下方式编写DLL
1、C#新建类库项目MyDLL
① 需要导出的属性、方法必须使用接口的形式
② 需要导出的类、属性、方法必须声明为publicusing System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MyDLL
{
public interface IMyDLL{
string sayHello(string name);
}
[ClassInterface(ClassInterfaceType.None)]
public class MyDLL:IMyDLL
{
public string sayHello(string _name){
return "Hello "+_name;
}
}
}
2、设置项目属性
① 选中项目->属性->应用程序->程序集信息,勾选【使程序集COM可见】
② 属性->生成,勾选【为com互操作注册】
3、重新生成项目,得到DLL文件