.NET 3.5 中有 GUID.TryParse() 吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
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 sayInt32.TryParse
.Guid.TryParse 使用正则表达式实现。
Guid.TryParse implementation using regular expressions.
IsGuid 实现为字符串的扩展方法...
IsGuid implemented as extension method for string...
仅适用于 .NET Framework 3.5 或更早版本:
Guids 的
TryParse
实现使用 try-catch 来捕获格式错误的 Guid。它作为扩展方法实现,必须放置在静态类中:它可以
通过从 C# 7.0 / Visual Studio 2017 开始调用,您可以使用内联输出变量声明并使用以下方式调用它:
由于 .NET Framework 4.0 中,
System.Guid
提供了TryParse
和TryPareExact
方法,使得上面的代码片段变得过时。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:It can be called with
Starting with C# 7.0 / Visual Studio 2017, you can use an inline out-variable declaration and call it with:
Since .NET Framework 4.0,
System.Guid
providesTryParse
andTryPareExact
methods making the code snippets above obsolete.至于为什么没有,这是一种疏忽。 .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).据我所知,目前 .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.
这应该有效:
This should work:
您可以编写自己的 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.