简体   繁体   English

获取具体 class 实现的特定接口的类型列表

[英]Get list of type(s) of specific interface implemented by concrete class

Given a class, how can i find the types for the INotificationHandler<>:给定一个 class,我如何找到 INotificationHandler<> 的类型:

public class OtherClass : INotificationHandler<Aggregate>, INotificationHandler<Quote>
{
  /* */
}

var typesList = [] { typeof(OtherClass) };

var result = MagicFunctionToGetTemplateTypesForNotificationHandlerInterface(typesList)

// where result = [] { typeof(Aggregate), typeof(Quote) };

I am considering going down the road of GetType().GenericTypeArguments[0] however want to check if there is a safer way first.我正在考虑走GetType().GenericTypeArguments[0]的道路,但是想先检查是否有更安全的方法。

I have tried searching and appreciate this could very well be a duplicate, if so please let me know and I will delete.我已经尝试搜索并欣赏这很可能是重复的,如果是这样,请告诉我,我将删除。

You could try something like this:你可以尝试这样的事情:

var result = typeof(OtherClass).GetInterfaces()
    .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(INotificationHandler<>))
    .Select(x => x.GetGenericArguments()[0])
    .ToArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [抽象类] 类型的接口属性可以在 C# 中实现为具体类吗? - Can an interface property of type [abstract class] be implemented as a concrete class in C#? 泛型参数-使用具体类型进行编译,但是实现的接口不会编译 - Generic parameter - using a concrete type compiles, but an implemented interface does not 为什么不能使用持有实现接口的具体类型的属性来实现持有接口类型的接口属性? - Why can interface properties holding an interface type not be implemented with a property holding a concrete type that implements the interface? 如何从具体类转换为接口类型? - How to cast from a concrete class to an interface type? 如何通过泛型类型获得接口的具体实现? - How to get the concrete implementation of an interface by the generic type? 从接口序列化通用类时,如何让DataContractJsonSerializer在类型提示中使用具体类型 - How do I get DataContractJsonSerializer to use concrete type in type hint when serializing generic class from interface 获取单个指定具体类型的特定接口,并使用 Simple Injector 应用装饰器 - Get specific interface for a single specified concrete type, with decorators applied with Simple Injector 返回实现类型的类的列表 - Return List of type implemented class 具有接口列表的接口,如何选择由接口实现的一种类型 - Interface with a list of interface, how to choose one type implemented by interface 将界面投射到混凝土类型 - Cast interface to concrete type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM