C# .NET 3.5 中有 GUID.TryParse() 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1688624/
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
Is there a GUID.TryParse() in .NET 3.5?
提问by Hyman Marchetti
UPDATE
更新
Guid.TryParse is available in .NET 4.0
Guid.TryParse 在 .NET 4.0 中可用
END UPDATE
结束更新
Obviously there is no public GUID.TryParse() in .NET CLR 2.0.
显然,.NET CLR 2.0 中没有公共 GUID.TryParse()。
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
所以,我正在研究正则表达式 [又名谷歌搜索找到一个],每次我找到一个时,评论部分都会有激烈的争论关于 RegEx A 不起作用,使用 RegEx B。然后有人会写 Regex C 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?
有谁知道为什么 .NET Framework 中没有公共 Guid.TryParse()?
Does anyone have a real Regular Expression that will work for all GUIDs?
有没有人有一个真正适用于所有 GUID 的正则表达式?
采纳答案by JaredPar
There is no Guid.TryParse in CLR 2.0 and earlier. It will be available starting with CLR 4.0 and Visual Studio 2010.
CLR 2.0 及更早版本中没有 Guid.TryParse。它将从 CLR 4.0 和 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
.
至于为什么没有。这些类型的问题通常很难正确回答。很可能是疏忽或时间限制问题。如果您在反射器中打开 mscorlib,您会看到实际上有一个名为 TryParse 的方法,Guid
但它是私有的。在某些情况下它也会抛出异常,所以它不是一个很好的等价物 say Int32.TryParse
。
回答by bdukes
In terms of why there isn't one, it's an oversight. There will be a Guid.TryParse
in .NET 4 (see BCL blog postfor details).
至于为什么没有,这是一种疏忽。Guid.TryParse
.NET 4中将有一个(有关详细信息,请参阅BCL 博客文章)。
回答by Darin Dimitrov
Guid.TryParseimplementation using regular expressions.
Guid.TryParse使用正则表达式实现。
回答by Yannick Compernol
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.
据我所知,.NET Framework 中目前没有 TryParse 功能。您将不得不求助于 RegEx 或 try-catch 选项。RegEx 不是我的菜,所以我相信其他人会发布答案。
Exceptions are expensive performance wise, so my vote goes to the RegEx option.
例外是昂贵的性能明智的,所以我投票给 RegEx 选项。
回答by jheddings
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}\}?$"
回答by ryber
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.
您可以编写自己的 TryParse 作为 Guid 的扩展方法。然后,当来自 MS 的“真正的”出现时,您就可以开始使用了,无需更改。
回答by Partha Choudhury
IsGuid implemented as extension method for string...
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);
}
回答by Olivier Jacot-Descombes
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:
这个TryParse
for Guids 的实现使用 try-catch 来捕获格式错误的 Guids。它作为扩展方法实现并且必须放置在静态类中:
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:
从 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.
}
UPDATE
更新
Since Visual Studio 2010 / .NET Framework 4.0, System.Guid
provides a TryParse
and a TryPareExact
method.
从 Visual Studio 2010 / .NET Framework 4.0 开始,System.Guid
提供了一个TryParse
和一个TryPareExact
方法。