将T类更改为另一个T.

时间:2017-10-31 11:02:13

标签: c# generics

如何在不必实现IConvertable接口的情况下将T转换为另一个T.

我想将曾经是int的类的T改为double,我不知道如何通过反射来做到这一点

public class TestClass<T>
{
    private T value_;
    public TestClass(T val)
    {
        value_ = val;
    }


    public T Compute()
    {
        return value_;
    }
}


 static void Main(string[] args)
    {

        var tClass = new TestClass<int>(5);
        var value = tClass.Compute();
        //now make compute return a double without doing a (double)tClass.Compute()
        //i want to change the T of the class which used to be an int to a double, i'm not sure how to do this through reflection
        Console.ReadLine();
    }

0 个答案:

没有答案