Is there a GUID.TryParse() in .NET 3.5?

2019-02-16 03:53发布

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?

7条回答
做个烂人
2楼-- · 2019-02-16 04:13

Guid.TryParse implementation using regular expressions.

查看更多
孤傲高冷的网名
3楼-- · 2019-02-16 04:15

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}\}?$"
查看更多
Fickle 薄情
4楼-- · 2019-02-16 04:16

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.

查看更多
【Aperson】
5楼-- · 2019-02-16 04:18

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 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.
}

UPDATE

Since Visual Studio 2010 / .NET Framework 4.0, System.Guid provides a TryParse and a TryPareExact method.

查看更多
一夜七次
6楼-- · 2019-02-16 04:24

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.

查看更多
三岁会撩人
7楼-- · 2019-02-16 04:25

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).

查看更多
登录 后发表回答