C# 使用特定接口的构造函数参数查找所有类型?

C# 使用特定接口的构造函数参数查找所有类型?,c#,reflection,types,.net-assembly,C#,Reflection,Types,.net Assembly,我试图获取一个特定程序集中所有类型的列表,然后使用一个构造函数将其过滤到所有类型,该构造函数将特定类型作为参数 在本例中,我将查找所有包含ILoggerFactory的构造函数 // Get reference to the assembly under test. This is necessary because this code is not contained with the same assembly. Type type = typeof(Program); Assembly a

我试图获取一个特定程序集中所有类型的列表,然后使用一个构造函数将其过滤到所有类型,该构造函数将特定类型作为参数

在本例中,我将查找所有包含
ILoggerFactory
的构造函数

// Get reference to the assembly under test. This is necessary because this code is not contained with the same assembly.
Type type = typeof(Program);
Assembly assembly = type.Assembly;

// Find all types with a constuctor which have a parameter of type ILoggerFactory.
var types = assembly.GetTypes()
    .Where(t => t.GetConstructors()
        .Any(c => c.GetParameters()
            .Any(p => t.IsAssignableFrom(typeof(ILoggerFactory)))));
我有许多带有
ILoggerFactory
参数的类型,例如:

public class MyClass
{
    public MyClass(ILoggerFactory loggerFactory) { }
}

public class MyOtherClass
{
    public MyOtherClass(ILoggerFactory loggerFactory) { }
}

public interface ILoggerFactory { }

然而,我得到了一个空列表。我希望列表包含
MyClass
MyOtherClass
,等等。我的查询有什么错?

t.IsAssignableFrom
t
是您在这里寻找的构造函数类型。例如,它将是
MyClass
MyOtherClass
。你想写
p.ParameterType.IsAssignableFrom
把它写下来作为一个答案,我会把它标记为接受的@Rob