如何制作通用类型转换函数[重复]

时间:2021-11-02 16:14:50

Possible Duplicate:
is there a generic Parse() function that will convert a string to any type using parse?

可能重复:是否有一个通用的Parse()函数,它将使用parse将字符串转换为任何类型?

I want to make a generic function for doing some operations, like:

我想做一些通用函数来做一些操作,比如:

ConvertValue<T>(string value)

If T is int then the function will convert the value to int and return the result.

如果T为int,则函数将该值转换为int并返回结果。

Similarly, if T is boolean, the function will convert the value to boolean and return it.

类似地,如果T是布尔值,函数会将值转换为布尔值并返回它。

How to write this?

怎么写这个?

3 个解决方案

#1


156  

Something like this?

像这样的东西?

public static T ConvertValue<T>(string value)
{
    return (T)Convert.ChangeType(value, typeof(T));
}

You can then use it like this:

然后你可以像这样使用它:

int val = ConvertValue<int>("42");

Edit:

编辑:

You can even do this more generic and not rely on a string parameter provided the type U implements IConvertible - this means you have to specify two type parameters though:

如果类型U实现了IConvertible,你甚至可以做到更通用而不依赖于字符串参数 - 这意味着你必须指定两个类型参数:

public static T ConvertValue<T,U>(U value) where U : IConvertible
{
    return (T)Convert.ChangeType(value, typeof(T));
}

I considered catching the InvalidCastException exception that might be raised by Convert.ChangeType() - but what would you return in this case? default(T)? It seems more appropriate having the caller deal with the exception.

我考虑过捕获可能由Convert.ChangeType()引发的InvalidCastException异常 - 但在这种情况下你会返回什么?默认(T)?让调用者处理异常似乎更合适。

#2


6  

While probably not as clean looking as the IConvertible approach, you could always use the straightforward checking typeof(T) to return a T:

虽然可能不像IConvertible方法那样干净,但您总是可以使用简单的检查类型(T)来返回T:

public static T ReturnType<T>(string stringValue)
{
    if (typeof(T) == typeof(int))
        return (T)(object)1;
    else if (typeof(T) == typeof(FooBar))
        return (T)(object)new FooBar(stringValue);
    else
        return default(T);
}

public class FooBar
{
    public FooBar(string something)
    {}
}

#3


-2  

ConvertValue( System.Object o ), then you can branch out by o.GetType() result and up-cast o to the types to work with the value.

ConvertValue(System.Object o),然后你可以通过o.GetType()结果分支出来并向上转换o类型来处理这个值。

#1


156  

Something like this?

像这样的东西?

public static T ConvertValue<T>(string value)
{
    return (T)Convert.ChangeType(value, typeof(T));
}

You can then use it like this:

然后你可以像这样使用它:

int val = ConvertValue<int>("42");

Edit:

编辑:

You can even do this more generic and not rely on a string parameter provided the type U implements IConvertible - this means you have to specify two type parameters though:

如果类型U实现了IConvertible,你甚至可以做到更通用而不依赖于字符串参数 - 这意味着你必须指定两个类型参数:

public static T ConvertValue<T,U>(U value) where U : IConvertible
{
    return (T)Convert.ChangeType(value, typeof(T));
}

I considered catching the InvalidCastException exception that might be raised by Convert.ChangeType() - but what would you return in this case? default(T)? It seems more appropriate having the caller deal with the exception.

我考虑过捕获可能由Convert.ChangeType()引发的InvalidCastException异常 - 但在这种情况下你会返回什么?默认(T)?让调用者处理异常似乎更合适。

#2


6  

While probably not as clean looking as the IConvertible approach, you could always use the straightforward checking typeof(T) to return a T:

虽然可能不像IConvertible方法那样干净,但您总是可以使用简单的检查类型(T)来返回T:

public static T ReturnType<T>(string stringValue)
{
    if (typeof(T) == typeof(int))
        return (T)(object)1;
    else if (typeof(T) == typeof(FooBar))
        return (T)(object)new FooBar(stringValue);
    else
        return default(T);
}

public class FooBar
{
    public FooBar(string something)
    {}
}

#3


-2  

ConvertValue( System.Object o ), then you can branch out by o.GetType() result and up-cast o to the types to work with the value.

ConvertValue(System.Object o),然后你可以通过o.GetType()结果分支出来并向上转换o类型来处理这个值。