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:Toolsgacutil.exe /i Pay.dll
E:Toolsgacutil.exe /i Newtonsoft.Json.dll
E:Toolsgacutil.exe /i RestSharp.dll
E:Toolsgacutil.exe /i Log4net.dll
C:WindowsMicrosoft.NETFramework64v4.0.30319RegAsm.exe Pay.dll
C:WindowsMicrosoft.NETFramework64v4.0.30319RegAsm.exe Newtonsoft.Json.dll
C:WindowsMicrosoft.NETFramework64v4.0.30319RegAsm.exe RestSharp.dll
C:WindowsMicrosoft.NETFramework64v4.0.30319RegAsm.exe Log4net.dll
6.log4net读取配置文件:
1 string configFile = $@"{Environment.CurrentDirectory}log4net.config"; 2 log4net.Config.XmlConfigurator.Configure(new FileInfo(configFile));