2010-03-03 149 views
10

請告訴我如何驗證.net中的GUID,它始終是唯一的?如何驗證.net中的Guid

+1

的GUID總是唯一如果所有的生成器都遵循以下規則:http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx – 2014-05-18 10:28:01

回答

28

Guid's是獨一無二的99.9999999999999999999999999999999999%的時間。

這取決於你的意思是驗證嗎?

代碼,以確定一個GUID字符串,其實是一個GUID,是as follows

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; 
} 
+0

+1爲了避免使用異常,-1因爲'Guid '可以有其他格式,目前的方法會引發以下情況的例外:「{230a0f8b-81fb-4052-866e-9 ac6a7611c77「 – 2010-03-03 11:43:25

+5

+1,在.NET 4.0中,我們終於可以看到'Guid.TryParse()'的引入,這顯然比使用正則表達式更可取。儘管如此,這是完全可以接受的。 http://connect.microsoft.com/VisualStudio/feedback/details/94072/guid-tryparse – 2010-03-03 11:43:32

+0

Guid's不是隨機的,也許有一種方法可以檢查它們是否符合以下條件:http://blogs.msdn.com /b/oldnewthing/archive/2008/06/27/8659071.aspx – 2014-05-18 10:27:22

1

您無法驗證GUID的唯一性。你只希望它是用一個產生唯一的16字節的工具生成的。至於驗證,這個簡單的代碼可能工作(假設你正在處理的GUID的字符串表示:

bool ValidateGuid(string theGuid) 
{ 
    try { Guid aG = new Guid(theGuid); } 
    catch { return false; } 

    return true; 
} 
+1

這就是簡單的出路。請記住,例外情況應該用於特殊情況,而不是用於驗證類型。 – 2010-03-03 11:26:53

+3

@Kyle Rozendo:是的,但使用正則表達式,你幾乎限制了GUID字符串表示的格式。例如,在您的代碼中,格式僅爲WITH DASHES。 – Kerido 2010-03-03 11:33:19

+0

是的,當編寫大量代碼來做同樣的事情會更好時,我們絕不會想要「簡單的出路」......如果這是一個單元測試呢?那麼它會被接受嗎? – iGanja 2013-11-05 00:16:38

0

如果你正在尋找一種方式來確定它是否是一個實際的淨Guid類型,take a look at this article的格式。一個快速的正則表達式就可以做到這一點:

11

2^128是一個非常非常大的數字,它比宇宙生命中的皮秒數大十億倍,通過一個長鏡頭來驗證太大,答案註定是「42」,這是使用它們的關鍵:你不必如果你擔心重複出現,你擔心錯誤的原因,你的機器將被流星imp破壞行爲相當大。

鴨子!

0

這個問題已經在this post中討論過了。您可能會發現更多有趣的細節

2

這裏有一個非正則表達式的答案應該是非常快:

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; 
} 
0

在.NET 4中,你可以使用這個擴展方法

public static class GuidExtensions 
    { 
     public static bool IsGuid(this string value) 
     { 
      Guid output; 
      return Guid.TryParse(value, out output); 
     } 
    }