RTTI在设计时可以从项目代码询问类型吗? [英] Can RTTI interrogate types from project code at designtime?

查看:162
本文介绍了RTTI在设计时可以从项目代码询问类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用RTTI在设计时而不是运行时检查项目源文件中包含的类型。

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

据我所知,这是不受支持的,但是此问题的评论中的讨论表明,这是可能的,并且已经适用于多个Delphi版本。这是我第一次听说这个功能是可用的,而且我一直无法为自己重现。

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.

这是我的测试示例。它使用一个简单的 TListBox descendant TMyListBox 其中有一个字符串属性 TypeToExplore 当设置时填写具有输入到其中的符合类型名称的属性的列表框。

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.

使用这个 TMyListBox 组件I


  • 将其编译并安装到Delphi XE IDE中

  • 将组件DCU位置添加到IDE库路径

  • 重新启动IDE,以确保

  • 创建一个新的空的 Project1

  • 删除 MyListBox1 TForm1

  • 保存,编译并运行 Project1

  • 关闭 Project1 应用程序(但不是项目)

  • 在对象检查器集 MyListBox1.TypeToExplore Unit1.TForm1

  • 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

MyListBox1 报告找不到类型,这与我的了解RTTI的工作原理,即在设计时它只能探索安装在IDE中的软件包中包含的类型,而不是项目源文件。

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.

如果IDE确实有能力检查类型声明红色在项目中我缺少什么?

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

推荐答案

我读过的RTTI.pas源码导致我得出结论,Delphi RTTI不能检查IDE的当前项目。在设计时,RTTI能够检查由IDE承载的软件包内的类型。它不能检查任何进一步的。

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.

这篇关于RTTI在设计时可以从项目代码询问类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆