.NET Core 1.0,枚举所有实现基类的类 [英] .NET Core 1.0, Enumerate All classes that implement base class

查看:25
本文介绍了.NET Core 1.0,枚举所有实现基类的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on migrating an ASP.NET project to RC2. I am using AutoFac to try to enumerate classes that implement AutoMapper Profile base class to set up all my mapping profiles without having to call them explicitly. Previously in older version of ASP.NET (even in RC1) I was able to use the following code:

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".我已经看到了一些关于使用 ILibraryManager 或 DependencyContext 来做到这一点的建议,但我不知道如何让其中的任何一个工作.有什么建议吗?

This worked fantastically, until I tried converting my project to RC2 using the new netcoreapp1.0 framework, except now I am getting a design time error on AppDomain stating the "AppDomain does not exist in the current context". I've seen some suggestions about using ILibraryManager or DependencyContext to do this but I can't figure out how to get any of that to work. Any suggestions?

推荐答案

.Net Core 当前 (1.0 RTM) 不支持 AppDomain.GetAssemblies() 或类似的 API.很可能会在 1.1 中支持它.

.Net Core currently (1.0 RTM) does not support AppDomain.GetAssemblies() or a similar API. It's likely that it will support it in 1.1.

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

Until then, if you need this feature, you will need to stick with net452 (i.e. .Net Framework) instead of netcoreapp1.0.

这篇关于.NET Core 1.0,枚举所有实现基类的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆