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

前端之家收集整理的这篇文章主要介绍了delphi – 如何在TVirtualInterface TVirtualInterfaceInvokeEvent中获取(Method:TRttiMethod)的所有权属性?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我如何在TVirtualInterface类的OnInvoke方法中获得Method:TRttiMethod的所有权属性

我有这个界面:

  1. IPerson = interface(IInvokable)
  2. ['{45CE428C-F880-4D61-A2C1-0F3CB47130B5}']
  3. procedure SetName(const Value: string);
  4. function GetName(): string;
  5.  
  6. [TEntityField('Field Name Here')]
  7. property Name: string read GetName write SetName;
  8. end;

而这堂课:

  1. type
  2. TVirtualEntity<T: IInvokable> = class(TVirtualInterface)
  3. public
  4. constructor Create(); reintroduce; overload;
  5. end;
  6.  
  7. constructor TVirtualEntity<T>.Create;
  8. begin
  9. inherited Create(TypeInfo(T));
  10. Self.OnInvoke :=
  11. procedure(Method: TRttiMethod; const Args: TArray<TValue>; out Result: TValue)
  12. var
  13. attributes: TArray<TCustomAttribute>;
  14. attributesManager: TAttributesManager;
  15. entityFieldAttribute: TEntityField;
  16. begin
  17. attributesManager := TAttributesManager.Create(Method.GetAttributes);
  18. try
  19. if attributesManager.HasAttribute<TEntityField>() then
  20. begin
  21. Result := attributesManager.GetAttribute<TEntityField>.FieldName;
  22. end;
  23.  
  24. finally
  25. attributesManager.Free;
  26. end;
  27. end;
  28. end;

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

  1. IPerson = interface(IInvokable)
  2. ['{45CE428C-F880-4D61-A2C1-0F3CB47130B5}']
  3. procedure SetName(const Value: string);
  4. [TEntityField('Field Name Here')]
  5. function GetName(): string;
  6.  
  7. property Name: string read GetName write SetName;
  8. end;

代码工作,但我想用这样的用户界面:

  1. IPerson = interface(IInvokable)
  2. ['{45CE428C-F880-4D61-A2C1-0F3CB47130B5}']
  3. procedure SetName(const Value: string);
  4. function GetName(): string;
  5.  
  6. [TEntityField('Field Name Here')]
  7. property Name: string read GetName write SetName;
  8. end;

解决方法

不幸的是,你做不到.没有为接口属性生成RTTI,因此没有任何内容可以附加到自定义属性.即使没有警告,您对interface属性的修饰也没有任何效果.

猜你在找的Delphi相关文章