如何在TVirtualInterface TVirtualInterfaceInvokeEvent中获取(Method:TRttiMethod)的所有权属性?

时间:2016-08-25 17:17:06

标签: delphi rtti delphi-10.1-berlin

我如何在TVirtualInterface类的OnInvoke方法中获取Method:TRttiMethod的所有权属性?

我有这个界面:

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;

和这堂课:

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;

我想获得方法的TRttiProperty:TRttiMethod,但是怎么样? 如果我将界面更改为:

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;

代码有效,但我喜欢这样的用户界面:

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;

1 个答案:

答案 0 :(得分:6)

不幸的是,你不能。没有为界面属性生成RTTI,因此您的自定义属性无法附加到其中。即使没有警告,您对界面属性的修饰也没有效果。