C# 如何在 .net 中验证 Guid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2370689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate Guid in .net
提问by AjmeraInfo
Please tell me how to validate GUID in .net and it is unique for always?
请告诉我如何在 .net 中验证 GUID 并且它始终是唯一的?
采纳答案by Kyle Rozendo
Guid's are unique 99.99999999999999999999999999999999% of the time.
Guid 是唯一的 99.99999999999999999999999999999999% 的时间。
It depends on what you mean by validate?
这取决于您所说的验证是什么意思?
Code to determine that a Guid string is in fact a Guid, is as follows:
确定 Guid 字符串实际上是 Guid 的代码如下:
private static Regex isGuid =
new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);
internal static bool IsGuid(string candidate, out Guid output)
{
bool isValid = false;
output = Guid.Empty;
if(candidate != null)
{
if (isGuid.IsMatch(candidate))
{
output=new Guid(candidate);
isValid = true;
}
}
return isValid;
}
回答by Kerido
You cannot validate GUID's uniqueness. You just hope it was generated with a tool that produces unique 16 bytes. As for validation, this simple code might work (assuming you are dealing with GUID's string representation:
您无法验证 GUID 的唯一性。你只是希望它是用产生唯一 16 个字节的工具生成的。至于验证,这个简单的代码可能会起作用(假设您正在处理 GUID 的字符串表示:
bool ValidateGuid(string theGuid)
{
try { Guid aG = new Guid(theGuid); }
catch { return false; }
return true;
}
回答by Nick Craver
If you're looking for a way to determine if it's the format of the actual .Net Guid
type, take a look at this article. A quick regex does the trick.
如果您正在寻找一种方法来确定它是否是实际 .NetGuid
类型的格式,请查看这篇文章。一个快速的正则表达式可以解决问题。
回答by Hans Passant
2^128 is a very, very large number. It is a billion times larger than the number of picoseconds in the life of the universe. Too large by a long shot to ever validate, the answer is doomed to be "42". Which is the point of using them: you don't have to. If you worry about getting duplicates then you worry for the wrong reason. The odds your machine will be destroyed by a meteor impact are considerably larger.
2^128 是一个非常非常大的数字。它比宇宙生命中的皮秒数大十亿倍。远太大而无法验证,答案注定是“42”。这就是使用它们的重点:您不必这样做。如果您担心得到重复,那么您担心的原因是错误的。您的机器被流星撞击摧毁的几率要大得多。
Duck!
鸭子!
回答by PierrOz
回答by Wayne Bloss
Here's a non-Regex answer that should be pretty fast:
这是一个非正则表达式的答案,应该很快:
public static bool IsHex(this char c)
{
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}
public static bool IsGuid(this string s)
{
// Length of a proper GUID, without any surrounding braces.
const int len_without_braces = 36;
// Delimiter for GUID data parts.
const char delim = '-';
// Delimiter positions.
const int d_0 = 8;
const int d_1 = 13;
const int d_2 = 18;
const int d_3 = 23;
// Before Delimiter positions.
const int bd_0 = 7;
const int bd_1 = 12;
const int bd_2 = 17;
const int bd_3 = 22;
if (s == null)
return false;
if (s.Length != len_without_braces)
return false;
if (s[d_0] != delim ||
s[d_1] != delim ||
s[d_2] != delim ||
s[d_3] != delim)
return false;
for (int i = 0;
i < s.Length;
i = i + (i == bd_0 ||
i == bd_1 ||
i == bd_2 ||
i == bd_3
? 2 : 1))
{
if (!IsHex(s[i])) return false;
}
return true;
}
回答by Damian Green
In .net 4, you can use this extension method
在 .net 4 中,你可以使用这个扩展方法
public static class GuidExtensions
{
public static bool IsGuid(this string value)
{
Guid output;
return Guid.TryParse(value, out output);
}
}
回答by Chandu Ranwala
i wrote a extension for this
我为此写了一个扩展
public static bool TryParseGuid(this Guid? guidString)
{
if (guidString != null && guidString != Guid.Empty)
{
if (Guid.TryParse(guidString.ToString(), out _))
{
return true;
}
else
return false;
}
return false;
}