获得CoreCLR可用的类型 [英] Get available types in CoreCLR

查看:1149
本文介绍了获得CoreCLR可用的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很容易得到所有可用的类型(对于某些接口为例)在旧的.NET,但我不能找到办法如何做到这一点,在新CoreCLR。

This is easy to get all available types (for some interface for example) in the old .NET, but I can't find the way how to do that in the new CoreCLR.

我想要做的是有像GetRepository功能,应该寻找IRepository现有的实现,并返回该类型的新实例。 。实施将设在不同的项目

What I want to do is to have function like GetRepository, that should look for existing implementation of IRepository and return new instance of that type. Implementation will be located in the different project.

所以,在.NET我可以使用这样的:

So, in .NET I can use something like this:

AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes())

我有CoreCLR目前唯一的解决办法是:

The only solution I have for CoreCLR for now is:

public T GetRepository<T>()
{
  foreach (Type type in typeof(T).GetTypeInfo().Assembly.GetTypes())
    if (typeof(T).IsAssignableFrom(type) && type.GetTypeInfo().IsClass)
      return (T)Activator.CreateInstance(type);

  return default(T);
}



但它工作仅当接口和实现都位于同一组件(和这不是我的情况)。

But it works only if interface and implementation are located in the same assembly (and this is not my case).

感谢您!

推荐答案

所以,这里是来自微软的答案:
https://github.com/dotnet/ coreclr /问题/ 919

So, here is the answer from Microsoft: https://github.com/dotnet/coreclr/issues/919

在简而言之,新的

Microsoft.Framework.Runtime.LibraryManager

public IEnumerable<ILibraryInformation> GetLibraries();
public IEnumerable<ILibraryInformation> GetReferencingLibraries(string name);



etc

UPD: 从RC2使用Microsoft.Extensions.DependencyModel.DependencyContext开始,而不是:

UPD: starting from RC2 use Microsoft.Extensions.DependencyModel.DependencyContext instead:

DependencyContext.Default.CompileLibraries
DependencyContext.Default.RuntimeLibraries

这篇关于获得CoreCLR可用的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆