如何获得的所有类型在参考文献中实现IMyInterface的 [英] How to get all types in the references that implement 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.
我该怎么办呢?
推荐答案
我想这个问题可能是你的一些引用的程序目前尚未加载。你可以得到所有引用的程序与<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getreferencedassemblies.aspx"相对=nofollow> 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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!