CSharp随笔-癫疯之作
181 字
1 分钟
CSharp随笔-癫疯之作

起因是写了这样一段代码
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add){ string ipString = string.Join(",", e.NewItems.Cast<IWebSocketConnection>().Select(x => x.ConnectionInfo.ClientIpAddress)); WeakReferenceMessenger.Default.Send<DisplayAlertMessage>(new("有新用户连接", ipString));}else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove){ string ipString = string.Join(",", e.OldItems.Cast<IWebSocketConnection>().Select(x => x.ConnectionInfo.ClientIpAddress)); WeakReferenceMessenger.Default.Send<DisplayAlertMessage>(new("有用户断开连接", ipString));}else{ throw new InvalidOperationException("不支持此类操作");}这里显然是可以复用一下的 而我又正好想到了C#的新特性 模式匹配
于是有了下面这段@#%&%&#@%&
WeakReferenceMessenger.Default.Send<DisplayAlertMessage>(((Func<(string, System.Collections.IList), (string, string)>)(t => (t.Item1, string.Join(",", t.Item2.Cast<IWebSocketConnection>().Select(x => x.ConnectionInfo.ClientIpAddress)))))(e.Action switch{ System.Collections.Specialized.NotifyCollectionChangedAction.Add => ("有新用户连接", e.NewItems), System.Collections.Specialized.NotifyCollectionChangedAction.Remove => ("有用户断开连接", e.OldItems), _ => throw new InvalidOperationException("不支持此类操作")}).ToDisplayAlertMessage());internal static class ValueTupleExtensions // C#不能把Tuple展开成多个参数 :({ public static DisplayAlertMessage ToDisplayAlertMessage(this ValueTuple<string, string> tuple) { return new DisplayAlertMessage(tuple.Item1, tuple.Item2); }}文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
相关文章智能推荐
1
C#随笔
技术2023-06-22
2
.NET 7中System.IO.Compression的性能测试
技术2023-06-10
3
.NET MAUI 踩坑
技术2023-07-08
4
代码片段
技术2023-07-06
5
建立EntityFrameworkCore项目的模板
技术2026-05-16
随机文章随机推荐
真-大沙子
