创建Generic T参数

时间:2011-11-14 21:46:37

标签: c# generics syntax arguments

假设我有两种类似的方法:

public List<object> Do<T>(Stream stream)
{
    ...  does cool things
}

public List<object> Do(Type type, Stream stream)
{
    T = type // <- what should this be
    return Do<T>(Stream);
}

允许其按预期运行的代码是什么?

我想这个问题必须在这里复制一些东西,但我找不到我的google-fu。

1 个答案:

答案 0 :(得分:8)

如果你反过来这样做很容易:

public List<object> Do<T>(Stream stream)
{
    var type = typeof(T);
    Do(type, stream);
}

然后另一种方法将包含非重复的逻辑。

相关问题