//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.Collections.Concurrent; using System.Globalization; using System.Reflection; namespace TouchSocket.Core { /// /// 为System提供扩展。 /// public static class SystemExtensions { #region 其他 /// /// 安全性释放(不用判断对象是否为空)。不会抛出任何异常。 /// /// /// public static void SafeDispose(this IDisposable dis) { if (dis == default) { return; } try { dis.Dispose(); } catch { } } #endregion 其他 /// /// 获取自定义attribute /// /// /// /// public static T GetAttribute(this Enum enumObj) where T : Attribute { Type type = enumObj.GetType(); Attribute attr = null; string enumName = Enum.GetName(type, enumObj); //获取对应的枚举名 FieldInfo field = type.GetField(enumName); attr = field.GetCustomAttribute(typeof(T), false); return (T)attr; } /// /// 格林尼治标准时间 /// /// /// /// public static string ToGMTString(this DateTime dt, string v) { return dt.ToString("r", CultureInfo.InvariantCulture); } #if !NETCOREAPP3_1_OR_GREATER /// /// 清除所有成员 /// /// /// public static void Clear(this ConcurrentQueue queue) { while (queue.TryDequeue(out _)) { } } #endif /// /// 清除所有成员 /// /// /// /// public static void Clear(this ConcurrentQueue queue, Action action) { while (queue.TryDequeue(out T t)) { action?.Invoke(t); } } /// /// 获取字节中的指定Bit的值 /// /// 字节 /// Bit的索引值(0-7) /// public static int GetBit(this byte @this, short index) { byte x; switch (index) { case 0: { x = 0x01; } break; case 1: { x = 0x02; } break; case 2: { x = 0x04; } break; case 3: { x = 0x08; } break; case 4: { x = 0x10; } break; case 5: { x = 0x20; } break; case 6: { x = 0x40; } break; case 7: { x = 0x80; } break; default: { return 0; } } return (@this & x) == x ? 1 : 0; } /// /// 设置字节中的指定Bit的值 /// /// 字节 /// Bit的索引值(0-7) /// Bit值(0,1) /// public static byte SetBit(this byte @this, short index, int bitvalue) { var _byte = @this; if (bitvalue == 1) { switch (index) { case 0: { return _byte |= 0x01; } case 1: { return _byte |= 0x02; } case 2: { return _byte |= 0x04; } case 3: { return _byte |= 0x08; } case 4: { return _byte |= 0x10; } case 5: { return _byte |= 0x20; } case 6: { return _byte |= 0x40; } case 7: { return _byte |= 0x80; } default: { return _byte; } } } else { switch (index) { case 0: { return _byte &= 0xFE; } case 1: { return _byte &= 0xFD; } case 2: { return _byte &= 0xFB; } case 3: { return _byte &= 0xF7; } case 4: { return _byte &= 0xEF; } case 5: { return _byte &= 0xDF; } case 6: { return _byte &= 0xBF; } case 7: { return _byte &= 0x7F; } default: { return _byte; } } } } } }