如何导出用C#编写的接口以实现TLB生成的Delphi代码 [英] How do I export an interface written in C# to achieve Delphi code generated by TLB

查看:62
本文介绍了如何导出用C#编写的接口以实现TLB生成的Delphi代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发旧COM接口(用于与其他设备进行通信)的嵌入式"替换.该接口当前在大型应用程序中使用.库的作者现在不赞成使用旧的COM接口,他们现在仅支持和开发C#接口.我的任务是开发上述嵌入式"替换.它充当旧应用程序(用Delphi编写)和新的基于C#的接口之间的代理.我试图在主应用程序中尽可能少地更改代码.因此,我尝试尽可能地模仿旧界面.所以我用C#编写代码,然后将其导出到TLB文件中.TLB文件用于通过"TLIBIMP.EXE -P"命令生成Delphi副本.

I'm currently developing a "drop-in" replacement of an old COM interface (which is used to communicate with other devices). This interface is currently used in a big application. The old COM interface is now deprecated by the author of the library they now only support and develop a C# interface. My task is to develop the above mentioned "drop-in" replacement. Which acts as a proxy between the old application (written in Delphi) and the new C# based interface. Im trying to have as little as possible code changes in the main application. Therefore I try to mimic the the old interface as good as possible. So I'm writing code in C# which then get exported into an TLB file. The TLB file is used to generate the Delphi counterpart using the "TLIBIMP.EXE -P" command.

这是使用旧界面生成的代码.如您所见,有一个属性Cat,可以使用索引调用该属性,以在其后面获取适当的集合项.

This is the code which was generated using the old interface. As you can see there is a property Cat which can be called with an index to get the appropriate item of the collection behind it.

IDFoo = interface(IDispatch)
    ['{679F4D30-232F-11D3-B461-00A024BEC59F}']
    function Get_Cat(Index: Integer): IDFoo; safecall;
    procedure Set_Cat(Index: Integer; const Evn: IDFoo); safecall;
    property Cat[Index: Integer]: IDFoo read Get_Cat write Set_Cat;
end;

我正在尝试获取C#副本,该副本会生成其中带有Cat [index]属性的TLB文件.

I'm trying to get a C# counterpart which produces a TLB file with the Cat[index] property in it.

到目前为止,我的解决方案是:C#:

So my solution so far is this: C#:

[ComVisible(true)]
[Guid("821A3A07-598B-450D-A22B-AA4839999A18")]
public interface ICat
{
    ICat this[int index] { get; set; }
}

这将产生一个TLB,然后生成以下Delphi代码:

And this produces a TLB which then resulting in this Delphi code:

  ICat = interface(IDispatch)
    ['{821A3A07-598B-450D-A22B-AA4839999A18}']
    function Get_Item(index: Integer): ICat; safecall;
    procedure _Set_Item(index: Integer; const pRetVal: ICat); safecall;
    property Item[index: Integer]: ICat read Get_Item write _Set_Item; default;
  end;

到目前为止,一切都很好.但是该属性被命名为"Item",而不是原始的"Cat".有没有人暗示我该如何实现?

So far so good. But the property is named "Item" and not like the original "Cat". Does anyone have a hint how I can achieve this?

推荐答案

Item 是C#索引器的默认名称.

Item is the default name of C# indexers.

第一种可能性是在生成的Delphi代码中将 Item 重命名为 Cat .

The first possibility is to just rename Item to Cat in the generated Delphi code.

第二种可能性是指定C#索引器名称:

The second possibility is to specify the C# indexer name:

[System.Runtime.CompilerServices.IndexerName("Cat")]
public ICat this[int index] { get; set; }

这篇关于如何导出用C#编写的接口以实现TLB生成的Delphi代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆