简体   繁体   中英

Delphi OpenTools API get component property

I'm implementing a package to convert and auto-generate components in the delphi IDE. I'm aware that GExperts has a similar function but I need to customize some specific properties.

Right now I'm stuck on accessing the TADOQuery.SQL property, which is an instance of 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;

I'm not really sure whether using TValue from RTTI is the correct way to go.

Thanks

Assuming GetPropValueByName() is returning a valid TValue (you did not show that code), then using aVal.AsClass is wrong since the SQL property getter does not return a metaclass type. It returns an object pointer, so use aVal.AsObject instead, or even aVal.AsType<TStrings> .


Update If comp is actually IOTAComponent than TValue is definitely wrong to use at all. The output of IOTAComponent.GetPropValueByName() is an untyped var that receives the raw data of the property value, or an IOTAComponent for TPersistent -derived objects:

var
  aVal: IOTAComponent;
  aSqlS : TStrings;
begin
    [...]
    if (mycomp.GetComponentType = 'TADOQuery') then
      if mycomp.PropValueByName('SQL', aVal) then
        ShowMessage(TStrings(aVal.GetComponentHandle).Text);
end;

However, a better option would be to access the actual TADOQuery object instead:

if (mycomp.GetComponentType = 'TADOQuery') then
  ShowMessage(TADOQuery(comp.GetComponentHandle).SQL.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM