Estoy tratando de convertir una cadena a su clase correspondiente (es decir, "verdadero" a true
). Y obtengo "TypeConverter no se puede convertir de System.String". El valor pasado es "verdadero".TypeConverter no se puede convertir de System.String
¿Estoy llamando al método de la manera incorrecta?
public static T ToClass<T>(this IDictionary<string, string> source) where T : class, new()
{
Type type = typeof(T);
T ret = new T();
foreach (var keyValue in source)
{
type.GetProperty(keyValue.Key).SetValue(ret, keyValue.Value.ToString().TestParse<T>(), null);
}
return ret;
}
public static T TestParse<T>(this string value)
{
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(value);
}
posible duplicado de: http://stackoverflow.com/questions/8625/generic-type-conversion-from-string – Adam