通过字符串得到自定义类的某个属性的值?(详细代码见内容)(在线等待,下班结贴)(300分)

  • 主题发起人 主题发起人 jlutt-sadan
  • 开始时间 开始时间
J

jlutt-sadan

Unregistered / Unconfirmed
GUEST, unregistred user!
自定义了一个类,我现在想通过一个字符串去得到该类的实例的某属性的值,我是这样写的
TDictItem=class(TPersistent)
private
FVar1,FVar2,FVar3,FVar4: string;
FDictList: TDictList;
function GetVar1: string;
procedure SetVar1(const Value: string);
function GetVar2: string;
procedure SetVar2(const Value: string);
function GetVar3: string;
procedure SetVar3(const Value: string);
function GetVar4: string;
procedure SetVar4(const Value: string);
public
constructor Create(AList: TDictList);
destructor Destroy;override;
property Var1: string read GetVar1 write SetVar1;
property Var2: string read GetVar2 write SetVar2;
property Var3: string read GetVar3 write SetVar3;
property Var4: string read GetVar4 write SetVar4;
end;

..........

initialization
Classes.RegisterClass(TDictItem);

finalization
Classes.UnregisterClass(TDictItem);
end.

比如我现在想通过字符串Var1来得到一个TDictItem实例的Var1的值
代码如下:

function ReadProp(Obj: TObject; const PropName: string): string;
var
Prop: PPropInfo;
begin
result := '';
Prop := GetPropInfo(pTypeInfo(Obj.ClassInfo), PropName);
//我跟到这里Prop为空,故得不到相应的值
if not Assigned(Prop) then exit;
with Prop^ do
case PropType^.Kind of
tkWString,tkLString,tkString:
result := GetStrProp(Obj, Prop);
end
end;

procedure TFrmDictMain.GetColumns;
var
AItem: TDictItem;
sTmp: string;
i: Integer;
begin
if cbDicts.ItemIndex=-1 then exit;
AItem := listDictTypes.Items[cbDicts.ItemIndex];
for i:=1 to 4 do begin
ShowMessage(ReadProp(AItem,'Var'+IntToStr(i)));
end;
end.

我用ReadProp去读取ComboBox的Name属性能读出来。
是不是我哪地方有问题?RegisterClass?
 
Public的不行
http://www.delphibbs.com/keylife/iblog_show.asp?xid=100
//属性必须是published的,也就是可以在运行期在Object Inspector上设置的属性
 
必须将属性定义为 Published 才可以脱离 IDE 环境检测到属性
 
RegisterClass(TDictItem);
没有执行

 
或者
published
 
^_^,果然,我改成这样的了
published
property Var1: string read GetVar1 write SetVar1;
property Var2: string read GetVar2 write SetVar2;
property Var3: string read GetVar3 write SetVar3;
property Var4: string read GetVar4 write SetVar4;
end;
一直以为Rtti也能搞得定public的属性呢:(
谢谢
 
多人接受答案了。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo;

type

TDictItem = class(TPersistent)
private
FVar1, FVar2: string;
function GetVar1: string;
procedure SetVar1(const Value: string);
function GetVar2: string;
procedure SetVar2(const Value: string);
public
constructor Create;
destructor Destroy; override;
published
property Var1: string read GetVar1 write SetVar1;
property Var2: string read GetVar2 write SetVar2;
end;

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function ReadProp(Obj: TObject; const PropName: string): string;
var
Prop: PPropInfo;
begin
result := '';
Prop := GetPropInfo(pTypeInfo(Obj.ClassInfo), PropName);
//我跟到这里Prop为空,故得不到相应的值
if not Assigned(Prop) then exit;
with Prop^ do
case PropType^.Kind of
tkWString, tkLString, tkString:
result := GetStrProp(Obj, Prop);
end
end;

procedure TForm1.Button1Click(Sender: TObject);
var
d: TDictItem;
begin
showmessage(ReadProp(Sender, 'Name'));

d := TDictItem.Create;
d.Var1 := '我爱啃注脚';
showmessage(ReadProp(D, 'Var1'));

FreeAndNil(d);


end;

{ TDictItem }

constructor TDictItem.Create;
begin

end;

destructor TDictItem.Destroy;
begin

inherited;
end;

function TDictItem.GetVar1: string;
begin
result := FVar1;
end;

function TDictItem.GetVar2: string;
begin
result := FVar2;
end;

procedure TDictItem.SetVar1(const Value: string);
begin
FVar1 := Value
end;

procedure TDictItem.SetVar2(const Value: string);
begin
FVar2 := Value
end;
initialization
Classes.RegisterClass(TDictItem);

finalization
Classes.UnregisterClass(TDictItem);

end.

 
后退
顶部