2017-05-14 133 views
4

我想写一些逻辑来镜像原始的.NET应用程序中的一些现有的逻辑。在我的OnModelCreating()方法中,我想在加载的程序集中加载所有当前类型,以找到需要在模型中为实体类型配置注册的类型。替换为.NET核心中的AppDomain.GetLoadedAssemblies()?

这是在.NET中用AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetTypes())完成的,但是AppDomain不再存在于.NET Core中。

有没有新的方法来做到这一点?

我在网上看到一些使用DependencyContext.Default.RuntimeLibraries的例子,但是DependencyContext.Default似乎不再存在了。

编辑:

我现在,加入Microsoft.Extensions.DependencyModel发现一个.netcoreapp1.1项目工程。但是我其实写有多个项目的解决方案,我需要以某种方式在我.netstandard1.4项目做这种类型的装载在我的的DbContext实现和实体类型配置

回答

2

通过升级我的.netstandard项目从1.41.6修正这一点。包Microsoft.Extensions.DependencyModel 1.1.2现在工作。

编辑:

使用.netstandard2.0消除了一个AppDomain填充工具类的需要,因为它包含了更多的.NET API,其中包括System.AppDomain

3

你在找什么被广泛地解释here。作者建议创建polyfill。

如果页面丢失,我将复制粘贴。

public class AppDomain 
{ 
    public static AppDomain CurrentDomain { get; private set; } 

    static AppDomain() 
    { 
     CurrentDomain = new AppDomain(); 
    } 

    public Assembly[] GetAssemblies() 
    { 
     var assemblies = new List<Assembly>(); 
     var dependencies = DependencyContext.Default.RuntimeLibraries; 
     foreach (var library in dependencies) 
     { 
      if (IsCandidateCompilationLibrary(library)) 
      { 
       var assembly = Assembly.Load(new AssemblyName(library.Name)); 
       assemblies.Add(assembly); 
      } 
     } 
     return assemblies.ToArray(); 
    } 

    private static bool IsCandidateCompilationLibrary(RuntimeLibrary compilationLibrary) 
    { 
     return compilationLibrary.Name == ("Specify") 
      || compilationLibrary.Dependencies.Any(d => d.Name.StartsWith("Specify")); 
    } 
} 
+0

我看到沿着在博客中这些方针的东西,但是我有添加'Microsoft.Extensions.DependencyModel'包,但'DependencyContext.Default'似乎不存在。 –

+0

@ m.brookson我刚刚使用DependencyModel(版本1.1.2)创建了.NET核心1.1应用程序,它的工作原理非常完美。 在涉及另外的依赖可能(或不): 'Microsoft.DotNet.PlatformAbstractions 1.1.2, Microsoft.Extensions.DependencyModel 1.1.2, Newtonsoft.Json 9.0.1, System.Runtime.Serialization.Primitives 4.1.1' – MaLiN2223

+0

我实际上正在试图在.netstore1.4类库项目中实现这一点,该项目是由.netcoreapp1.1 web应用程序引用的。这会有所作为吗? –