Delphi OpenTools API get component property

183 views Asked by At

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

1

There are 1 answers

0
Remy Lebeau On

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);