问题描述
在我的问题中:如何将发件人"参数与"As"运算符一起使用一次而不是一个类
我选择雷米·勒博(Remy Lebeau)的答案,因为它是在大多数情况下最具活力的技术.它使用RTTI TypInfo类.
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;
示例:我有一个TIBQuery,我想更新SQL属性的文本.但是SQL属性是TStrings类,因此我必须使用SQL.Text.在上面的代码中,它将引发错误无效的属性类型",因为我得到了一个TStrings,后来又尝试设置一个普通的String.
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.
如何使用GetPropInfo到达SQL.Text?是否存在具有SQL属性的TIBQuery和TZQuery的共同祖先,所以我可以更改为该函数参数中的TDataSet而不是TDataSet?
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?
推荐答案
在Delphi 2006中,不能通过RTTI访问TStrings.Text
属性.即使可以,也不必使用RTTI来访问它.由于您知道SQL
属性是TStrings
对象,因此可以简单地从该属性获取实际的对象指针并将其类型转换为TStrings
指针,然后您可以对该对象执行任何操作,例如:
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;
这篇关于如何使用TypInfo RTTI方法为子属性项目设置值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!