C#下开发及调用dll文件

Posted on 2012-06-29 19:47  aniu123  阅读(171)  评论(0)    收藏  举报
在.net中,可调用的dll(动态链接库)文件其实就是一个类库。

   我们可以通过写一个类,然后把它编译成dll文件形式,在其他的项目中就可以直接调用此编译好的dll文件,而不用重复 写这个类的代码。

 

   下面详细介绍此过程:

   一、开发dll文件

   (1)打开vs2005,新建项目中模板选择“类库”

C下开发及调用dll文件  - liubaolongg - 我的博客

   (2)在解决资源管理器里面添加一文件夹“First”,在其中添加两个类MathAdd和MathMinus,在其父目录里直接添加一个名为yun(写这个类的时候,我已经晕了,不知道取什么名字好,于是就yun了)的类。

C下开发及调用dll文件  - liubaolongg - 我的博客

下面是各个类的代码,注意类前一定要加上public,不然这个类会被当做是私有的,不能够被引用,当初我就是犯了这个错误,弄个了好久才想起,真是追悔莫及啊。类中的方法为静态或非静态都可以。

C下开发及调用dll文件  - liubaolongg - 我的博客C下开发及调用dll文件  - liubaolongg - 我的博客MathAdd类代码

using System;

using System.Collections.Generic;

using System.Text;

namespace noo.First

{

     public class MathAdd

     {

         public int add( int a,int b)

         {

             return a + b;

         }

         public static   int Muti(int a, int b)

         {

             int mutiSum;

             mutiSum = a * b;

             return mutiSum;

         }

     }

}

 

C下开发及调用dll文件  - liubaolongg - 我的博客C下开发及调用dll文件  - liubaolongg - 我的博客MathMinus类代码

using System;

using System.Collections.Generic;

using System.Text;

namespace noo.First

{

     public   class   MathMinus

     {

         public static int minus(int a, int b)

         {

             return a - b;

         }

     }

}

 

C下开发及调用dll文件  - liubaolongg - 我的博客C下开发及调用dll文件  - liubaolongg - 我的博客yun类代码

using System;

using System.Collections.Generic;

using System.Text;

namespace noo

{

     public   class   yun

     {

         public int add(int a, int b,int c)

         {

             return a + b-c;

         }

     }

到此,dll代码其实已经写好,这里我们可以发现,其实dll文件就是一个一个封装好的类库而已。

   (3)现在可以把代码编译为dll文件了。这里有两种方法

   方法一:在解决方案资源管理器里面,直接右击项目名称,选择“生成”即可

C下开发及调用dll文件  - liubaolongg - 我的博客

   方法二:比较复杂点,由于.net自带了csc.exe编译器,可以用这个工具对刚编写的类文件编译为dll文件,以下为其详细步骤。

   ①,通常安装.net的时候,CSC.EXE一般位于C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727文件夹下,为了能在命令窗口下的任何目录都能调用csc.exe文件,右击我的电脑——属性——高级——环境变量,双击PATH,在“变量值”的已有字符串的后面加上“;C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”(引号不要,但是分号一定要加上)。

   ②,点击开始——运行,输入cmd,打开命令窗口,输入csc -help,如果出现一大片字符,说明你的csc路径配置成功了。

C下开发及调用dll文件  - liubaolongg - 我的博客

   ③,由于我刚编写的类文件(MathAdd.cs和MathMinus.cs)存放在E:\shy\noo\noo\First目录下,点击开始——运行,输入cmd,打开命令窗口,输入E:目录指向E盘,接着输入cd E:\shy\noo\noo\First(这里的目录就是存放类的目录,一定要切记)改变目录指向,然后输入csc /target:library /out:myFirstDll.DLL MathAdd.cs MathMinus.cs编译不同的类时,只有myFirstDll.dll(输出的dll文件名,可以随意取),MathAdd.cs MathMinus.cs(这两个都是类文件名,必须要同自己编写的类名相同)这两个地方需要作修改。

C下开发及调用dll文件  - liubaolongg - 我的博客

   到此,dll文件编译完成。

   可以发现方法一在E:\shy\noo\noo\bin\Debug文件夹下生成了一名为noo.dll(同项目名称相同)的文件,它包含整个项目的完整的三个类(MathAdd、MathMinus和yun),方法二在E:\shy\noo\noo\First文件夹下生成一名为myFirstDll.dll(自定义的文件名)的文件,它只包含此文件夹下的两个类(MathAdd和MathMinus)。

   二、调用dll文件

   (1)新建一windows应用程序

C下开发及调用dll文件  - liubaolongg - 我的博客

   (2),在解决方案资源管理器中,右击“引用”——“添加引用”,

C下开发及调用dll文件  - liubaolongg - 我的博客

选择编译好的noo.dll文件

C下开发及调用dll文件  - liubaolongg - 我的博客

   (3),添加了dll文件引用后,会发现引用下会多了一个noo

C下开发及调用dll文件  - liubaolongg - 我的博客

 

双击这个noo,打开对象浏览器,打开noo这棵树,会发现其中包含命名空间、类及方法

C下开发及调用dll文件  - liubaolongg - 我的博客

说明添加dll文件成功

   (4),在Form1.cs中首先要添加命名空间,using noo和using noo.First(注意此处有智能提示,如没有可能添加dll出错),在button1的click事件中能够调用MathAdd类的Add方法,故调用dll成功。

C下开发及调用dll文件  - liubaolongg - 我的博客

到此,dll的创建和调用已经完成。

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3