31
我有以下的代碼工作:通用轉換功能似乎不使用的GUID
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,但我不斷收到錯誤:
Invalid cast from System.String to System.Guid
這與原始數據類型完美配合。
有什麼辦法可以讓使這項工作適用於非原始數據類型?
小心使用ConvertFromInvariantString。如果您的類型是日期時間,並且您有國際日期格式,則會爆炸。我根據這個答案追蹤了一個非常難以發現的錯誤。 – 2013-04-27 21:42:39