3

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.

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;

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

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; }
}

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;

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?

White
  • 157
  • 9

1 Answers1

4

Item is the default name of C# indexers.

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

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

[System.Runtime.CompilerServices.IndexerName("Cat")]
public ICat this[int index] { get; set; }
Olivier
  • 13,283
  • 1
  • 8
  • 24
  • Thanks for your quick reply. This is indeed the correct solution. But one new questions come in my mind. What is when I have a second property which also have an indexer? for example I also need an `[System.Runtime.CompilerServices.IndexerName("Dog")]` `public IDog this[int index] { get; set; }` This will not compile because there is already one "this" prroperty with an other name. – White Apr 22 '20 at 13:03
  • @White You can have several indexers in a class only if they have different signatures (a signature is defined by the number and the types of parameters). In your example, the signatures are the same. – Olivier Apr 22 '20 at 13:50
  • Yes I know. I will make a new thread, because thats a quite different problem now :-) – White Apr 22 '20 at 16:14