delphi - 如何获取(方法: TRttiMethod) in TVirtualInterface TVirtualInterfaceInvokeEvent?

标签 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;

我想获取方法:TRttiMethod 的 TRttiProperty,但是如何获取? 如果我将界面更改为:

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;

最佳答案

不幸的是,你不能。不会为接口(interface)属性生成 RTTI,因此您的自定义属性无需附加任何内容。即使没有警告,您对接口(interface)属性的装饰也没有任何效果。

关于delphi - 如何获取(方法: TRttiMethod) in TVirtualInterface TVirtualInterfaceInvokeEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39151187/

相关文章:

Delphi 脚本和 ASM

rest - 如何模拟超时属性?

Delphi 10.1 Firemonkey - 检测组件外部的鼠标单击

delphi - 模态窗口后面的网格滚动

security - Delphi:安全/加密从互联网下载更新

delphi - 是否可以确定 dbEdit 中的文本是否比可见文本长?

Delphi RTTI 使用 Method.Invoke 作为 tkEnumeration 参数

跨dll的c++类冲突

delphi - 从指向记录类型 rtti 字段的指针获取值

delphi - 如何使用 like 子句和元音变音来过滤数据集中的字符串字段?