11

我有一个扩展方法可以正常地将字符串值转换为各种类型,它看起来像这样:

public static T ToType<T> (this string value, T property)
    {
        object parsedValue = default(T);
        Type type = property.GetType();

        try
        {
            parsedValue = Convert.ChangeType(value, type);
        }
        catch (ArgumentException e)
        {
            parsedValue = null;
        }

        return (T)parsedValue;
    }

但是,我对调用该方法时的外观不满意:

myObject.someProperty = stringData.ToType(myObject.someProperty);

仅仅为了获取属性的类型而指定属性似乎是多余的。我宁愿使用这样的签名:

public static T ToType<T> (this string value, Type type) { ... }

并让 T 最终成为类型的类型。这将使调用更清晰:

myObject.someProperty = stringData.ToType(typeof(decimal));

但是,当我尝试以这种方式调用时,编辑器抱怨无法从使用情况中推断出扩展方法的返回类型。我可以将 T 链接到 Type 参数吗?

我错过了什么?

谢谢

4

3 回答 3

17

这是你想要的?对于演员表无效的情况,我添加了一个额外的捕获

Decimal i = stringName.ToType<Decimal>();

public static T ToType<T>(this string value)
{
     object parsedValue = default(T);
     try
     {
         parsedValue = Convert.ChangeType(value, typeof(T));
     }
     catch (InvalidCastException)
     {
         parsedValue = null;
     }
     catch (ArgumentException)
     {
         parsedValue = null;
     }
     return (T)parsedValue;
} 

编辑

修复安东评论的捷径

if (typeof(T).IsValueType)
   return default(T);
于 2013-07-23T17:58:00.290 回答
2

为什么要使用财产?只需更改将类型变量设置为泛型类型的方式即可。

    public static T ToType<T>(this string value)
    {
        object parsedValue = default(T);
        Type type = typeof(T);

        try
        {
            parsedValue = Convert.ChangeType(value, type);
        }
        catch (ArgumentException e)
        {
            parsedValue = null;
        }

        return (T) parsedValue;
    }

用法:

myObject.someProperty = stringData.ToType<decimal>()
于 2013-07-23T17:59:11.403 回答
0

我将其用于通用转换:

    public bool ConvertTo<T>(object from, out T to) {
        to = default(T);
        if (from is T) { to = (T)from; return true; }                                           
        Type t = typeof(T);
        //TypeConverter converter = p.converter == null ? TypeDescriptor.GetConverter(t) : p.converter;
        TypeConverter converter = TypeDescriptor.GetConverter(t);
        if ((converter != null) && (converter.CanConvertTo(t))) {
            try { to = (T)converter.ConvertTo(null, culture, from, t); return true; }
            catch { }
        }
        try { to = (T)Convert.ChangeType(from, t, culture); return true; }
        catch { }
        return false;                                                                                       
    }

    public bool ConvertTo(object from, out object to, Type type) {
        to = null;
        if (from.GetType() == type) { to = from; return true; }     
        TypeConverter converter = TypeDescriptor.GetConverter(type);
        if ((converter != null) && (converter.CanConvertTo(type))) {
            try { to = converter.ConvertTo(null, culture, from, type); return true; }
            catch { }
        }
        try { to = Convert.ChangeType(from, type, culture); return true; }
        catch { }
        return false;                                           
    }

在调用 之前,它会检查给定变量Convert.ChangeType是否存在。TypeConverter

这样称呼它:

int i = 123;
string s;
if (ConvertTo<string>(i, out s) {
    // use s
}
于 2013-07-23T18:02:37.410 回答