using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace RoadFlow.Utility { public static class GuidExtensions { /// /// 生成新的GUID(有的需要生成连续的Guid,所以统一调用这里的方法) /// /// public static Guid NewGuid() { return Guid.NewGuid(); } /// /// 判断为空GUID /// /// /// public static bool IsEmptyGuid(this Guid guid) { return guid == Guid.Empty; } /// /// 判断为空GUID /// /// /// public static bool IsEmptyGuid(this Guid? guid) { return !guid.HasValue || guid.Value == Guid.Empty; } /// /// 判断不为空GUID /// /// /// public static bool IsNotEmptyGuid(this string guid) { if (guid.IsNullOrWhiteSpace()) return false; return guid.ToGuid().IsNotEmptyGuid(); } /// /// 判断不为空GUID /// /// /// public static bool IsNotEmptyGuid(this Guid guid) { return guid != Guid.Empty; } /// /// 判断不为NULL和空GUID /// /// /// public static bool IsNotEmptyGuid(this Guid? guid) { return guid.HasValue && guid.Value != Guid.Empty; } /// /// 将GUID转换为整数 /// /// /// HashCode整数 public static int ToInt(this Guid guid) { return Math.Abs(guid.GetHashCode()); //byte[] buffer = guid.ToByteArray(); //return BitConverter.ToInt32(buffer, 0); } /// /// 转换GUID为大写字符串 /// /// /// public static string ToUpperString(this Guid guid) { return guid.ToString().ToUpper(); } /// /// 转换guid为小写字符串 /// /// /// public static string ToLowerString(this Guid guid) { return guid.ToString().ToLower(); } /// /// 没有分隔线的字符串 /// /// /// public static string ToNString(this Guid guid) { return guid.ToString(); } /// /// 没有分隔线的小写字符串 /// /// /// public static string ToLowerNString(this Guid guid) { return guid.ToString().ToLower(); } /// /// 没有分隔线的大写字符串 /// /// /// public static string ToUpperNString(this Guid guid) { return guid.ToString().ToUpper(); } /// /// 将int转换为GUID /// /// 111 /// 00000000-0000-0000-0000-000000000111 public static Guid ToGuid(this int i) { return i.ToString().PadLeft(32, '0').ToGuid(); } /// /// 将long转换为GUID /// /// 111 /// 00000000-0000-0000-0000-000000000111 public static Guid ToGuid(this long i) { return i.ToString().PadLeft(32, '0').ToGuid(); } /// /// 将GUID转换为int /// /// 00000000-0000-0000-0000-000000000111 的GUID /// 111 public static int ToInteger(this Guid guid) { return guid.ToNString().TrimStart('0').ToInt(); } /// /// 将GUID转换为long /// /// 00000000-0000-0000-0000-000000000111 的GUID /// 111 public static long ToLong(this Guid guid) { return guid.ToNString().TrimStart('0').ToLong(); } #region 生成顺序GUID //private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create(); ///// ///// 创建顺序GUID ///// ///// //public static Guid NewSequentialGuid() //{ // SequentialGuidType sequentialGuidType; // switch (Config.DatabaseType) // { // case "sqlserver": // sequentialGuidType = SequentialGuidType.SequentialAtEnd; // break; // case "oracle": // sequentialGuidType = SequentialGuidType.SequentialAsBinary; // break; // case "mySql": // sequentialGuidType = SequentialGuidType.SequentialAsString; // break; // default: // sequentialGuidType = SequentialGuidType.SequentialAsString; // break; // } // var randomBytes = new byte[10]; // Rng.Locking(r => r.GetBytes(randomBytes)); // long timestamp = DateTime.UtcNow.Ticks / 10000L; // byte[] timestampBytes = BitConverter.GetBytes(timestamp); // if (BitConverter.IsLittleEndian) // { // Array.Reverse(timestampBytes); // } // byte[] guidBytes = new byte[16]; // switch (sequentialGuidType) // { // case SequentialGuidType.SequentialAsString: // case SequentialGuidType.SequentialAsBinary: // Buffer.BlockCopy(timestampBytes, 2, guidBytes, 0, 6); // Buffer.BlockCopy(randomBytes, 0, guidBytes, 6, 10); // if (sequentialGuidType == SequentialGuidType.SequentialAsString && BitConverter.IsLittleEndian) // { // Array.Reverse(guidBytes, 0, 4); // Array.Reverse(guidBytes, 4, 2); // } // break; // case SequentialGuidType.SequentialAtEnd: // Buffer.BlockCopy(randomBytes, 0, guidBytes, 0, 10); // Buffer.BlockCopy(timestampBytes, 2, guidBytes, 10, 6); // break; // } // return new Guid(guidBytes); //} //private enum SequentialGuidType //{ // /// // /// The GUID should be sequential when formatted using the // /// method. // /// // SequentialAsString, // /// // /// The GUID should be sequential when formatted using the // /// method. // /// // SequentialAsBinary, // /// // /// The sequential portion of the GUID should be located at the end // /// of the Data4 block. // /// // SequentialAtEnd //} #endregion } }