c# 封装动态链接库dll

转载自 https://blog.csdn.net/liuqinghui1990/article/details/76943922?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.compare&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.compare

前天学习了下将自己的方法封装进dll,同时在其他的项目里引用封装的dll,并调用dll里的方法。同时还试探了下将Windows应用程序封装进dll(Winform),下面详细介绍。

一、建立  类库  将方法封装进dll

在VS里新建一个类库项目,项目名称为MyTestDll,并添加两个类文件(TestDll.cs和TestDll2.cs),下面是方法代码。

TestDll文件代码

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
     
  6.  
    namespace MyTestDll
  7.  
    {
  8.  
    /// <summary>
  9.  
    /// TestDll类
  10.  
    /// </summary>
  11.  
    public class TestDll
  12.  
    {
  13.  
    /// <summary>
  14.  
    /// 加法函数
  15.  
    /// </summary>
  16.  
    /// <param name="number1">参数一</param>
  17.  
    /// <param name="number2">参数二</param>
  18.  
    /// <returns>两个参数之和</returns>
  19.  
    public int MathAdd(int number1, int number2)
  20.  
    {
  21.  
    return (number1 + number2);//返回两个参数的和
  22.  
    }
  23.  
     
  24.  
    /// <summary>
  25.  
    /// 减法函数
  26.  
    /// </summary>
  27.  
    /// <param name="number1">参数一</param>
  28.  
    /// <param name="number2">参数二</param>
  29.  
    /// <returns>两个参数的差</returns>
  30.  
    private int MathSubtract(int number1, int number2)
  31.  
    {
  32.  
    return (number1 - number2);
  33.  
    }
  34.  
    }
  35.  
    }

TestDll2文件代码

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
     
  6.  
    namespace MyTestDll
  7.  
    {
  8.  
    /// <summary>
  9.  
    /// TestDll2类
  10.  
    /// </summary>
  11.  
    public class TestDll2
  12.  
    {
  13.  
    /// <summary>
  14.  
    /// 乘法函数
  15.  
    /// </summary>
  16.  
    /// <param name="number1">参数一</param>
  17.  
    /// <param name="number2">参数二</param>
  18.  
    /// <returns>两个参数的乘积</returns>
  19.  
    public int MathMultiplication(int number1, int number2)
  20.  
    {
  21.  
    return (number1 * number2);
  22.  
    }
  23.  
     
  24.  
    /// <summary>
  25.  
    /// 除法函数
  26.  
    /// </summary>
  27.  
    /// <param name="number1">参数一</param>
  28.  
    /// <param name="number2">参数二</param>
  29.  
    /// <returns>两个参数的商</returns>
  30.  
    public double MathDivision(int number1, int number2)
  31.  
    {
  32.  
    return (number1 / number2);
  33.  
    }
  34.  
    }
  35.  
    }

生成解决方案后,在文件夹MyTestDll\bin\Debug目录中,就能找到封装好的dll文件  MyTestDll.dll。这时候就可以在别的项目中添加这个引用,那么就能够使用刚刚封装的方法了,详细说明如下:

建立一个控制台应用程序项目Test6,在   解决方案资源管理器--引用  中添加 MyTestDll.dll,下面是具体代码

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Text;
  5.  
    using MyTestDll; //添加引用
  6.  
     
  7.  
    namespace Test6
  8.  
    {
  9.  
    class Program
  10.  
    {
  11.  
    static void Main(string[] args)
  12.  
    {
  13.  
    TestDll td = new TestDll();
  14.  
    td.MathAdd( 3,5);
  15.  
    td.MathSubtract( 3, 5);//提示MyTestDll中不包含MathSubtract的定义
  16.  
    }
  17.  
    }
  18.  
    }

可以看出,新的项目中可以使用自己封装的代码MathAdd().

补充两点:1 MathSubtract()方法定义的时候是私有的,所以封装到dll里后,在其他项目引用这个dll后也无法访问到;2 如果要想看到封装后的dll方法的注释,则生成dll的时候要同时生成注释文档xml(具体可百度),然后引用的时候讲dll和对应的xml同时复制到项目的debug目录下。

二、将windows项目封装进dll(以Winform为例)

建立一个  窗体应用程序  项目  ,项目名Test8,然后随便拖几个控件上去,实现一些简单的功能,如下面的Form1实现加法和减法功能。

代码如下:

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.ComponentModel;
  4.  
    using System.Data;
  5.  
    using System.Drawing;
  6.  
    using System.Linq;
  7.  
    using System.Text;
  8.  
    using System.Windows.Forms;
  9.  
     
  10.  
    namespace Test8
  11.  
    {
  12.  
    public partial class Form1 : Form
  13.  
    {
  14.  
    public Form1()
  15.  
    {
  16.  
    InitializeComponent();
  17.  
    }
  18.  
     
  19.  
    //加法代码按钮
  20.  
    private void btn_add_Click(object sender, EventArgs e)
  21.  
    {
  22.  
    int num1 = Convert.ToInt16(tbx_num1.Text);
  23.  
    int num2 = Convert.ToInt16(tbx_num2.Text);
  24.  
    tbx_result.Text = add(num1, num2).ToString();
  25.  
    }
  26.  
     
  27.  
    public int add(int num1, int num2)
  28.  
    {
  29.  
    return (num1 + num2);
  30.  
    }
  31.  
     
  32.  
    public int sub(int num1, int num2)
  33.  
    {
  34.  
    return (num1 - num2);
  35.  
    }
  36.  
     
  37.  
    //减法按钮代码
  38.  
    private void btn_sub_Click(object sender, EventArgs e)
  39.  
    {
  40.  
    int num1 = Convert.ToInt16(tbx_num1.Text);
  41.  
    int num2 = Convert.ToInt16(tbx_num2.Text);
  42.  
    tbx_result.Text = sub(num1, num2).ToString();
  43.  
    }
  44.  
    }
  45.  
    }

下面将这个项目封装进dll,在 解决方案资源管理器 中右键点击项目名称,选择 属性 ,在弹出的窗口--应用程序-- 输出类型下拉框中选择  类库 ,点击保存后,生成解决方案,就会在debug目录中生成dll。

然后将这个封装的Test8.dll复制到新建的winform项目(Test9)中,添加引用,引入命名空间后,在Test9中,可以实例化Test8中的Form1,具体不在细说。

Test9的窗体:

具体代码:

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.ComponentModel;
  4.  
    using System.Data;
  5.  
    using System.Drawing;
  6.  
    using System.Linq;
  7.  
    using System.Text;
  8.  
    using System.Windows.Forms;
  9.  
    using Test8;
  10.  
     
  11.  
    namespace Test9
  12.  
    {
  13.  
    public partial class Form_a : Form
  14.  
    {
  15.  
    public Form_a()
  16.  
    {
  17.  
    InitializeComponent();
  18.  
    }
  19.  
     
  20.  
    private void button1_Click(object sender, EventArgs e)
  21.  
    {
  22.  
    if (textBox_s.Text.Trim() == "0")
  23.  
    {
  24.  
    Form1 form = new Form1();
  25.  
    this.Hide();
  26.  
    form.Show(); //显示在Test8项目中设计的那个Form1窗体
  27.  
    }
  28.  
    }
  29.  
    }
  30.  
    }


总结:不仅仅可以将方法封装到dll中供其他项目或别人使用,还可以将窗体封装进dll,当然这样是否有意思另说,存在即合理。

猜你喜欢

转载自www.cnblogs.com/sclu/p/13397055.html