通用转换功能似乎不使用GUID来工作 [英] Generic conversion function doesn't seem to work with Guids

查看:168
本文介绍了通用转换功能似乎不使用GUID来工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

    public static T ParameterFetchValue<T>(string parameterKey)
    {
        Parameter result = null;

        result = ParameterRepository.FetchParameter(parameterKey);

        return (T)Convert.ChangeType(result.CurrentValue, typeof(T), CultureInfo.InvariantCulture);  
    }

result.CurrentValue类型为字符串。我希望能够将其转换为GUID,但我不断收到错误:

The type of result.CurrentValue is a string. I would like to be able to convert this to a Guid but I keep getting the error:

Invalid cast from System.String to System.Guid

这可以完美地与基本数据类型......有没有什么办法为,使这项工作对非原始数据类型

This works perfectly with primitive data types ... is there any way to make this work for non-primitive data types?

推荐答案

怎么样:

T t = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(text);

工作正常,的Guid 和大多数其他类型的。

Works fine for Guid and most other types.

这篇关于通用转换功能似乎不使用GUID来工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆