您的位置:首页 > 编程语言 > Delphi

Delphi的RTTI(许多参考链接)

2014-04-16 15:44 375 查看
RTTI(RunTime Type Information): 运行时类型信息, 就是在程序运行后也能得到类型(譬如 TButton 类)的信息.

这在早期主要用于 IDE 设计时, 譬如把一个 Button 放到窗体后, 此时我们的程序虽然没有运行, 但在 Delphi 的 IDE 编辑环境中, 这个 Button 已经是在运行状态(要不然IDE怎么才能显示我们要求的TButton呢); 此时我们对 Button 的属性等信息的设置都是通过 RTTI 技术实现的.

但在 Delphi 2007 之前, 能够获取 RTTI 信息是有限的.
Delphi 2009 增加了 ObjAuto 单元、Delphi 2010 增加的 RTTI 单元, 这都可以让程序在运行时对类型有更多掌控(副作用是最后生成的程序越来越大).
/article/6946207.html
/article/6946242.html

-------------------------------------例子1----------------------------------------

Uses Rtti, TypInfo;

procedure TForm1.Button3Click(Sender: TObject);
begin
ShowMessage(GetEnumName(TypeInfo(TFormStyle), Ord(FormStyle)));
end;

主窗体上放2个Label,3个Button,然后

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
PropInfo: PPropInfo;
begin
for I := 0 to ComponentCount-1 do
begin
PropInfo:=GetPropInfo(Components[i].ClassInfo, 'Color');
if PropInfo <> nil then
SetOrdProp(Components[i], PropInfo, clBlue);
end;
end;

参考: http://blog.csdn.net/lailai186/article/details/8823496 http://blog.csdn.net/shuaihj/article/details/6125317 http://blog.csdn.net/shuaihj/article/details/6125321 http://blog.csdn.net/shuaihj/article/details/6125691 http://blog.csdn.net/shuaihj/article/details/6125688 http://blog.csdn.net/shuaihj/article/details/6125713 http://blog.csdn.net/shuaihj/article/details/6125693
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: