Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get ownership property of (Method: TRttiMethod) in TVirtualInterface TVirtualInterfaceInvokeEvent?

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;
like image 892
Passella Avatar asked Aug 25 '16 17:08

Passella


1 Answers

Unfortunately, you can't. There is no RTTI generated for interface properties so there's nothing for your custom attribute to be attached to. Your decoration of the interface property has no effect, even if there's no warning.

like image 141
Ondrej Kelle Avatar answered Oct 10 '22 10:10

Ondrej Kelle