反射以找到 类 实现具有多个开放通用参数的接口
Reflection to find classes that implement interface with multiple open generic parameters
假设我有一个这样的界面:
public interface ITest<T1, T2>
{
}
还有一个 class 这样的:
public class Concrete : ITest<int, string>
{
}
使用反射,我怎样才能找到这个具体的class?我已经尝试了以下但它不起作用,因为我没有指定泛型类型参数。
var concretes = Assembly.GetAssembly(typeof(Concrete)).GetTypes()
.Where(x => x.IsAssignableFrom(typeof(ITest<,>))
&& !x.IsInterface);
这 returns 零项。甚至可以做我想做的事吗?
试试这个:
var concretes =
Assembly
.GetAssembly(typeof (Concrete))
.GetTypes()
.Where(t =>
t.GetInterfaces()
.Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ITest<,>)))
.ToList();
假设我有一个这样的界面:
public interface ITest<T1, T2>
{
}
还有一个 class 这样的:
public class Concrete : ITest<int, string>
{
}
使用反射,我怎样才能找到这个具体的class?我已经尝试了以下但它不起作用,因为我没有指定泛型类型参数。
var concretes = Assembly.GetAssembly(typeof(Concrete)).GetTypes()
.Where(x => x.IsAssignableFrom(typeof(ITest<,>))
&& !x.IsInterface);
这 returns 零项。甚至可以做我想做的事吗?
试试这个:
var concretes =
Assembly
.GetAssembly(typeof (Concrete))
.GetTypes()
.Where(t =>
t.GetInterfaces()
.Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ITest<,>)))
.ToList();