天天看点

c# 调用 .cs 生成的 dll 文件

第一步:

打开VS2010(我用的是这个版本的)

文件->新建->项目->类库->

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

namespace add_dll
{
    public class Class1
    {
        public static int add(int a, int b)
        {
            return a + b;
        }
    }
}
           

上面为简单范例

生成后debug 或者release 里面会生成dll文件

第二步:

新建C#控制台程序(当然也可以是其他)

在解决方案的地方引用 刚刚生成的dll文件;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using add_dll;
namespace test_dll_
{
    class Program
    {

        static void Main(string[] args)
        {
            int a = Class1.add(5,10);
        }
    }
}
           

即可调用dll里面的函数。

继续阅读