how i get ownership property of Method: TRttiMethod in OnInvoke method of TVirtualInterface class?
I have this interface:
IPerson = interface(IInvokable)
['{45CE428C-F880-4D61-A2C1-0F3CB47130B5}']
procedure SetName(const Value: string);
function GetName(): string;
[TEntityField('Field Name Here')]
property Name: string read GetName write SetName;
end;
and this class:
type
TVirtualEntity<T: IInvokable> = class(TVirtualInterface)
public
constructor Create(); reintroduce; overload;
end;
constructor TVirtualEntity<T>.Create;
begin
inherited Create(TypeInfo(T));
Self.OnInvoke :=
procedure(Method: TRttiMethod; const Args: TArray<TValue>; out Result: TValue)
var
attributes: TArray<TCustomAttribute>;
attributesManager: TAttributesManager;
entityFieldAttribute: TEntityField;
begin
attributesManager := TAttributesManager.Create(Method.GetAttributes);
try
if attributesManager.HasAttribute<TEntityField>() then
begin
Result := attributesManager.GetAttribute<TEntityField>.FieldName;
end;
finally
attributesManager.Free;
end;
end;
end;
I'd like to get TRttiProperty of Method: TRttiMethod, but how? if i change the interface to:
IPerson = interface(IInvokable)
['{45CE428C-F880-4D61-A2C1-0F3CB47130B5}']
procedure SetName(const Value: string);
[TEntityField('Field Name Here')]
function GetName(): string;
property Name: string read GetName write SetName;
end;
the code works, but i'd like to user interfaces like this:
IPerson = interface(IInvokable)
['{45CE428C-F880-4D61-A2C1-0F3CB47130B5}']
procedure SetName(const Value: string);
function GetName(): string;
[TEntityField('Field Name Here')]
property Name: string read GetName write SetName;
end;
Method.Parent
returns the interface type. So yes, you could decorate the interface with multiple attributes (one per entity field) and resolve the correct one by method naming convention. Another possibility is to just decorate the relevant methods: either with extended attributes specifying getter/setter flags, or perhaps this could be detected in code by checking if the method returns a result (getter) or not (setter). – Handmaiden