You are not logged in.
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
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
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