I valori sono memorizzati in codice xml e lua e accesso alle proprietà dell'oggetto tramite RTTI.Come posso impostare/ottenere il valore della proprietà tramite RTTI per oggetti compex come TStringGrid.Cells?
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);
posso lavorare con molte proprietà come Width
, Lines.Text
di TMemo ecc, anche con Panels[0].Width
di TStatusBar (in cui i pannelli è TCollection discendente), ma cosa come TStringGrid.Cells[x, y]
è qualcosa che non riesco a risolvere. C'è aiuto su Embarcadero e alcune funzioni come GetIndexedProperty
(forse è quello che mi serve), ma la spiegazione è valida come "Gets Indexed Property"
.
Come impostare e ottenere TStringGrid.Cells[x,y]
tramite RTTI in fase di esecuzione se i valori sono memorizzati come stringhe come "Cells[1,1]"
?
Grazie. C'è un modo per trovare quando dovrei usare GetIndexedProperty e quando GetProperty? – user2091150
OK, suppongo che tu stia analizzando il testo in questo modo: 'Celle [1,1]: = ...'. Nel qual caso la presenza di '[]' ti dice che è una proprietà indicizzata. Puoi anche usare 'TRttiType.GetIndexedProperties' e verificare se la tua proprietà è in quell'elenco. –
Sì, ma curioso è Delphi sa che la proprietà è indicizzata e forse RTTI può restituire in qualche modo IsIndexed. Grazie. – user2091150