问题
In my Question: How to use “Sender” parameter with “As” operator for more then one class at a time
I choose the Remy Lebeau's Answer because it was the most dynamic tech for most situations like that. It uses the RTTI TypInfo Class.
But as I was using this class, another problem came: How do we Set a sub-property value?
function TRemote.UpdateQuery(DataSet: TDataSet; SQL: String): Boolean;
var
PropInfo: PPropInfo;
begin
{ atualiza o código SQL padrão de um dataSet de consulta tipo View }
PropInfo := GetPropInfo(DataSet, 'SQL', []);
if not Assigned(PropInfo) then
begin
Result := False;
Exit;
end;
try
DataSet.Close;
SetPropValue(DataSet, PropInfo, SQL);
DataSet.Open;
Result := True;
except
Result := False;
end;
end;
Example: I have a TIBQuery and I want to update the text of the SQL property. But the SQL property is a TStrings class, so I have to use SQL.Text. In the code above, It will raise an error "Invalid Property Type" because I got a TStrings and later I try to Set a normal String.
How to reach the SQL.Text using GetPropInfo? Is there a common ancestor of TIBQuery and TZQuery that has the SQL property, so I can change to, instead of the TDataSet in the function parameter?
回答1:
The TStrings.Text
property is not accessible via RTTI in Delphi 2006. Even if it were, you do not need to use RTTI to access it anyway. Since you know the SQL
property is a TStrings
object, you can simply retreive the actual object pointer from the property and type-cast it to a TStrings
pointer, then you can do whatever you need to do with that object, eg:
function TRemote.UpdateQuery(DataSet: TDataSet; SQL: String): Boolean;
var
PropInfo: PPropInfo;
SQLObj: TStrings;
begin
Result := False;
try
PropInfo := GetPropInfo(DataSet, 'SQL', [tkClass]);
if not Assigned(PropInfo) then Exit;
SQLObj := TStrings(GetObjectProp(DataSet, PropInfo, TStrings));
if not Assigned(SQLObj) then Exit;
DataSet.Close;
SQLObj.Text := SQL;
DataSet.Open;
Result := True;
except
end;
end;
来源:https://stackoverflow.com/questions/11728068/how-to-set-a-value-to-a-sub-property-item-using-typinfo-rtti-methods