c# - 无法在 C#.NET 中将字符串转换为 GUID

标签 c# string .net-3.5 casting guid

<分区>

为什么转换(到 System.Guid 类型)语句无效(try block 中的第二行)?

例如,假设我有一个值为“5DD52908-34FF-44F8-99B9-0038AFEFDB81”的字符串。我想将其转换为 GUID。这不可能吗?

    Guid ownerIdGuid = Guid.Empty;
    try
    {
        string ownerId = CallContextData.Current.Principal.Identity.UserId.ToString();
        ownerIdGuid = (Guid)ownerId;
    }
    catch
    {
        // Implement catch
    }

最佳答案

试试这个:

Guid ownerIdGuid = Guid.Empty;            
try
{
    string ownerId = CallContextData.Current.Principal.Identity.UserId.ToString();
    ownerIdGuid = new Guid(ownerId);
}
catch
{
    // implement catch 
}

关于c# - 无法在 C#.NET 中将字符串转换为 GUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7197790/

相关文章:

c# - 泛型:其中 T 是 TypeA 或 TypeB

Python:字符串计数内存错误

string - 显示二维字符串数组的索引作为它们的值。例如arr[2][1] = "21"和 arr[3][1] = "31"等等

c# - 使用linq to xml用单引号保存xml元素的属性值

c# - 将 BitmapImage 转换为灰度,并保留 alpha channel

c# - 检测 Windows 7 审核模式

c# - asp.net mvc 中内容文件夹的路径

c# - 模型绑定(bind) Web 表单 TryUpdateModel 不工作

php - 使用 PHP 确保字符串只包含数字、字母和下划线?

c# - 有没有办法将此枚举转换为正确的类型