如您所说,如果字符串属性很简单,那么我假设您是从单元调用GetStrProp
和调用的。使用和可以同样简单地使用类类型属性。SetStrProp
TypInfo
GetObjectProp
SetObjectProp
if Supports(GetObjectProp(Obj, 'data'), IMyInterface, Intf) then
Intf.Go;
如果你真的不需要接口,并且你知道data
属性有 type 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;