rtti读取和设置属性
编辑器通过 Rtti 还能够调用一个类的方法, 也能读取或设置其属性值.
--------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
{自定义的类}
TMyClass = class(TComponent)
public
procedure msg(const str: string);
function Add(const a,b: Integer): Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Rtti;
{ MyClass 类的实现 -----------------------------------------------------------}
procedure TMyClass.msg(const str: string);
begin
MessageDlg(str, mtInformation, [mbYes], 0);
end;
function TMyClass.Add(const a, b: Integer): Integer;
begin
Result := a + b;
end;
//通过 Rtti 的手段使用 TMyClass 类的方法 --------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
obj: TMyClass;
t: TRttiType;
m1,m2: TRttiMethod;
r: TValue; //TRttiMethod.Invoke 的返回类型
begin
t := TRttiContext.Create.GetType(TMyClass);
{获取 TMyClass 类的两个方法}
m1 := t.GetMethod('msg'); {procedure}
m2 := t.GetMethod('Add'); {function}
obj := TMyClass.Create(Self); {调用需要依赖一个已存在的对象}
{调用 msg 过程}
m1.Invoke(obj, ['Delphi 2010']); {将弹出信息框}
{调用 Add 函数}
r := m2.Invoke(obj, [1, 2]); {其返回值是个 TValue 类型的结构}
ShowMessage(IntToStr(r.AsInteger)); {3}
obj.Free;
end;
//通过 Rtti 的手段修改并获取 TMyClass 类的属性 --------------------------------
procedure TForm1.Button2Click(Sender: TObject);
var
obj: TMyClass;
t: TRttiType;
p: TRttiProperty;
r: TValue;
begin
obj := TMyClass.Create(Self);
t := TRttiContext.Create.GetType(TMyClass);
p := t.GetProperty('Name');
p.SetValue(obj, 'NewName');
r := p.GetValue(obj);
ShowMessage(r.AsString); {NewName}
obj.Free;
end;
end.
--------------------------------------------------------------------------------
回复下面的问题:如何设置枚举值:
--------------------------------------------------------------------------------
procedure TForm1.Button3Click(Sender: TObject);
var
t: TRttiType;
p: TRttiProperty;
r: TValue;
begin
t := TRttiContext.Create.GetType(TButton);
p := t.GetProperty('Align');
p.SetValue(Button1, TValue.FromOrdinal(TypeInfo(TAlign), Ord(alLeft)));
r := p.GetValue(Button1);
ShowMessage(IntToStr(r.AsOrdinal)); {3}
end;
加载中...