GetImplementedInterfaces (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example iterates through the types that have RTTI in the current application and displays the names of classes that implement interfaces. GetImplementedInterfaces is used to get the set of implemented interfaces for a specific type.

Code

program GetImplementedInterfaces;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Rtti;

var
  LContext: TRttiContext;
  LType: TRttiType;
  LInstanceType: TRttiInstanceType;
  LInterfaces: TArray<TRttiInterfaceType>;

begin
  LContext := TRttiContext.Create;

  { Enumerate all types declared in the application }
  for LType in LContext.GetTypes do
  begin
    if LType.IsInstance then
    begin
      LInstanceType := LType.AsInstance;
      LInterfaces := LInstanceType.GetImplementedInterfaces;
      if Length(LInterfaces) > 0 then
        Writeln(LInstanceType.Name);
    end;
  end;

  LContext.Free;

end.

Uses

Personal tools
Assistance