var Edit: TComponent;begin Edit := FindComponent("Edit1"); If Edit is TEdit then TEdit(Edit).Text := '你好 Delphi7';end; RTTI(RunTime Type Information): ...
var
Edit: TComponent;
begin
Edit := FindComponent("Edit1");
If Edit is TEdit then
TEdit(Edit).Text := '你好 Delphi7';
end;
RTTI(RunTime Type Information): 運行時類型信息, 就是在程式運行後也能得到類型(譬如 TButton 類)的信息.
這在早期主要用於 IDE 設計時, 譬如把一個 Button 放到窗體後, 此時我們的程式雖然沒有運行, 但在 Delphi 的 IDE 編輯環境中, 這個 Button 已經是在運行狀態(要不然IDE怎麼才能顯示我們要求的TButton呢); 此時我們對 Button 的屬性等信息的設置都是通過 RTTI 技術實現的.
但在 Delphi 2007 之前, 能夠獲取 RTTI 信息是有限的.
Delphi 2009 增加了 ObjAuto 單元、Delphi 2010 增加的 RTTI 單元, 這都可以讓程式在運行時對類型有更多掌控(副作用是最後生成的程式越來越大).
-------------------------------------例子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;
-----------------------------------
Delphi的RTTI(許多參考鏈接)
https://blog.51cto.com/u_15127692/4693281