2
votes

I'm currently trying to reimplement a com interface. This interface is currently used in a Delphi project. The Delphi interface code is presumably machine generated using "TLIBIMP.EXE -P") In this autogenerated code there is for example this interface:

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;

As you can see there are multiple property which are accessed like fields or arrays using square brackets.

What did I have achieved so far:

In C# it's possible to write one indexer accessor using this code

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

(from: How do I export an interface written in C# to achieve Delphi code generated by TLB)

The question:

But now I need to have more than one indexer in a class. And they only differ in their return type so I can't simply overload the "this" keyword.

So does anyone have an idea how I can implement this in C# so that I get an TLB file which than can be used to generate the Delphi code you can see in the top of this post?

Any ideas are highly appreciated.

Edit: I already stumbled across this post https://stackoverflow.com/a/4730299/3861861 It kind of work, So I'm able to export multiple properties with an index to Delphi. But the type of this properties are not the right one. For example: a double is not a double it is an IIndexerDouble (I needed to remove the generic from the indexer for the com export, so I had to write an indexer for every datatype I want to use)

1

1 Answers

1
votes

You don't need to "have more than one indexer in a class". What you really need is to have several indexed properties in Delphi.

Let's say you want to add the Dog indexed property. Start by adding these regular methods to the C# class:

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

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

They will generate this:

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

Now just add the declaration for the indexed property:

property Dog[Index: Integer]: IDDogs read GetDog write SetDog;