如何获得的所有类型在参考文献中实现IMyInterface的

编程入门 行业动态 更新时间:2024-10-28 14:30:20
本文介绍了如何获得的所有类型在参考文献中实现IMyInterface的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个项目中多次提到。 我需要找到所有实现IMyInterface的接口类型。

I have a project contains many references. I need to find all the types that implement IMyInterface interface.

我试过 AppDomain.CurrentDomain.GetAssemblies()的SelectMany(X => x.GetTypes())。,但它并没有在参考文献中返回的所有类型。

I tried AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()) but it didn't returned all the types in the references.

我该怎么办呢?

推荐答案

我想这个问题可能是你的一些引用的程序目前尚未加载。你可以得到所有引用的程序与 GetReferencedAssemblies - 但这只会产生名字

I guess the problem might be that some of your referenced assemblies are not currently loaded. You can get all referenced assemblies with GetReferencedAssemblies - but this will only yield the names.

如果你愿意,你可以去和加载组件与的Assembly.Load ,并进一步对其进行检查。

If you want you can go on and load the assemblies with Assembly.Load and inspect them further.

因此​​,一个可能的片段应该是

So a possible snippet should be

var types = System.Reflection.Assembly.GetExecutingAssembly() .GetReferencedAssemblies() .SelectMany(name => Assembly.Load(name).GetTypes()) .Union(AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()));

寻找实现你的接口类型:

to search for the types implementing your interface:

var withInterfaces = types.Where(t => t.GetInterfaces().Any(i => i == typeof(IDisposable)));

如果这不招我丢失了...

If this does not the trick I'm lost as well...

更多推荐

如何获得的所有类型在参考文献中实现IMyInterface的

本文发布于:2023-10-09 10:43:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参考文献   如何获得   类型   IMyInterface

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!