简体   繁体   English

在.NET Core中替换AppDomain.GetLoadedAssemblies()?

[英]Replacement for AppDomain.GetLoadedAssemblies() in .NET Core?

I'm trying to write some logic to mirror some existing logic in an original .NET application. 我正在尝试编写一些逻辑来镜像原始.NET应用程序中的一些现有逻辑。 In my OnModelCreating() method I want to load all current types in loaded assemblies to find the ones that need to be registered for entity type configuration in the model. 在我的OnModelCreating()方法中,我想加载已加载程序集中的所有当前类型,以查找需要在模型中注册实体类型配置的类型。

This was done in .NET with AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetTypes()) , however AppDomain no longer exists in .NET Core. 这是在.NET中使用AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetTypes()) ,但.NET Core中不再存在AppDomain

Is there a new way to do this? 有没有新的方法来做到这一点?

I have seen some examples online of using DependencyContext.Default.RuntimeLibraries however DependencyContext.Default does not seem to exist anymore either. 我在网上看到了一些使用DependencyContext.Default.RuntimeLibraries例子,但DependencyContext.Default似乎也不再存在。

Edit: 编辑:

I have found now that adding Microsoft.Extensions.DependencyModel to a .netcoreapp1.1 project works. 我现在发现将Microsoft.Extensions.DependencyModel添加到.netcoreapp1.1项目可以正常工作。 However I am actually writing a solution with multiple projects and I somehow need do this type loading in my .netstandard1.4 project where my DbContext implementation and entity type configuration is 但是我实际上正在编写一个包含多个项目的解决方案,我在某种程度上需要在我的.netstandard1.4项目中执行此类型加载,其中我的DbContext实现和实体类型配置是

What you are looking for is broadly explained here . 您正在寻找的内容在此处进行了广泛的解释。 The author recommends creating a polyfill. 作者建议创建一个polyfill。

I am going to copy and paste in case the page goes missing. 我打算复制并粘贴以防页面丢失。

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"));
    }
}

Fixed this by upgrading my .netstandard projects from 1.4 to 1.6 . 通过将我的.netstandard项目从1.4升级到1.6 The package Microsoft.Extensions.DependencyModel 1.1.2 now works. Microsoft.Extensions.DependencyModel 1.1.2包现在可以正常工作。

Edit: 编辑:

Using .netstandard2.0 removes the need for an AppDomain polyfill class since it contains many more .NET APIs including System.AppDomain 使用.netstandard2.0不再需要AppDomain polyfill类,因为它包含更多.NET API,包括System.AppDomain

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

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