简体   繁体   English

如何使用CoreClr获取加载的程序集中的所有类型?

[英]How do I get all types in loaded Assemblies with CoreClr?

In my application I need to get a list of all types loaded regardless of what assembly they are contained in. For example, Project A might need to get a list of all types that implement interface A even if that implementation exists in project B. 在我的应用程序中,我需要获取所有类型的列表,无论它们包含在哪个程序集中。例如,项目A可能需要获得实现接口A的所有类型的列表,即使项目B中存在该实现。

In the .Net Framework I would do this by querying for all assemblies in the current app domain. 在.Net Framework中,我会通过查询当前应用程序域中的所有程序集来完成此操作。

Since it seems like the concept of an app domain is gone in CoreCLR, what is the proper way to accomplish this? 由于看起来应用领域的概念在CoreCLR中消失了,实现这一目标的正确方法是什么?

When run under DNX in RC1-Update1 (note flakey behavior when run under dotnet.exe directly): 在RC1-Update1中在DNX下运行时(注意直接在dotnet.exe下运行时的flakey行为):

var assemblies = PlatformServices.Default.LibraryManager.GetLibraries().SelectMany(l => l.Assemblies.Select(an =>
{
try
{ return Assembly.Load(an); }
catch (ReflectionTypeLoadException)
{ return null; }
})).Where(a => (object)a != null);

For a real workd example, see AssemblyLoaderContainerContext.cs in my TextMteal project on GitHub. 有关实际工作示例,请参阅GitHub上TextMteal项目中的AssemblyLoaderContainerContext.cs

It is also worth noting that a naive attempt to "get me all the types that implement the interface of X" would fail to look at interface maps of types. 值得注意的是,“让我获得实现X接口的所有类型”的天真尝试将无法查看类型的接口映射。 Lucky for Core CLR, TypeInfo exposes ImplementedInterfaces which greatly simplifies this task versus the previos .NET full frameworks (prior to System.Runtime). 幸运的是,对于Core CLR,TypeInfo公开了ImplementedInterfaces,与previos .NET完整框架(在System.Runtime之前)相比,它大大简化了这项任务。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM