GetImplementedInterfaces (Delphi)

From RAD Studio Code Examples
Revision as of 20:09, 3 November 2011 by L10nBot (talk | contribs) (normalized vcl links)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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