如何在 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/