C# 如何尝试将字符串转换为 Guid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/350500/
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
How to try convert a string to a Guid
提问by DanH
I did not find the TryParse method for the Guid. I'm wondering how others handle converting a guid in string format into a guid type.
我没有找到 Guid 的 TryParse 方法。我想知道其他人如何将字符串格式的 guid 转换为 guid 类型。
Guid Id;
try
{
Id = new Guid(Request.QueryString["id"]);
}
catch
{
Id = Guid.Empty;
}
回答by leppie
new Guid(string)
You could also look at using a TypeConverter
.
您也可以考虑使用TypeConverter
.
回答by Joseph Ferris
Unfortunately, there isn't a TryParse() equivalent. If you create a new instance of a System.Guid and pass the string value in, you can catch the three possible exceptions it would throw if it is invalid.
不幸的是,没有与 TryParse() 等效的方法。如果创建 System.Guid 的新实例并传入字符串值,则可以捕获它在无效时抛出的三个可能的异常。
Those are:
那些是:
- ArgumentNullException
- FormatException
- OverflowException
- 参数空异常
- 格式异常
- 溢出异常
I have seen some implementations where you can do a regex on the string prior to creating the instance, if you are just trying to validate it and not create it.
我已经看到一些实现,如果您只是尝试验证它而不是创建它,那么您可以在创建实例之前对字符串执行正则表达式。
回答by Brian Rudolph
This will get you pretty close, and I use it in production and have never had a collision. However, if you look at the constructor for a guid in reflector, you will see all of the checks it makes.
这会让你非常接近,我在生产中使用它并且从未发生过碰撞。但是,如果您查看反射器中 guid 的构造函数,您将看到它所做的所有检查。
public static bool GuidTryParse(string s, out Guid result)
{
if (!String.IsNullOrEmpty(s) && guidRegEx.IsMatch(s))
{
result = new Guid(s);
return true;
}
result = default(Guid);
return false;
}
static Regex guidRegEx = new Regex("^[A-Fa-f0-9]{32}$|" +
"^({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$|" +
"^({)?[0xA-Fa-f0-9]{3,10}(, {0,1}[0xA-Fa-f0-9]{3,6}){2}, {0,1}({)([0xA-Fa-f0-9]{3,4}, {0,1}){7}[0xA-Fa-f0-9]{3,4}(}})$", RegexOptions.Compiled);
回答by Brian Balamut
If all you want is some very basic error checking, you could just check the length of the string.
如果你想要的只是一些非常基本的错误检查,你可以只检查字符串的长度。
string guidStr = "";
if( guidStr.Length == Guid.Empty.ToString().Length )
Guid g = new Guid( guidStr );
回答by juFo
Guid.TryParse()
https://msdn.microsoft.com/de-de/library/system.guid.tryparse(v=vs.110).aspx
https://msdn.microsoft.com/de-de/library/system.guid.tryparse(v=vs.110).aspx
or
或者
Guid.TryParseExact()
https://msdn.microsoft.com/de-de/library/system.guid.tryparseexact(v=vs.110).aspx
https://msdn.microsoft.com/de-de/library/system.guid.tryparseexact(v=vs.110).aspx
in .NET 4.0 (or 3.5?)
在 .NET 4.0(或 3.5?)
回答by Behrooz
use code like this:
使用这样的代码:
new Guid("9D2B0228-4D0D-4C23-8B49-01A698857709")
instead of "9D2B0228-4D0D-4C23-8B49-01A698857709" you can set your string value
您可以设置字符串值而不是“9D2B0228-4D0D-4C23-8B49-01A698857709”