有没有办法自动配置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);
}
我尝试使用SelfConfigure,但找不到任何关于如何使用它来过滤配置文件的文档:
public static void Configure()
{
Mapper.Initialize(x =>
{
// My Custom profile
x.AddProfile<MyMappingProfile>();
// Scan Assembly
x.SelfConfigure(Assembly.GetCallingAssembly());
});
}
另一个问题是如何报告已经初始化的所有映射/配置文件(类似于StructureMap中的ObjectFactory.WhatDoIHave())?