Если строковое свойство легко, как вы говорите, то я предполагаю, что вы вызываете GetStrProp
и SetStrProp
из модуля TypInfo
. Свойства типа класса могут быть одинаково просты с GetObjectProp
и SetObjectProp
.
if Supports(GetObjectProp(Obj, 'data'), IMyInterface, Intf) then
Intf.Go;
Если вам не особо нужен интерфейс, и вы знаете, что свойство data
имеет тип TMyClass
, то можно пойти более прямым путем:
(GetObjectProp(Obj, 'data') as TMyClass).Go;
Это требует, чтобы свойство имело ненулевое значение.
Если вы не знаете имя нужного вам свойства, вы можете использовать некоторые другие вещи в TypInfo
для его поиска. Например, вот функция, которая найдет все опубликованные свойства объекта, имеющие значения, реализующие IMyInterface
; он вызывает Go
для каждого из них в произвольном порядке.
procedure GoAllProperties(Other: TObject);
var
Properties: PPropList;
nProperties: Integer;
Info: PPropInfo;
Obj: TObject;
Intf: IMyInterface;
Unk: IUnknown;
begin
// Get a list of all the object's published properties
nProperties := GetPropList(Other.ClassInfo, Properties);
if nProperties > 0 then try
// Optional: sort the list
SortPropList(Properties, nProperties);
for i := 0 to Pred(nProperties) do begin
Info := Properties^[i];
// Skip write-only properties
if not Assigned(Info.GetProc) then
continue;
// Check what type the property holds
case Info.PropType^^.Kind of
tkClass: begin
// Get the object reference from the property
Obj := GetObjectProp(Other, Info);
// Check whether it implements IMyInterface
if Supports(Obj, IMyInterface, Intf) then
Intf.Go;
end;
tkInterface: begin
// Get the interface reference from the property
Unk := GetInterfaceProp(Obj, Info);
// Check whether it implements IMyInterface
if Supports(Unk, IMyInterface, Intf) then
Intf.Go;
end;
end;
end;
finally
FreeMem(Properties);
end;
end;
person
Rob Kennedy
schedule
09.09.2009