从C#中导出多个索引属性到tlb-> delphi

问题描述 投票:2回答:1

我目前正试图重新实现一个com接口.这个接口目前在一个Delphi项目中使用。Delphi接口代码大概是用 "TLIBIMP.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; }

(来自 如何导出用C#编写的接口,实现TLB生成的Delphi代码?)

这个问题。

但现在我需要在一个类中有多个索引器。它们只是在返回类型上有所不同,所以我不能简单地重载 "this "关键字。

那么,有谁知道如何在C#中实现这个功能,从而得到一个TLB文件,这个文件可以用来生成你在这个帖子顶部看到的Delphi代码?

任何想法都非常感激。

编辑:我已经偶然发现了这个帖子。https:/stackoverflow.coma47302993861861。它有点工作,所以我能够导出多个带索引的属性到Delphi。但是这些属性的类型并不正确,例如:一个double并不是double,而是一个IIndexerDouble(我需要从索引中删除generic)。例如:一个double不是double,而是一个IIndexerDouble(我需要从com导出的索引器中删除generic,所以我必须为每个我想使用的数据类型写一个索引器)。

c# delphi com tlb
1个回答
1
投票

你不需要 "在一个类中拥有一个以上的索引器"。你真正需要的是在Delphi中拥有几个索引属性。

比方说,你想在类中添加 Dog 索引属性。首先将这些常规方法添加到C#类中。

public Dog GetDog(int index)
{
    ...
}

public void SetDog(int index, Dog value)
{
    ...
}

他们会生成这个:

function GetDog(Index: Integer): IDDogs; safecall;
procedure SetDog(Index: Integer; const Ptn: IDDogs); safecall; 

现在只需添加indexed属性的声明。

property Dog[Index: Integer]: IDDogs read GetDog write SetDog;
© www.soinside.com 2019 - 2024. All rights reserved.