您的位置:首页 > 编程语言 > C#

C#创建并使用DLL文件

2021-05-27 15:45 1436 查看

一.创建DLL文件

1.新建C#类库项目


2.编写代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyLibrary
{
public class Class1
{
public string getMsg() {
return "Message";
}
}
}

3.生成DLL文件

右键MyLibrary项目,点击生成,生成dll文件。或者选中MyLibrary按F6键生成dll文件。生成的dll文件路径为:…\MyLibrary\bin\Debug\MyLibrary.dll

二.使用DLL文件

1.新建C#窗体应用程序


2.添加DLL引用

右键MyApplication-添加-新建文件夹。新建lib文件夹,存放刚才已经生成的dll文件。


将生成好的dll文件复制到lib文件夹下。

右键MyApplication-添加引用-浏览-lib-MyLibrary.dll,然后点击确定。


接着就可以看见MyLibrary引用已经添加进来了。

3.编写代码使用库文件

右键Form1-查看代码

添加代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyLibrary;

namespace MyApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Class1 class1 = new Class1();
Console.WriteLine("============="+class1.getMsg()+"=================");
}
}
}

运行项目

三.运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: