* Reworked patch from Henrique Gottardo with additional function to get custom attribute (merge request pas2js rtti.pas)

This commit is contained in:
Michaël Van Canneyt 2023-03-09 17:42:24 +01:00
parent 0a4390f54a
commit 882044c2e0

View File

@ -118,6 +118,10 @@ type
destructor Destroy; override;
function GetAttributes: TCustomAttributeArray;
generic function GetAttribute<T: TCustomAttribute>: T;
function GetAttribute(const Attribute: TCustomAttributeClass): TCustomAttribute;
generic function HasAttribute<T: TCustomAttribute>: Boolean;
function HasAttribute(const Attribute: TCustomAttributeClass): Boolean;
property Attributes: TCustomAttributeArray read GetAttributes;
property Handle: Pointer read FHandle;
@ -1638,6 +1642,34 @@ begin
Result := FAttributes;
end;
function TRttiObject.GetAttribute(const Attribute: TCustomAttributeClass): TCustomAttribute;
var
CustomAttribute: TCustomAttribute;
begin
Result := nil;
for CustomAttribute in GetAttributes do
if CustomAttribute is Attribute then
Exit(CustomAttribute);
end;
generic function TRttiObject.GetAttribute<T>: T;
begin
Result := T(GetAttribute(TCustomAttributeClass(T.ClassType)));
end;
function TRttiObject.HasAttribute(const Attribute: TCustomAttributeClass): Boolean;
begin
Result := GetAttribute(Attribute) <> nil;
end;
generic function TRttiObject.HasAttribute<T>: Boolean;
begin
Result := HasAttribute(TCustomAttributeClass(T.ClassType));
end;
{ TRttiNamedObject }
function TRttiNamedObject.GetName: string;