gpt4 book ai didi

delphi - 如何通过 RTTI 设置/获取 TStringGrid.Cells 等复杂事物的属性值?

转载 作者:行者123 更新时间:2023-12-03 15:00:55 30 4
gpt4 key购买 nike

我将值存储在 xml 和 lua 代码中,并通过 RTTI 访问对象的属性。

var
o, v: TValue; // o is current object
a: TStringDynArray; // params as array
ctx: TRttiContext;
tt: TRttiType;
p: TRttiProperty;
pt: PTypeInfo;
begin
...
ctx := TRttiContext.Create;
try
o := GetLastClassInParams(ctx, obj, a, param_idx);
tt := ctx.GetType(o.TypeInfo);
if high(a) < param_idx then
raise Exception.Create(S_FN + S_NOP);
p := tt.GetProperty(a[param_idx]);
if p = nil then
raise Exception.Create(S_FN + S_PNE + a[param_idx]);
pt := p.PropertyType.Handle;
case p.PropertyType.TypeKind of
tkInteger: v := TValue.From<integer>(integer(Value));
tkEnumeration: v := TValue.FromOrdinal(pt, GetEnumValue(pt, VarToStr(Value)));
tkUString: v := TValue.From<string>(VarToStr(Value));
tkFloat: v := TValue.From<double>(double(Value));
tkSet: begin
temp_int := StringToSet(pt, VarToStr(Value));
TValue.Make(@temp_int, pt, v);
end;
else v := TValue.FromVariant(Value);
end;
p.SetValue(o.AsObject, v);

我可以使用许多属性,例如 Width , Lines.Text TMemo 等,即使有 Panels[0].Width TStatusBar(其中 Panels 是 TCollection 后代),但类似 ​​TStringGrid.Cells[x, y]是我无法解决的事情。有 Embarcadero 的帮助和一些功能,如 GetIndexedProperty (也许这就是我需要的),但那里的解释和 "Gets Indexed Property" 一样好。

如何设置和获取TStringGrid.Cells[x,y]如果我有像 "Cells[1,1]" 这样的字符串存储值,则在运行时通过 RTTI ?

最佳答案

这是我能想到的使用 RTTI 从字符串网格获取和设置值的最简单示例:

var
ctx: TRttiContext;
rttitype: TRttiType;
rttiprop: TRttiIndexedProperty;
value: TValue;
....
rttitype := ctx.GetType(StringGrid1.ClassType);
rttiprop := rttitype.GetIndexedProperty('Cells');
value := rttiprop.GetValue(StringGrid1, [1, 1]);
rttiprop.SetValue(StringGrid1, [1, 1], value.ToString + ' hello');

为了简单起见,我删除了错误检查。我假设您已经知道如何检查错误。

关于delphi - 如何通过 RTTI 设置/获取 TStringGrid.Cells 等复杂事物的属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15702778/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com