I have a class which should contain datafields like TIntegerField, TFloatField, ... In an Init-Function I want to open a dataset to get the right datarecord from DB and put the values to the fields in the classinstance. Because I don't want to write down all the fields of the class I want do this dynamicly with rtti. I wonder how I can create an instance e.g. of a TFLoatfield and copy the FloatField from dataset to classfield. My function finds the floatfiled but I cannot create the Instance and copy the value.
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;