#1 2020-01-10 05:59:53

TPrami
Member
Registered: 2010-07-06
Posts: 116

Does anyone know how to update value of Interface property with RTTI

I've got few interfaces I need to update, they are not inherited from common ancestor (Would have been too easy). They have properties with same name, and datatype.

So to save tons of repeating code I could use RTTI to update the properties by name. Sadly did not find any good example on RTTI and Interface properties.

If someone has code to donate or point me into the right direction, I would be very happy camper...

-Tee-

Offline

#2 2020-01-10 09:26:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,546
Website

Re: Does anyone know how to update value of Interface property with RTTI

To my knownledge, there is no RTTI for interface properties neither in FPC and Delphi.
You just get information about the methods.

Properties information is available only for classes.

Offline

#3 2020-01-16 08:22:40

TPrami
Member
Registered: 2010-07-06
Posts: 116

Re: Does anyone know how to update value of Interface property with RTTI

That was the case, which is kind of bummer.

Went with invoking the Setter-method to update the value. Need to add

  {$RTTI EXPLICIT
    METHODS([vcPublic, vcPublished, vcProtected])
    PROPERTIES([vcPublic, vcPublished, vcProtected])
  }

and use this code to kick Setter:

class procedure TInterfaceMethodInvoker.InvokeMethod<T>(const AIntf: IInterface; const AMathodName: string; const AValue: T);
var
  LObj: TObject;
  LRttiContext: TRttiContext;
  LRttiType: TRttiType;
  LRttiMethod: TRttiMethod;
  LValue: TValue;
begin
  LObj := AIntf as TObject;
  LRttiContext := TRttiContext.Create;
  try
    LRttiType := LRttiContext.GetType(LObj.ClassInfo);
    LRttiMethod := LRttiType.GetMethod(AMathodName);
    LValue := TValue.From<T>(AValue);
    LRttiMethod.Invoke(LObj, [LValue])
  finally
    LRttiContext.Free;
  end;
end;

Offline

Board footer

Powered by FluxBB