Rtti.TVirtualInterface (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the capabilities of the TVirtualInterface class. An implementation of a specific interface (ISpecificInterface) is created at run time with the help of TVirtualInterface. An instance of ISpecificInterface ("virtual interface") is obtained from a TVirtualInterface instance by casting (another way is to use QueryInterface). The TVirtualInterface instance is not destroyed explicitly; it is destroyed automatically when the application ends.

An event is defined so that every interface method call will display its name on the console.

Code

{$APPTYPE CONSOLE}

uses
  SysUtils, Rtti;

type
  ISpecificInterface = interface(IInvokable)
    ['{281D8B97-397E-430A-895A-9CA4E1F5FB5F}']
    procedure SpecificProcedure;
  end;

procedure AProcedure(Method: TRttiMethod; const Args: TArray<TValue>;
  out Result: TValue);
begin
  Writeln(Method.ToString);
end;

var
  ISpecificInterfaceInstance: ISpecificInterface;

begin
  ISpecificInterfaceInstance := TVirtualInterface.Create
    (TypeInfo(ISpecificInterface), AProcedure) as ISpecificInterface;

  ISpecificInterfaceInstance.SpecificProcedure;

end. // TVirtualInterface ref. counter is decremented

Console Output

procedure SpecificProcedure

Uses

Personal tools
Assistance