首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用仅在执行时已知的类型参数调用泛型方法是什么?

使用仅在执行时已知的类型参数调用泛型方法是什么?

提问于 2018-03-02 06:01:06
回答 1关注 0查看 154

我试图循环遍历命名空间中的所有接口,并将它们作为参数发送到如下所示的一般方法:

代码语言:txt
复制
public void Method<T>() where T : class
{}
public void AnotherMethod()
{
    Assembly assembly = Assembly.GetExecutingAssembly();

    var interfaces = from i in assembly.GetTypes()
    where i.Namespace == "MyNamespace.Interface" // only interfaces stored here
    select i;

    foreach(var interface in interfaces)
    {
        Method<interface>(); // Get compile error here!
    }
}

我得到的错误是“Typename,但找到了局部变量名”。如果我尝试

代码语言:txt
复制
...
    foreach(var interface in interfaces)
    {
        Method<interface.MakeGenericType()>(); // Still get compile error here!
    }
}

我得到“不能将运算符‘<’应用于‘方法组’和‘System.Type’类型的操作数‘”,对如何解决这个问题有什么想法吗?

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档