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

查看:70
本文介绍了.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天全站免登陆