tags:

views:

143

answers:

2

I have the following 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);  
    }

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?

+3  A: 

How about:

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

Works fine for Guid and most other types.

Marc Gravell
A: 

Hi,

This seems to be unwoking with GUID for me when i'm trying to convert the GUID into string. Do you have any clue about? I get the following error: **"StringConverter cannot convert from System.Guid"**

thanks for your help.

Tamir