Passing an interface to Codesite.Send in Delphi

441 Views Asked by At

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

There are 2 best solutions below

1
Uwe Raabe On BEST ANSWER

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.

1
Uwe Raabe On

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;