首页 > Windows开发 > 详细

C#生成COM组件

时间:2019-03-05 19:08:50      阅读:222      评论:0      收藏:0      [点我收藏+]

1.类库代码

1.1暴露的方法必须以接口的方式实现

1.2类需要GUID编号

 1 using System;
 2 using System.Runtime.InteropServices;
 3 
 4 //COM组件
 5 namespace PayCls
 6 {
 7     [Guid("051A5FA3-E34E-4DFF-AF77-BA6D6277A132")]
 8     public interface IPay
 9     {
10         string Init(string strJson);
11     }
12 
13 
14     [ClassInterface(ClassInterfaceType.None)]
15     [Guid("B88176F4-33E9-4C9B-807F-5AB3E25A0CF6")]
16     [ProgId("Pay.Application")]
17     public class Pay: IPay
18     {
19         public string Init(string strJson)
20         {
21             string result = "";
22             Decive dev = new Decive();23             try
24             {
25                 result = dev.Run(strJson);
26             }
27             catch (Exception ex)
28             {
29                 result = ex.Message;
30             }
31             return result;
32         }
33     }
34 }

 

2.在类库项目右键---属性---程序集信息 "使程序集可见" 打勾

3.生成---"为COM互操作注册"打勾

4.签名---”为程序集签名"打勾,选择强名称密钥文件 创建。可不输密码

5.生成dll后需要用gacutil与RegAsm加载并注册程序集,注意该dll所引用的其他dll都必须要加载并注册

E:\Tools\gacutil.exe /i Pay.dll
E:\Tools\gacutil.exe /i Newtonsoft.Json.dll
E:\Tools\gacutil.exe /i RestSharp.dll
E:\Tools\gacutil.exe /i Log4net.dll

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe Pay.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe Newtonsoft.Json.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe RestSharp.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe Log4net.dll

6.log4net读取配置文件:

1                 string configFile = $@"{Environment.CurrentDirectory}\log4net.config";
2                 log4net.Config.XmlConfigurator.Configure(new FileInfo(configFile));

 

C#生成COM组件

原文:https://www.cnblogs.com/xiaoguanqiu/p/10478753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!