From 4066123f375c357b20909189dc38a679507ea140 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Mon, 30 Dec 2024 18:03:27 +0100 Subject: [PATCH] Added GetPropertyKeyByName --- sources/MVCFramework.Serializer.Commons.pas | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sources/MVCFramework.Serializer.Commons.pas b/sources/MVCFramework.Serializer.Commons.pas index 9f82b168..b1323ccf 100644 --- a/sources/MVCFramework.Serializer.Commons.pas +++ b/sources/MVCFramework.Serializer.Commons.pas @@ -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(const AMember: TRttiObject): Boolean; overload; static; class function HasAttribute(const AMember: TRttiObject; out AAttribute: T): Boolean; overload; static; @@ -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));