问题
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?
回答1:
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; }
来源:https://stackoverflow.com/questions/61099378/how-do-i-export-an-interface-written-in-c-sharp-to-achieve-delphi-code-generated