From 8cd260612a8ab10387af7f05594a58ce26f44d35 Mon Sep 17 00:00:00 2001 From: fengjiayi <12821976+ning_xi@user.noreply.gitee.com> Date: Mon, 25 Aug 2025 11:05:05 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=B8=BA=20FlowContext=EF=BC=88=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E4=B8=8A=E4=B8=8B=E6=96=87=EF=BC=89=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=20Tag=20=E5=B1=9E=E6=80=A7=E6=9C=89=E5=85=B3=E7=9A=84?= =?UTF-8?q?=20Set/Get=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/FlowNode/FlowContext.cs | 74 ++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/Library/FlowNode/FlowContext.cs b/Library/FlowNode/FlowContext.cs index f5227a1..44b6c2e 100644 --- a/Library/FlowNode/FlowContext.cs +++ b/Library/FlowNode/FlowContext.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reactive.Concurrency; using System.Security.Cryptography.X509Certificates; using System.Threading; @@ -30,9 +31,11 @@ namespace Serein.Library /// /// 用于同一个流程上下文中共享、存储任意数据 /// 流程完毕时,如果存储的对象实现了 IDisposable 接口,将会自动调用 - /// 谨慎使用,注意数据的生命周期和内存管理 + /// 该属性的 set 仅限内部访问,如需赋值,请通过 SetTag() + /// 请谨慎使用,请注意数据的生命周期和内存管理 /// - public object? Tag { get; set; } + public object? Tag { get;private set; } + /// @@ -257,6 +260,73 @@ namespace Serein.Library throw new InvalidOperationException($"透传{nodeModel}节点数据时发生异常:上一节点不存在数据"); } + private object _tagLockObj = new object(); + + /// + /// 设置共享对象,不建议设置非托管对象 + /// + /// + private void SetTag(object tag) + { + lock (_tagLockObj) + { + Tag = tag; + } + } + + /// + /// 获取共享对象(将在同一个 Web Socket 调起的上下文中保持一致) + /// + /// + private T? GetTag() + { + TryGetTag(out T? tag); + return tag; + } + +#if NET6_0_OR_GREATER + /// + /// 获取共享对象(将在同一个 Web Socket 调起的上下文中保持一致) + /// + /// + /// + private bool TryGetTag([NotNullWhen(true)] out T? tag) + { + lock (_tagLockObj) + { + if (Tag is T t) + { + tag = t; + return true; + } + tag = default; + return false; + } + } + +#else + /// + /// 获取共享对象(将在同一个 Web Socket 调起的上下文中保持一致) + /// + /// + /// + private bool TryGetTag(out T? tag) + { + lock (_tagLockObj) + { + if (Tag is T t) + { + tag = t; + return true; + } + tag = default; + return false; + } + } + +#endif + + /// /// 重置 ///