Replacement for AppDomain.GetLoadedAssemblies () in .NET Core?

I am trying to write some logic to reflect some existing logic in the original .NET application. In my method, OnModelCreating()I want to load all current types into loaded assemblies in order to find those that need to be registered for the entity type configuration in the model.

This was done in .NET with AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetTypes()), but AppDomainno longer exists in .NET Core.

Is there a new way to do this?

I have seen some examples of online use DependencyContext.Default.RuntimeLibraries, however DependencyContext.Defaultit does not seem to exist anymore.

Edit:

Now I have found that the project is being added Microsoft.Extensions.DependencyModelto the project .netcoreapp1.1. However, I actually write a solution with several projects, and somehow I need to load this type in my project .netstandard1.4, where is my implementation of the DbContext implementation and the entity type

+6
source share
2 answers

Fixed this by updating my projects .netstandardfrom 1.4to 1.6. The package Microsoft.Extensions.DependencyModel 1.1.2now works.

Edit:

Use .netstandard2.0eliminates the need for a AppDomainpolyfill class , as it contains many other .NET APIs, includingSystem.AppDomain

+5
source

, , . .

, .

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

Source: https://habr.com/ru/post/1677117/


All Articles