编辑:
当然,我的真实代码看起来并不完全像这样.我试着编写半伪代码,以便更清楚地表达我想做的事情.
看起来它只是把事情搞砸了.
所以,我真正想做的是:
Method<Interface1>();
Method<Interface2>();
Method<Interface3>();
...
Run Code Online (Sandbox Code Playgroud)
嗯......我想也许我可以用反射把它变成一个循环.所以问题是:我该怎么做.我有很反射的浅识.所以代码示例会很棒.
场景如下所示:
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 i in interfaces)
{
Method<i>(); // Get compile error here!
}
Run Code Online (Sandbox Code Playgroud)
原帖:
嗨!
我正在尝试遍历命名空间中的所有接口,并将它们作为参数发送到这样的泛型方法:
public void Method<T>() where T : class
{}
public void AnotherMethod()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var interfaces = …
Run Code Online (Sandbox Code Playgroud)