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
ontoTForm1
- Save, compile and run
Project1
- Close the
Project1
application (but not the project) - In the object inspector set
MyListBox1.TypeToExplore
toUnit1.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?