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?
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?