10

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?

7
  • 1
    +1 Good question. I'm very intrigued to find out what the answer is!! Jan 29, 2012 at 22:23
  • Do you want the functionality of populating a TListBox with all properties of a qualified type name available both in designtime and in runtime?
    – menjaraz
    Jan 30, 2012 at 4:13
  • @menjaraz: Yes both designtime and runtime. This example works fine at runtime with the qualified type name Unit1.TForm1 but not at designtime.
    – LachlanG
    Jan 30, 2012 at 7:43
  • Quiet here isn't it..... Jan 30, 2012 at 19:27
  • 1
    @LaKraven You don't actually need to call either of those Jan 30, 2012 at 20:12

2 Answers 2

1

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.

4
  • Having now looked again at my Wrapper Unit Generator's original source (originally written for D2010 back in late 2009), I can confirm what David is saying here! My Wrapper Unit Generator is in fact creating a package with the same properties as the selected Project, registering that with the IDE... THEN it is using the RTTI! Now I feel both confused and a little stupid!
    – LaKraven
    Jan 30, 2012 at 20:45
  • @LaKraven Well, I'm glad we cleared all that up. I reverted your edits to my answer at the other question. Your acceptance of the other answer there is perhaps a little confusing. Jan 30, 2012 at 20:46
  • I'm quite surprised at just how much of my method I have forgotten since writing the source just two years ago!
    – LaKraven
    Jan 30, 2012 at 20:48
  • Oh well, that's a shame. I was looking forward to making use of this secret RTTI capability. Thanks to you both, it's been an interesting discussion and experiment anyway.
    – LachlanG
    Jan 30, 2012 at 21:12
1

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:

3
  • 1
    @paulsm4 The article you link to pre-dates enhanced RTTI (D2010). It doesn't seem relevant to this question which concerns enhanced RTTI. Jan 29, 2012 at 22:35
  • 1
    If you're saying "if it's old, it must be wrong" - that's nonsense. If you're saying "the rules might have changed when Enhanced RTTI was introduced", that's more plausible. But I don't think so. I said above - and I still believe - even for the latest/greatest XE2 - that "RTTI" is a runtime thing, more or less independent of the IDE (and the debugger). Here is more evidence supporting that belief: delphi.about.com/b/2011/07/26/… PS: the article remains a very, very good one. IMHO...
    – paulsm4
    Jan 30, 2012 at 6:18
  • LaKraven asserted that enhanced RTTI was able to introspect active project at designtime. So I think this question is about enhanced RTTI. Jan 30, 2012 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.