【问题标题】:Unable to get the type of an interface/class using more than one generic type?无法使用多个泛型类型获取接口/类的类型?
【发布时间】:2013-02-07 10:47:13
【问题描述】:

给出下面的示例代码,谁能解释为什么第一个typeof() 调用成功但第二个失败?不管它们是类还是接口,它都会失败。

interface ITestOne<T1>
{
   T1 MyMethod();
}

interface ITestMany<T1, T2>
{
   T1 MyMethod(T2 myParameter);
}

void Main()
{
    var typeOne = typeof(ITestOne<>); //This line works
    var typeTwo = typeof(ITestMany<>); //Compile error
}

【问题讨论】:

    标签: c# generics compiler-errors typeof


    【解决方案1】:

    您需要让编译器知道您正在寻找具有两个泛型参数的泛型类型。在尖括号之间添加逗号:

    var typeTwo = typeof(ITestMany<,>);
    

    【讨论】:

    • 卫生署!好的,这是我一天中的荷马辛普森时刻。非常感谢您的快速回复!! 15 分钟强制窗口关闭后,我会将其标记为答案。
    • 别担心 :) 我们都有这样的时刻
    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 2019-07-21
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多