之前.net framwork用的是:
System.Web.Compilation.BuildManager.GetReferencedAssemblies();
在.net core上好像用不了。
请问,我想要获取所有页编译都必须引用的程序集,应该怎么实现,谢谢。
代码如下,欲实现AutoFac的注册:
/// <summary>
/// AutoFac注册
/// </summary>
public class DefaultModuleRegister : Module
{
protected override void Load(ContainerBuilder builder)
{
var assemblys = System.Web.Compilation.BuildManager.GetReferencedAssemblies()
.Where(m =>
m.FullName.Contains(".ApplicationService") ||
m.FullName.Contains(".Infrastructure.Repository"))
.ToArray();
builder.RegisterAssemblyTypes(assemblys)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(assemblys)
.Where(t => t.Name.EndsWith("Repos"))
.AsImplementedInterfaces().InstancePerLifetimeScope();
}
}
已解决:
IEnumerable<Assembly> allAssemblies = Assembly.GetEntryAssembly().GetReferencedAssemblies().Select(Assembly.Load);
Assembly[] assemblies = allAssemblies.Where(m =>
m.FullName.Contains(".ApplicationService") ||
m.FullName.Contains(".Infrastructure.Repository"))
.ToArray();
试试 Assembly.GetEntryAssembly().GetReferencedAssemblies()
试了,返回的是AssemblyName的数组,RegisterAssemblyTypes方法需要Assembly数组,是还需要转换下吗?
https://github.com/hstarorg/Lara/blob/master/src/Lara.DI/Helpers/AutofacHelper.cs 也可以参考下。