Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2007 Mar : Is it possible to get Field value dynamicly during runtime?

www.cryer.info
Managed Newsgroup Archive

Is it possible to get Field value dynamicly during runtime?

Subject:Is it possible to get Field value dynamicly during runtime?
Posted by:"xy" (..@delphi.com)
Date:Mon, 26 Mar 2007 14:36:37

I want a function like GetValueFromFieldName(Order, 'OrderNo');
  TOrder = class(TObject)
    OrderNo: string;
    OrderName: string;
  end;

The jvcl method provide access to property value at runtime, but I want
Field value:(.
function GetValueFromPropertyName(Component: TObject; const PropertyName:
string): string;
var
  PropInfo: PPropInfo;
  TypeInf, PropTypeInf: PTypeInfo;
  TypeData: PTypeData;
  I: Integer;
  AName, PropName: string;
  PropList: PPropList;
  NumProps: Word;
  PropObject: TObject;
begin
  { Playing with RTTI }
  TypeInf := Component.ClassInfo;
  AName := TypeInf^.Name;
  TypeData := GetTypeData(TypeInf);
  NumProps := TypeData^.PropCount;

  Result := '';
  GetMem(PropList, NumProps * SizeOf(Pointer));
  try
    { Retrieving list of properties [translated] }
    GetPropInfos(TypeInf, PropList);

    for I := 0 to NumProps - 1 do
    begin
      PropName := PropList^[I]^.Name;
      PropTypeInf := PropList^[I]^.PropType^;
      PropInfo := PropList^[I];

      if PropTypeInf^.Kind = tkClass then
      begin
        PropObject := GetObjectProp(Component, PropInfo);
        Result := GetValueFromPropertyName(PropObject, PropertyName);
      end
      else
      if CompareText(PropName, PropertyName) = 0 then
      begin
        Result := GetPropValue(Component, PropName, True);
        Break;
      end;

      if Result <> '' then
        Exit;
    end;
  finally
    FreeMem(PropList);
  end;
end;

Replies:

www.cryer.info
Managed Newsgroup Archive