我试图循环遍历命名空间中的所有接口,并将它们作为参数发送到如下所示的一般方法:
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,但找到了局部变量名”。如果我尝试
...
foreach(var interface in interfaces)
{
Method<interface.MakeGenericType()>(); // Still get compile error here!
}
}
我得到“不能将运算符‘<’应用于‘方法组’和‘System.Type’类型的操作数‘”,对如何解决这个问题有什么想法吗?
相似问题