File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
framework/src/Bing/Bing/Threading Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ using System . Collections . Concurrent ;
2+ using System . Threading ;
3+
4+ namespace Bing . Threading
5+ {
6+ /// <summary>
7+ /// 调用上下文
8+ /// </summary>
9+ public static class CallContext
10+ {
11+ /// <summary>
12+ /// 状态字典
13+ /// </summary>
14+ private static readonly ConcurrentDictionary < string , AsyncLocal < object > > _state = new ConcurrentDictionary < string , AsyncLocal < object > > ( ) ;
15+
16+ /// <summary>
17+ /// 设置值
18+ /// </summary>
19+ /// <param name="name">键名</param>
20+ /// <param name="data">数据</param>
21+ public static void SetValue ( string name , object data ) => _state . GetOrAdd ( name , _ => new AsyncLocal < object > ( ) ) . Value = data ;
22+
23+ /// <summary>
24+ /// 获取值
25+ /// </summary>
26+ /// <param name="name">键名</param>
27+ /// <returns>如果指定键名不存在,则返回 null。</returns>
28+ public static object GetValue ( string name ) => _state . TryGetValue ( name , out var data ) ? data . Value : null ;
29+
30+ /// <summary>
31+ /// 移除
32+ /// </summary>
33+ /// <param name="name">键名</param>
34+ public static void Remove ( string name ) => _state . TryRemove ( name , out _ ) ;
35+
36+ /// <summary>
37+ /// 清空
38+ /// </summary>
39+ public static void Clear ( ) => _state . Clear ( ) ;
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments