0

I am unable to get CodeSite.Send to work with a variable declared as an interface. The compile time error is E2250 There is no overloaded version of 'Send' that can be called with these arguments.

How can I use interfaces with CodeSite?

Code example that demonstrates the problem:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

  IMyInterface = Interface(IInterface)['{9BCD2224-71F8-4CE7-B04C-30703809FAAD}']
    function GetMyValue : String;
    property MyVaue : String read GetMyValue;
  End;

  MyType = class(TInterfacedObject, IMyInterface)
    private
      sValue : string;
      function GetMyValue : String;
    published
      property MyVaue : String read GetMyValue;
  end;

var
  Form1: TForm1;
  test : IMyInterface;
  testType : MyType;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  test := MyType.Create;
  testType := MyType.Create;
  CodeSite.Send( 'testType', testType );  // This compiles
  CodeSite.Send( 'test', test );  // Compiler error here
  FreeAndNil(test);
end;

function MyType.GetMyValue : String;
begin
  Result := Self.sValue;
end;
Timothy Vogel
  • 1,363
  • 2
  • 21
  • 39
  • 2
    What are you expecting it to log if it did accept an interface? `MyValue`? How would CodeSite know to access that particular property? RTTI? Near as I can tell, there is no overload of `Send()` for interfaces, which is why you are getting the error. – Remy Lebeau Mar 15 '14 at 08:10

2 Answers2

2

As Remy pointed out in his comment, CodeSite cannot know what to log when you specify an interface. Even if there were an overloaded Send accepting IInterface, it would not be of any help as it were still unaware of your specific interface IMyInterface.

The only workaround known to me is to implement ICodeSiteCustomData in your implementing class and use something like

CodeSite.Send( 'test', test as ICodeSiteCustomData);

Note that this feature is not available in CodeSite Express.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • Remy and Uwe - Thanks for the replies! I hoped it would use RTTI on interfaces as it does for classes. Perhaps there is no RTTI for interfaces? This limits the usefulness of CodeSite since I use interfaces extensively. Your workaround would be too much code maintenance. – Timothy Vogel Mar 15 '14 at 08:35
1

I found another way that might do the trick, but even this has its drawbacks.

Introduce a class helper for TCodeSiteLogger implementing an overload of Send for Interfaces. Inside you cast the interface back to the implementing instance and use that for the Send. Note that this will give you all the published properties of the underlyng object and not only those of the interface.

type
  TCodeSiteLoggerHelper = class helper for TCodeSiteLogger
    procedure Send(const Msg: string; const Value: IInterface); overload;
  end;

procedure TCodeSiteLoggerHelper.Send(const Msg: string;
  const Value: IInterface);
begin
  Send(Msg, Value as TObject);
end;
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • Using this approach, you could create `Send()` overloads for specific interface types, and not have to cast them back to the implementing class (which can only be done in D2010+, BTW). – Remy Lebeau Mar 16 '14 at 04:54