TIntegerField、TFloatField などのデータフィールドを含むクラスがあります... Init-Function で、データセットを開いて DB から適切なデータレコードを取得し、値をクラスインスタンスのフィールドに入れたいと考えています。クラスのすべてのフィールドを書き留めたくないので、これを rtti で動的に行います。TFLoatfield などのインスタンスを作成し、FloatField をデータセットからクラスフィールドにコピーする方法を知りたいです。私の関数は floatfiled を見つけますが、インスタンスを作成して値をコピーすることはできません。
var
rttiContext: TRttiContext;
rttiType: TRttiType;
attribute: TCustomAttribute;
rttiField: TRttiField;
begin
myQuery.Close;
myQuery.SQL.Clear;
myQuery.SQL.Add('SELECT * FROM prices');
myQuery.SQL.Add('WHERE ID=' + IntToStr(FID));
myQuery.Open();
try
rttiContext := TRttiContext.Create;
try
rttiType := rttiContext.GetType(TPrice);
for rttiField in rttiType.GetFields do
begin
if rttiField.FieldType.ToString = 'TFloatField' then
begin
// create Instance of Floatfield does not work!
if not assigned(TFloatField(rttiType.GetField(rttiField.Name))) then
TFloatField(rttiType.GetField(rttiField.Name)).Create(nil);
// Copy Floatfield from dataset to classfield does not work!
TFloatField(rttiType.GetField(rttiField.Name)).Value := tmpQuery.FieldByName(rttiField.Name).Value;
end;
end;
finally
rttiContext.Free;
end
finally
myQuery.Close;
end;
end;