【发布时间】:2020-08-05 18:44:30
【问题描述】:
我目前正在尝试重新实现一个 com 接口。 这个接口目前在一个Delphi项目中使用。 Delphi 接口代码大概是使用“TLIBIP.EXE -P”机器生成的) 在这个自动生成的代码中有例如这个接口:
IDPets = interface(IDispatch)
['{679DDC30-232F-11D3-B461-00A024BEC59F}']
function Get_Value(Index: Integer): Double; safecall;
procedure Set_Value(Index: Integer; Value: Double); safecall;
function Get_Pet(Index: Integer): IDPets; safecall;
procedure Set_Pet(Index: Integer; const Ptn: IDPets); safecall;
property Value[Index: Integer]: Double read Get_Value write Set_Value;
property Pet[Index: Integer]: IDPets read Get_Pet write Set_Pet;
end;
如您所见,有多个属性可以像使用方括号的字段或数组一样访问。
到目前为止我取得了什么成就:
在 C# 中,可以使用此代码编写一个索引器访问器
[System.Runtime.CompilerServices.IndexerName("Cat")]
public ICat this[int index] { get; set; }
(来自:How do I export an interface written in C# to achieve Delphi code generated by TLB)
问题:
但是现在我需要在一个类中拥有多个索引器。而且它们只是返回类型不同,所以我不能简单地重载“this”关键字。
那么有谁知道我如何在 C# 中实现它,以便我得到一个 TLB 文件,该文件可用于生成您可以在本文顶部看到的 Delphi 代码?
非常感谢任何想法。
编辑:我已经偶然发现了这篇文章https://stackoverflow.com/a/4730299/3861861 它有点工作,所以我能够将多个带有索引的属性导出到 Delphi。但是这个属性的类型不是正确的。例如:double 不是 double 它是 IIndexerDouble(我需要从索引器中删除泛型以进行 com 导出,因此我必须为我想要使用的每种数据类型编写一个索引器)
【问题讨论】: