From c454fa1acf1351a7684ec97dc1e364d6958d4f91 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Wed, 25 Feb 2026 21:33:41 +0800 Subject: [PATCH] =?UTF-8?q?perf(GFramework.Core):=20=E4=BC=98=E5=8C=96Prio?= =?UTF-8?q?rityEvent=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=E5=99=A8=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在MergeAndSortHandlers方法中创建_handlers和_contextHandlers的快照副本 - 避免在迭代期间直接访问集合可能发生的修改问题 - 提高事件处理时的性能表现和稳定性 --- GFramework.Core/events/PriorityEvent.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/GFramework.Core/events/PriorityEvent.cs b/GFramework.Core/events/PriorityEvent.cs index e3dc4c6..5be3376 100644 --- a/GFramework.Core/events/PriorityEvent.cs +++ b/GFramework.Core/events/PriorityEvent.cs @@ -191,11 +191,13 @@ public class PriorityEvent : IEvent private List<(int Priority, Action? Handler, Action>? ContextHandler, bool IsContext)> MergeAndSortHandlers(T t) { + var normalSnapshot = _handlers.ToArray(); + var contextSnapshot = _contextHandlers.ToArray(); // 使用快照避免迭代期间修改 - return _handlers + return normalSnapshot .Select(h => (h.Priority, Handler: (Action?)(() => h.Handler.Invoke(t)), ContextHandler: (Action>?)null, IsContext: false)) - .Concat(_contextHandlers + .Concat(contextSnapshot .Select(h => (h.Priority, Handler: (Action?)null, ContextHandler: (Action>?)h.Handler, IsContext: true))) .OrderByDescending(h => h.Priority)