Skip to content

Commit

Permalink
Added GetPropertyKeyByName
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Dec 30, 2024
1 parent 2199d45 commit 4066123
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sources/MVCFramework.Serializer.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ TMVCSerializerHelper = record
overload; static;
class function GetKeyName(const AProperty: TRttiProperty; const AType: TRttiType): string;
overload; static;
class function GetPropertyKeyName(const APropertyName: String; const AClass: TClass): string; static;
class function HasAttribute<T: class>(const AMember: TRttiObject): Boolean; overload; static;
class function HasAttribute<T: class>(const AMember: TRttiObject; out AAttribute: T): Boolean;
overload; static;
Expand Down Expand Up @@ -863,6 +864,34 @@ class function TMVCSerializerHelper.GetKeyName(const AProperty: TRttiProperty;
Result := TMVCSerializerHelper.ApplyNameCase(MVCNameCaseDefault, Result);
end;

class function TMVCSerializerHelper.GetPropertyKeyName(const APropertyName: String; const AClass: TClass): string;
var
Context: TRttiContext;
ObjectType: TRttiType;
lProp: TRttiProperty;
begin
{$IF not Defined(TokyoOrBetter)}
Result := nil;
{$ENDIF}
Context := TRttiContext.Create;
try
ObjectType := Context.FindType(AClass.QualifiedClassName);
if not Assigned(ObjectType) then
raise Exception.CreateFmt
('Cannot find RTTI for %s. Hint: Is the specified classtype linked in the module?',
[AClass.QualifiedClassName])
else
begin
lProp := ObjectType.GetProperty(APropertyName);
if not Assigned(lProp) then
raise Exception.Create('Cannot find property ' + APropertyName);
Result := TMVCSerializerHelper.GetKeyName(lProp, ObjectType);
end;
finally
Context.Free;
end;
end;

class function TMVCSerializerHelper.GetTypeKindAsString(const ATypeKind: TTypeKind): string;
begin
Result := GetEnumName(TypeInfo(TTypeKind), Ord(ATypeKind));
Expand Down

0 comments on commit 4066123

Please sign in to comment.