Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can RTTI interrogate types from project code at designtime?

I would like to use RTTI to examine the types contained within project source files at designtime rather than runtime.

To the best of my knowledge this is unsupported but the discussion in the comments of this question indicate that it is possible and has been for several Delphi versions. This is the first time I have heard of this functionality being available though and as yet I have been unable to reproduce it for myself.

Here is my test example. It uses a simple TListBox descendant TMyListBox which has a string property TypeToExplore which when set fills the list box with the properties of the qualified type name entered into it.

unit MyListBox;

interface

uses
  SysUtils, Classes, Controls, StdCtrls;

type
  TMyListBox = class(TListBox)
  private
    FTypeToExplore : string;
    procedure SetTypeToExplore(const inValue: string);
    procedure FillWithTypeDetails;
  published
    property TypeToExplore : string read FTypeToExplore write SetTypeToExplore;
  end;

procedure Register;

implementation

uses
  RTTI, TypInfo;

procedure TMyListBox.SetTypeToExplore(const inValue: string);
begin
  if inValue = FTypeToExplore then
    Exit;

  FTypeToExplore := inValue;
  Clear;
  FillWithTypeDetails;
end;

procedure TMyListBox.FillWithTypeDetails;
var
  context : TRTTIContext;
  theType : TRttiType;
  properties : TArray<TRttiProperty>;
  prop : TRttiProperty;
begin
  theType := context.FindType(FTypeToExplore);
  if Assigned(theType) then begin
    properties := theType.GetProperties;
    for prop in properties do
      Items.Add(prop.Name);
  end else
    Items.Add('No type found');
end;

procedure Register;
begin
  RegisterComponents('Samples', [TMyListBox]);
end;

end.

Using this TMyListBox component I

  • Compile and install it into the Delphi XE IDE
  • Add the component DCU location to the IDE library path
  • Restart the IDE just to make sure
  • Create a new empty Project1
  • Drop MyListBox1 onto TForm1
  • Save, compile and run Project1
  • Close the Project1 application (but not the project)
  • In the object inspector set MyListBox1.TypeToExplore to Unit1.TForm1

And the MyListBox1 reports "No type found" which is consistent with my understanding of how RTTI works, that is at design-time it can only explore types that are contained within packages installed into the IDE, not project source files.

If IDE does indeed have the ability to examine types declared within projects what am I missing?

like image 552
LachlanG Avatar asked Jan 29 '12 22:01

LachlanG


2 Answers

My reading of the RTTI.pas source leads me to conclude that Delphi RTTI cannot inspect the IDE's current project. At design-time, RTTI is able to inspect types inside packages hosted by the IDE. It cannot inspect any further than that.

like image 136
David Heffernan Avatar answered Oct 26 '22 04:10

David Heffernan


Q: Can you inquire/utilize types in the Delphi IDE at design time?

A: Yes, of course :)

Q: Does the IDE directly utilize RTTI?

AFAIK,the IDE's "knowledge" of types, methods, etc. is separate and distinct from runtime RTTI. AFAIK, this is equally true of, for example, Java introspection/the Eclipse IDE/debugger, or .Net Reflection/the MSVS IDE/debugger.

This article might help:

  • Runtime Type Information in Delphi
like image 30
paulsm4 Avatar answered Oct 26 '22 04:10

paulsm4