加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

COM interop,C#,Visual Studio 2010 – >嵌入互操作类型

发布时间:2020-12-15 21:30:15 所属栏目:百科 来源:网络整理
导读:我的C#程序通过Nco3(sapnco.dll)访问SAP.该程序还需要与Delphi一起使用.我的一些方法从sapnco.dll返回类型: public void IRfcTable table(...) { ... } 在Delphi中,这种方法显示为 function table(...): IUnknown { ... } 我想这个IUnknown是因为我的TLB不
我的C#程序通过Nco3(sapnco.dll)访问SAP.该程序还需要与Delphi一起使用.我的一些方法从sapnco.dll返回类型:

public void IRfcTable table(...) { ... }

在Delphi中,这种方法显示为

function table(...): IUnknown { ... }

我想这个IUnknown是因为我的TLB不包含sapnco.dll.我在Visual Studio中尝试“嵌入Interop Types = true”,但是出现此错误:

Error Interoptypen aus Assembly “C:…” k?nnen nicht eingebettet werden,weil das ImportedFromTypeLibAttribute-Attribut oder das PrimaryInteropAssemblyAttribute-Attribut fehlt. c:…sapnco.dll

(Interop Types could not be embedded because some attributes are missing).

这是正确的方法吗?如果是这样,在哪里放置这些属性?

解决方法

sapnco.dll是一个.NET DLL,因此它不会暴露给COM,因此您无法在COM环境中直接使用此类型.您的问题的解决方案是创建一个库来将sapnco.dll包装在COM公开的类中:

举个例子:

[ComVisible(true)]
public interface IComRfcTable
{
    public void DoSomething();
}

[ComVisible(true)]
public class ComRfcTable
{
    private _rfcTable; // object to wrap
    public ComRfcTable(IRfcTable rfcTable)
    {
        _rfcTable = rfcTable
    }

    public void DoSomething()
    {
        _rfcTable.DoSomething();
    }
}

那么你的方法必须实现如下:

public IComRfcTable table(...) { ... }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读