m.b*_*son 7 .net c# .net-core asp.net-core
我正在尝试编写一些逻辑来镜像原始.NET应用程序中的一些现有逻辑.在我的OnModelCreating()方法中,我想在加载的程序集中加载所有当前类型,以找到需要在模型中注册实体类型配置的类型.
这是在.NET中完成的AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetTypes()),但AppDomain在.NET Core中不再存在.
有没有新的方法来做到这一点?
我在网上看到了一些使用的例子,DependencyContext.Default.RuntimeLibraries但DependencyContext.Default似乎不再存在.
编辑:
我现在发现添加Microsoft.Extensions.DependencyModel到.netcoreapp1.1项目是有效的.然而,我实际上正在编写一个包含多个项目的解决方案,我不知何故需要在我的.netstandard1.4项目中执行此类型加载,其中我的DbContext实现和实体类型配置是
您在寻找什么在这里得到了广泛的解释。作者建议创建一个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"));
}
}
Run Code Online (Sandbox Code Playgroud)
通过升级我解决了这个问题.netstandard,从项目1.4到1.6.该套餐Microsoft.Extensions.DependencyModel 1.1.2现在有效.
编辑:
使用.netstandard2.0不需要AppDomainpolyfill类,因为它包含更多的.NET API,包括System.AppDomain