문자열 속성이 쉬운 경우 말한대로 단위에서 GetStrProp
및 SetStrProp
을 호출한다고 가정합니다. GetObjectProp
및 SetObjectProp
을 사용하면 클래스 유형 속성을 동일하게 쉽게 만들 수 있습니다. 속성을 필요로
(GetObjectProp(Obj, 'data') as TMyClass).Go;
은 가지고 :, 당신은 좀 더 직접적으로 갈 수
if Supports(GetObjectProp(Obj, 'data'), IMyInterface, Intf) then
Intf.Go;
당신은 정말 인터페이스를 필요로하지 않는 경우, 당신은 data
속성이 TMyClass
입력이 알고 null 이외의 값
원하는 부동산의 이름을 모르는 경우 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;
RTTI는 Delphi 2010에서 훨씬 개선되었습니다. rtti.pas라는 이름의 사용하기 쉬운 단위가 있습니다. 다른 말로하면 d2010으로 전환하면 통증이 줄어 듭니다. –
델파이에는 멋진 새로운 기능이 많이 있습니다. 그러나이 프로젝트에서 나는 정말로 할 수 없습니다 :/-하지만 2010 년 재판을하려고합니다. 내 상사를 납득 시키십시오 : D – Leo
현재 embarcadero가 독일의 roadshow에 있습니다. http://devtracks.de –