首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Delphi获取组件属性

Delphi获取组件属性
EN

Stack Overflow用户
提问于 2017-01-29 12:28:18
回答 1查看 158关注 0票数 0

我正在实现一个包来转换和自动生成delphi中的组件。我知道GExperts有一个类似的函数,但是我需要定制一些特定的属性。

现在我只能访问TADOQuery.SQL属性,这是TStrings的一个实例

代码语言:javascript
复制
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是否正确。

谢谢

EN

回答 1

Stack Overflow用户

发布于 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

代码语言:javascript
复制
var
  aVal: IOTAComponent;
  aSqlS : TStrings;
begin
    [...]
    if (mycomp.GetComponentType = 'TADOQuery') then
      if mycomp.PropValueByName('SQL', aVal) then
        ShowMessage(TStrings(aVal.GetComponentHandle).Text);
end;

但是,更好的选择是访问实际的TADOQuery对象:

代码语言:javascript
复制
if (mycomp.GetComponentType = 'TADOQuery') then
  ShowMessage(TADOQuery(comp.GetComponentHandle).SQL.Text);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41920888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档