相关疑难解决方法(0)

如何在AutoMapper中扫描和自动配置配置文件?

有没有办法自动配置Automapper来扫描命名空间/程序集中的所有配置文件?我想要做的是从给定的程序集中过滤的给定程序集中将映射配置文件添加到AutoMapper,类似于StructureMap中的扫描约定:

    public static void Configure()
    {
        ObjectFactory.Initialize(x =>
            {
                // Scan Assembly
                x.Scan(
                    scanner =>
                    {
                        scanner.TheCallingAssembly();
                        scanner.Convention<MyCustomConvention>();
                        scanner.WithDefaultConventions();
                    });

                // Add Registries
                x.AddRegistry(new SomeRegistry());
            });

        Debug.WriteLine(ObjectFactory.WhatDoIHave());
    }

public class MyCustomConvention : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        if (!type.CanBeCastTo(typeof(IMyType)))
        {
            return;
        }

        string name = type.Name.Replace("SomeRubishName", String.Empty);
        registry.AddType(typeof(IMyType), type, name);            
    }
Run Code Online (Sandbox Code Playgroud)

我试过使用SelfConfigure但找不到任何关于如何使用它来过滤配置文件的文档:

    public static void Configure()
    {
        Mapper.Initialize(x =>
                              {
                                  // My Custom profile
                                  x.AddProfile<MyMappingProfile>();

                                  // Scan Assembly
                                  x.SelfConfigure(Assembly.GetCallingAssembly());
                              });
    }
Run Code Online (Sandbox Code Playgroud)

另一个问题是我如何报告已经初始化的所有地图/配置文件(类似于StructureMap中的ObjectFactory.WhatDoIHave())?

.net structuremap profile automapper

22
推荐指数
4
解决办法
2万
查看次数

标签 统计

.net ×1

automapper ×1

profile ×1

structuremap ×1