From 706d3e06ee7445297473ddd297cdaba8c08b03f7 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Tue, 22 Jul 2025 16:39:57 +0300 Subject: [PATCH] Refactor ChatClientNameAttribute to reduce reflection calls --- .../Volo/Abp/AI/ChatClientNameAttribute.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientNameAttribute.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientNameAttribute.cs index 85c9d66de4..c8303a4de5 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientNameAttribute.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientNameAttribute.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Collections.Concurrent; namespace Volo.Abp.AI; @@ -15,6 +16,8 @@ public class ChatClientNameAttribute : Attribute Name = name; } + private static readonly ConcurrentDictionary _nameCache = new(); + public static string GetChatClientName() { return GetChatClientName(typeof(TChatClient)); @@ -22,16 +25,14 @@ public class ChatClientNameAttribute : Attribute public static string GetChatClientName(Type chatClientType) { - var chatClientNameAttribute = chatClientType - .GetCustomAttributes(true) - .OfType() - .FirstOrDefault(); - - if (chatClientNameAttribute != null) + return _nameCache.GetOrAdd(chatClientType, type => { - return chatClientNameAttribute.Name; - } + var chatClientNameAttribute = type + .GetCustomAttributes(true) + .OfType() + .FirstOrDefault(); - return chatClientType.FullName!; + return chatClientNameAttribute?.Name ?? type.FullName!; + }); } } \ No newline at end of file