查找实现具有特定T类型的某个通用接口的所有类型 [英] Find all types implementing a certain generic interface with specific T type

查看:93
本文介绍了查找实现具有特定T类型的某个通用接口的所有类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个继承自抽象类BrowsingGoal 的类。其中一些实现了一个名为 ICanHandleIntent< TPageIntent>的接口。其中TPageIntent:PageIntent



给出一个具体的例子:

  public class Authenticate:BrowsingGoal ,ICanHandleIntent< AuthenticationNeededIntent> 
{
...
}

现在我想使用 AuthenticationNeededIntent 扫描实现 ICanHandleIntent 的所有类型的CurrentDomain程序集。这是我迄今为止的,但它似乎没有发现任何东西:

 受保护的BrowsingGoal FindNextGoal(PageIntent意图)
{
//找到实现ICanHandleIntent< specific PageIntent>的目标
var goalHandler = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.IsAssignableFrom (typeof(BrowsingGoal)))&&
t.GetInterfaces()。Any(x =>
x.IsGenericType&&
x.IsAssignableFrom(typeof(ICanHandleIntent< >))&&
x.GetGenericTypeDefinition()== intent.GetType()));

if(goalHandler!= null)
返回Activators.CreateInstance(goalHandler)作为BrowsingGoal;
}

一些帮助将非常值得赞赏!


<这个条件是不正确的:

  x.IsAssignableFrom(typeof (ICanHandleIntent<>))

实现通用接口实例的类型不能从通用接口定义本身,其中 ICanHandleIntent<> 表示。



您需要的是

  x.GetGenericTypeDefinition()== typeof(ICanHandleIntent<>)

检查类型参数也是错误的。它应该是

  x.GetGenericArguments()[0] == intent.GetType()

$ b $ p $因为你正在寻找类型参数,即在通用名称后面的三角括号中的类型。


I have several classes inheriting from abstract class BrowsingGoal. Some of these implement an interface called ICanHandleIntent<TPageIntent> where TPageIntent: PageIntent.

To give a concrete example:

public class Authenticate : BrowsingGoal, ICanHandleIntent<AuthenticationNeededIntent>
{
    ...
}

Now I would like to scan the CurrentDomain's assemblies for all types implementing the ICanHandleIntent with the AuthenticationNeededIntent. This is what I have so far but it doesn't seem to find anything:

protected BrowsingGoal FindNextGoal(PageIntent intent)
{
    // Find a goal that implements an ICanHandleIntent<specific PageIntent>
    var goalHandler = AppDomain.CurrentDomain
        .GetAssemblies()
        .SelectMany(assembly => assembly.GetTypes())
        .FirstOrDefault(t => t.IsAssignableFrom((typeof (BrowsingGoal))) &&
                                t.GetInterfaces().Any(x =>
                                    x.IsGenericType &&
                                    x.IsAssignableFrom(typeof (ICanHandleIntent<>)) &&
                                    x.GetGenericTypeDefinition() == intent.GetType()));

    if (goalHandler != null)
        return Activator.CreateInstance(goalHandler) as BrowsingGoal;
}

Some help would be very appreciated!

解决方案

This condition is incorrect:

x.IsAssignableFrom(typeof(ICanHandleIntent<>))

a type implementing an instance of a generic interface is not assignable from the generic interface definition itself, which ICanHandleIntent<> represents.

What you want instead is

x.GetGenericTypeDefinition() == typeof(ICanHandleIntent<>)

The check for the type parameter is also wrong. It should be

x.GetGenericArguments()[0] == intent.GetType()

because you are looking for the type argument, i.e. the type in triangular brackets following the generic name.

这篇关于查找实现具有特定T类型的某个通用接口的所有类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆