.NET 3.5 中有 GUID.TryParse() 吗?

发布于 2024-08-10 11:38:28 字数 655 浏览 12 评论 0原文

UPDATE

Guid.TryParse 在 .NET 4.0 中可用

END UPDATE

显然,.NET CLR 2.0 中没有公共 GUID.TryParse()。

所以,我正在研究正则表达式[又名谷歌搜索寻找一个],每次我发现一个正则表达式时,评论部分都会激烈争论正则表达式 A 不起作用,请使用正则表达式 B。然后有人会写正则表达式 C yadda yadda

所以无论如何,我决定这样做,但我对此感到难过。

public static bool IsGuid (string possibleGuid) {

    try {
      Guid gid = new Guid(possibleGuid);
      return true;    
    } catch (Exception ex) {
      return false;
    }
}

显然我不太喜欢这个,因为从第一天起它就被灌输给我,以避免抛出异常(如果你可以防御性地围绕它编写代码)。

有谁知道为什么 .NET Framework 中没有 public Guid.TryParse() ?

有人有一个适用于所有 GUID 的真正的正则表达式吗?

UPDATE

Guid.TryParse is available in .NET 4.0

END UPDATE

Obviously there is no public GUID.TryParse() in .NET CLR 2.0.

So, I was looking into regular expressions [aka googling around to find one] and each time I found one there was a heated argument in the comments section about RegEx A doesn't work, use RegEx B. Then someone would write Regex C yadda yadda

So anyway, What I decided to do was this, but I feel bad about it.

public static bool IsGuid (string possibleGuid) {

    try {
      Guid gid = new Guid(possibleGuid);
      return true;    
    } catch (Exception ex) {
      return false;
    }
}

Obviously I don't really like this since it's been drilled into me since day one to avoid throwing exceptions if you can defensibly code around it.

Does anyone know why there is no public Guid.TryParse() in the .NET Framework?

Does anyone have a real Regular Expression that will work for all GUIDs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

活雷疯 2024-08-17 11:38:28

CLR 2.0 及更早版本中没有 Guid.TryParse。它将从 CLR 4.0 和 Visual Studio 2010 开始提供。

至于为什么没有。这类问题通常很难正确回答。这很可能是疏忽或时间限制问题。如果您在反射器中打开 mscorlib,您会看到 Guid 上实际上有一个名为 TryParse 的方法,但它是私有的。在某些情况下它还会引发异常,因此它不能很好地等同于 Int32.TryParse

There is no Guid.TryParse in CLR 2.0 and earlier. It will be available starting with CLR 4.0 and Visual Studio 2010.

As to why there wasn't. These types of questions are usually hard to answer correctly. Most likely it was an oversight or a time constraint issue. If you open up mscorlib in reflector you'll see there is actually a method named TryParse on Guid but it's private. It also throws an exception in certain cases so it's not a good equivalent to say Int32.TryParse.

倾城月光淡如水﹏ 2024-08-17 11:38:28

Guid.TryParse 使用正则表达式实现。

Guid.TryParse implementation using regular expressions.

┊风居住的梦幻卍 2024-08-17 11:38:28

IsGuid 实现为字符串的扩展方法...

public static bool IsGuid(this string stringValue)
{
   string guidPattern = @"[a-fA-F0-9]{8}(\-[a-fA-F0-9]{4}){3}\-[a-fA-F0-9]{12}";
   if(string.IsNullOrEmpty(stringValue))
     return false;
   Regex guidRegEx = new Regex(guidPattern);
   return guidRegEx.IsMatch(stringValue);
}

IsGuid implemented as extension method for string...

public static bool IsGuid(this string stringValue)
{
   string guidPattern = @"[a-fA-F0-9]{8}(\-[a-fA-F0-9]{4}){3}\-[a-fA-F0-9]{12}";
   if(string.IsNullOrEmpty(stringValue))
     return false;
   Regex guidRegEx = new Regex(guidPattern);
   return guidRegEx.IsMatch(stringValue);
}
面犯桃花 2024-08-17 11:38:28

仅适用于 .NET Framework 3.5 或更早版本:

Guids 的 TryParse 实现使用 try-catch 来捕获格式错误的 Guid。它作为扩展方法实现,必须放置在静态类中:

public static bool TryParseGuid(this string s, out Guid guid)
{
    try {
        guid = new Guid(s);
        return true;
    } catch {
        guid = Guid.Empty;
        return false;
    }
}

它可以

string s = "{CA761232-ED42-11CE-BACD-00AA0057B223}";
Guid id;
if (s.TryParseGuid(out id)) {
    // TODO: use id
} else {
    // Sorry not a valid Guid.
}

通过从 C# 7.0 / Visual Studio 2017 开始调用,您可以使用内联输出变量声明并使用以下方式调用它:

string s = "{CA761232-ED42-11CE-BACD-00AA0057B223}";
if (s.TryParseGuid(out Guid id)) {
    // TODO: use id
} else {
    // Sorry not a valid Guid.
}

由于 .NET Framework 4.0 中,System.Guid 提供了 TryParseTryPareExact 方法,使得上面的代码片段变得过时。

Only for .NET Framework 3.5 or earlier:

This implementation of a TryParse for Guids uses a try-catch in order to catch missformed Guids. It is implemented as extension method und must be placed in a static class:

public static bool TryParseGuid(this string s, out Guid guid)
{
    try {
        guid = new Guid(s);
        return true;
    } catch {
        guid = Guid.Empty;
        return false;
    }
}

It can be called with

string s = "{CA761232-ED42-11CE-BACD-00AA0057B223}";
Guid id;
if (s.TryParseGuid(out id)) {
    // TODO: use id
} else {
    // Sorry not a valid Guid.
}

Starting with C# 7.0 / Visual Studio 2017, you can use an inline out-variable declaration and call it with:

string s = "{CA761232-ED42-11CE-BACD-00AA0057B223}";
if (s.TryParseGuid(out Guid id)) {
    // TODO: use id
} else {
    // Sorry not a valid Guid.
}

Since .NET Framework 4.0, System.Guid provides TryParse and TryPareExact methods making the code snippets above obsolete.

美煞众生 2024-08-17 11:38:28

至于为什么没有,这是一种疏忽。 .NET 4 中将有一个 Guid.TryParse (请参阅 BCL 博客文章 了解详细信息)。

In terms of why there isn't one, it's an oversight. There will be a Guid.TryParse in .NET 4 (see BCL blog post for details).

深陷 2024-08-17 11:38:28

据我所知,目前 .NET Framework 中没有 TryParse 功能。您必须求助于 RegEx 或 try-catch 选项。正则表达式不是我的菜,所以我确信其他人会发布答案。

异常在性能方面是昂贵的,所以我投票给 RegEx 选项。

There's no TryParse functionality in the .NET Framework to my knowledge at this moment. You'll have to resort to RegEx or the try-catch option. RegEx isn't my cup of tea, so I'm sure someone else will post an answer.

Exceptions are expensive performance wise, so my vote goes to the RegEx option.

挥剑断情 2024-08-17 11:38:28

这应该有效:

@"^\{?[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}\}?$"

This should work:

@"^\{?[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}\}?$"
_失温 2024-08-17 11:38:28

您可以编写自己的 TryParse 作为 Guid 的扩展方法。然后,当 MS 的“真正”产品出现时,您就可以开始使用了,无需进行任何更改。

You can write your own TryParse as a extension method for Guid. Then when the 'real' one from MS shows up, your already good to go and don't have to change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文