C#实现COM接口IInsModule时Mod_Value属性报CS0535错误如何解决
问题原因
你遇到的报错是因为COM定义中的Mod_Value是带参数的属性(属性第一个参数key作为属性的索引参数使用),而你当前写的是无参数的C#普通属性,和接口要求的签名不匹配。
正确实现方案
有两种可直接使用的实现方式,按需选择即可:
方案1:使用带IndexerName特性的索引器实现
如果COM程序集导入时该属性被识别为索引器,选择该方案:
using System.Runtime.CompilerServices; public class CSharpDriver : IInsModule { public void Mod_Initialize(CoDriver driver, IInsCommBase comm, IInsBaseModule basemod, IInsBaseMessage basemsg, IInsLogWriter logger) { throw new NotImplementedException(); } public void Mod_Activate() { throw new NotImplementedException(); } public void Mod_Finish() { throw new NotImplementedException(); } [IndexerName("Mod_Value")] public string this[int key] // 若导入接口中key类型为long则将int改为long { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } }
方案2:显式实现接口的get/set方法
如果方案1编译仍不通过,说明导入的接口未将该属性识别为默认索引器,直接实现COM互操作生成的底层get、set方法即可:
public class CSharpDriver : IInsModule { public void Mod_Initialize(CoDriver driver, IInsCommBase comm, IInsBaseModule basemod, IInsBaseMessage basemsg, IInsLogWriter logger) { throw new NotImplementedException(); } public void Mod_Activate() { throw new NotImplementedException(); } public void Mod_Finish() { throw new NotImplementedException(); } // 对应COM定义中的propget方法 string IInsModule.get_Mod_Value(int key) // 若导入接口中key类型为long则将int改为long { throw new NotImplementedException(); } // 对应COM定义中的propput方法 void IInsModule.set_Mod_Value(int key, string value) // 若导入接口中key类型为long则将int改为long { throw new NotImplementedException(); } }
内容的提问来源于stack exchange,提问作者Xenofono
相关产品推荐
相关产品推荐

