---
title: "C#生成dll与调用"
author: "ruohanfly"
date: "2016-01-29T14:49:21Z"
tags: []
categories: ["编程语言"]
slug: "c-dll"
draft: false
lastmod: "2016-01-29T14:49:21Z"
id: 2274945
url: "https://ruohanfly.iteye.com/blog/2274945"
---

生成COM组件

1.打开VS，\[文件]-&gt;\[新建]-&gt;\[项目]-&gt;\[类库]

2.编辑新生成class代码，EG

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTT
{
    public interface MyInterface
    {
        int add(int a, int b);
    }

    public class MyClass : MyInterface
    {
        public int add(int a, int b)
        {
            return a + b;
        }
    }
}
```

 3.编辑AssemblyInfo.cs文件

将assembly:ComVisible(false)改为true

4.在项目上右键——属性

（1.应用程序——程序集信息——选中“使程序集COM可见”

（2.生成——选中“为COM互操作注册”

（3.签名——选中“为程序集签名”，去掉“使用密码保护密钥文件”

5.生成

 

 

 

调用COM组件

1.引用刚生成的dll，直接引路径也行

2.引用dll的命名空间，EG：using TTT

3.可以调用ttt里边的类了

完成