[英]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.