gpt4 book ai didi

c# - .NET Core 中 AppDomain.GetLoadedAssemblies() 的替代品?

转载 作者:太空狗 更新时间:2023-10-29 20:06:59 37 4
gpt4 key购买 nike

我正在尝试编写一些逻辑来反射(reflect)原始 .NET 应用程序中的一些现有逻辑。在我的 OnModelCreating() 方法中,我想在加载的程序集中加载所有当前类型,以找到需要在模型中注册实体类型配置的类型。

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

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

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

编辑:

我现在发现将 Microsoft.Extensions.DependencyModel 添加到 .netcoreapp1.1 项目是可行的。然而,我实际上正在编写一个包含多个项目的解决方案,我不知何故需要在我的 .netstandard1.4 项目中加载这种类型,我的 DbContext 实现和实体类型配置是

最佳答案

您正在寻找的内容已得到广泛解释 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"));
}
}

关于c# - .NET Core 中 AppDomain.GetLoadedAssemblies() 的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43965820/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com