Files
2022-03-30 17:54:33 +08:00

251 lines
8.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
{
/// <summary>
/// 生成新的GUID有的需要生成连续的Guid,所以统一调用这里的方法)
/// </summary>
/// <returns></returns>
public static Guid NewGuid()
{
return Guid.NewGuid();
}
/// <summary>
/// 判断为空GUID
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static bool IsEmptyGuid(this Guid guid)
{
return guid == Guid.Empty;
}
/// <summary>
/// 判断为空GUID
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static bool IsEmptyGuid(this Guid? guid)
{
return !guid.HasValue || guid.Value == Guid.Empty;
}
/// <summary>
/// 判断不为空GUID
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static bool IsNotEmptyGuid(this string guid)
{
if (guid.IsNullOrWhiteSpace())
return false;
return guid.ToGuid().IsNotEmptyGuid();
}
/// <summary>
/// 判断不为空GUID
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static bool IsNotEmptyGuid(this Guid guid)
{
return guid != Guid.Empty;
}
/// <summary>
/// 判断不为NULL和空GUID
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static bool IsNotEmptyGuid(this Guid? guid)
{
return guid.HasValue && guid.Value != Guid.Empty;
}
/// <summary>
/// 将GUID转换为整数
/// </summary>
/// <param name="guid"></param>
/// <returns>HashCode整数</returns>
public static int ToInt(this Guid guid)
{
return Math.Abs(guid.GetHashCode());
//byte[] buffer = guid.ToByteArray();
//return BitConverter.ToInt32(buffer, 0);
}
/// <summary>
/// 转换GUID为大写字符串
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static string ToUpperString(this Guid guid)
{
return guid.ToString().ToUpper();
}
/// <summary>
/// 转换guid为小写字符串
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static string ToLowerString(this Guid guid)
{
return guid.ToString().ToLower();
}
/// <summary>
/// 没有分隔线的字符串
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static string ToNString(this Guid guid)
{
return guid.ToString();
}
/// <summary>
/// 没有分隔线的小写字符串
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static string ToLowerNString(this Guid guid)
{
return guid.ToString().ToLower();
}
/// <summary>
/// 没有分隔线的大写字符串
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static string ToUpperNString(this Guid guid)
{
return guid.ToString().ToUpper();
}
/// <summary>
/// 将int转换为GUID
/// </summary>
/// <param name="i">111</param>
/// <returns>00000000-0000-0000-0000-000000000111</returns>
public static Guid ToGuid(this int i)
{
return i.ToString().PadLeft(32, '0').ToGuid();
}
/// <summary>
/// 将long转换为GUID
/// </summary>
/// <param name="i">111</param>
/// <returns>00000000-0000-0000-0000-000000000111</returns>
public static Guid ToGuid(this long i)
{
return i.ToString().PadLeft(32, '0').ToGuid();
}
/// <summary>
/// 将GUID转换为int
/// </summary>
/// <param name="guid">00000000-0000-0000-0000-000000000111 的GUID</param>
/// <returns>111</returns>
public static int ToInteger(this Guid guid)
{
return guid.ToNString().TrimStart('0').ToInt();
}
/// <summary>
/// 将GUID转换为long
/// </summary>
/// <param name="guid">00000000-0000-0000-0000-000000000111 的GUID</param>
/// <returns>111</returns>
public static long ToLong(this Guid guid)
{
return guid.ToNString().TrimStart('0').ToLong();
}
#region GUID
//private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
///// <summary>
///// 创建顺序GUID
///// </summary>
///// <returns></returns>
//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
//{
// /// <summary>
// /// The GUID should be sequential when formatted using the
// /// <see cref="Guid.ToString()" /> method.
// /// </summary>
// SequentialAsString,
// /// <summary>
// /// The GUID should be sequential when formatted using the
// /// <see cref="Guid.ToByteArray" /> method.
// /// </summary>
// SequentialAsBinary,
// /// <summary>
// /// The sequential portion of the GUID should be located at the end
// /// of the Data4 block.
// /// </summary>
// SequentialAtEnd
//}
#endregion
}
}