我正在实现一个包来转换和自动生成delphi中的组件。我知道GExperts有一个类似的函数,但是我需要定制一些特定的属性。
现在我只能访问TADOQuery.SQL属性,这是TStrings的一个实例
var
aVal : TValue;
aSqlS : TStrings;
begin
[...]
if (mycomp.GetComponentType = 'TADOQuery') then
if mycomp.GetPropValueByName('SQL', aVal) then
begin
aSqlS := TStrings(aVal.AsClass);
if Assigned(aSqlS) then <----- problem is here
ShowMessage(aSqlS.Text); <----- problem is here
end;
end;我不太确定使用RTTI的TValue是否正确。
谢谢
发布于 2017-01-29 18:50:45
假设GetPropValueByName()返回有效的TValue (您没有显示代码),那么使用aVal.AsClass是错误的,因为SQL属性getter不返回元类类型。它返回一个对象指针,因此可以使用aVal.AsObject,甚至aVal.AsType<TStrings>。
如果更新实际上是IOTAComponent而不是TValue,那么使用它肯定是错误的。IOTAComponent.GetPropValueByName()的输出是一个非类型化的var,它接收属性值的原始数据,或者是用于TPersistent-derived对象的IOTAComponent:
var
aVal: IOTAComponent;
aSqlS : TStrings;
begin
[...]
if (mycomp.GetComponentType = 'TADOQuery') then
if mycomp.PropValueByName('SQL', aVal) then
ShowMessage(TStrings(aVal.GetComponentHandle).Text);
end;但是,更好的选择是访问实际的TADOQuery对象:
if (mycomp.GetComponentType = 'TADOQuery') then
ShowMessage(TADOQuery(comp.GetComponentHandle).SQL.Text);https://stackoverflow.com/questions/41920888
复制相似问题