繁体   English   中英

通用方法返回类型作为类型参数

[英]Generic Method Return Type as Type parameter

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

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参数吗?

我错过了什么?

谢谢

这是你想要的? 我已经为演员表无效的情况添加了额外的收益

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);

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

    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>()

我正在使用它进行通用转换:

    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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM