2009. április 26., vasárnap

How to retrieve all string properties and their values from any TObject descendant


Problem/Question/Abstract:

How to retrieve all string properties and their values from any TObject descendant

Answer:

{ ... }
var
  XObject: TObject;
  XProps: PPropList;
  XPropInfo: PPropInfo;
  XTypeData: PTypeData;
  XPropsStr: string;
  i: integer;
  { ... }
begin
  { ... }
  XObject := OneOfYourComponents;
  XTypeData := TypInfo.GetTypeData(XObject.ClassInfo);
  XPropsStr := '';
  GetMem(XProps, XTypeData^.PropCount * sizeof(Pointer));
  try
    TypInfo.GetPropInfos(XObject.ClassInfo, XProps);
    for i := 0 to XTypeData.PropCount - 1 do
    begin
      XPropInfo := XProps[i];
      if Assigned(XPropInfo) then
      begin
        if XPropInfo.PropType^^.Kind in [tkString, tkLString, tkWString] then
          XPropsStr := XPropsStr + XProps[i].Name + ' = ' +
            GetStrProp(XObject, XProps[i].Name) + #$0D#$0A;
      end;
    end;
  finally
    FreeMem(XProps);
  end;
  ShowMessage(XPropsStr);
  { ... }

Nincsenek megjegyzések:

Megjegyzés küldése