.NET Core 1.0,枚举所有实现基础 class 的 classes

.NET Core 1.0, Enumerate All classes that implement base class

我正在努力将一个 ASP.NET 项目迁移到 RC2。我正在使用 AutoFac 尝试枚举实现 AutoMapper 配置文件基础 class 的 classes 来设置我所有的映射配置文件,而无需显式调用它们。以前在 ASP.NET 的旧版本中(甚至在 RC1 中)我能够使用以下代码:

public class AutoMapperModule : Module
{

    protected override void Load(ContainerBuilder builder)
    {

        builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>();

        builder.Register(context =>
        {
            var profiles =
               AppDomain.CurrentDomain.GetAssemblies()
               .SelectMany(IoC.GetLoadableTypes)
               .Where(t => t != typeof(Profile) && t.Name != "NamedProfile" && typeof(Profile).IsAssignableFrom(t));

            var config = new MapperConfiguration(cfg =>
            {
                foreach (var profile in profiles)
                {
                    cfg.AddProfile((Profile)Activator.CreateInstance(profile));
                }
            });
            return config;
        })
        .AsSelf()
        .As<IConfigurationProvider>()
        .SingleInstance();

        builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
        builder.RegisterType<MappingEngine>().As<IMappingEngine>();

    }
}

这非常有效,直到我尝试使用新的 netcoreapp1.0 框架将我的项目转换为 RC2,除了现在我在 AppDomain 上收到一个设计时错误,指出 "AppDomain does not exist in the current context"。我已经看到一些关于使用 ILibraryManager 或 DependencyContext 来执行此操作的建议,但我无法弄清楚如何让其中的任何一个工作。有什么建议吗?

.Net Core 当前 (1.0 RTM) 不支持 AppDomain.GetAssemblies() 或类似的 API。 It's likely that it will support it in 1.1.

到那时,如果您需要此功能,您将需要坚持使用 net452(即 .Net Framework)而不是 netcoreapp1.0

这行得通吗?

var all =
        Assembly
        .GetEntryAssembly()
        .GetReferencedAssemblies()
        .Select(Assembly.Load)
        .SelectMany(x => x.DefinedTypes)
        .Where(type => typeof(ICloudProvider).IsAssignableFrom(type.AsType()));
foreach (var ti in all)
{
    var t = ti.AsType();
    if (!t.Equals(typeof(ICloudProvider)))
    {
        // do work
    }
}