AutoMapper: Mapper.Initialize() 只能调用一次,Why?

最开始的代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection; using AutoMapper; using Happy.ExtentionMethods;
using Happy.Bootstrap; namespace Happy.Bootstrap.AutoMapper
{
/// <summary>
/// 自动添加配置。
/// </summary>
public class AutoAddProfilePlugin : IBootstrapPlugin
{
/// <inheritdoc />
public void Start(IBootstrapService bootstrapService, Assembly assembly)
{
bootstrapService.MustNotNull("bootstrapService");
assembly.MustNotNull("assembly"); foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract || type.IsInterface)
{
continue;
} if (typeof(Profile).IsAssignableFrom(type))
{
Mapper.Initialize(x => {
x.AddProfile(Activator.CreateInstance(type) as Profile);
});
}
}
}
}
}

问题

我的项目中,每个 dll 都是自描述的,系统启动的时候,对每个 dll 对执行上面的插件,结果, Mapper.Initialize() 只有最后一次配置才有效,前面的配置会丢失。

最后的代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection; using AutoMapper; using Happy.ExtentionMethods;
using Happy.Bootstrap; namespace Happy.Bootstrap.AutoMapper
{
/// <summary>
/// 自动添加配置。
/// </summary>
public class AutoAddProfilePlugin : IBootstrapPlugin
{
/// <inheritdoc />
public void Start(IBootstrapService bootstrapService, Assembly assembly)
{
bootstrapService.MustNotNull("bootstrapService");
assembly.MustNotNull("assembly"); foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract || type.IsInterface)
{
continue;
} if (typeof(Profile).IsAssignableFrom(type))
{
Mapper.AddProfile(Activator.CreateInstance(type) as Profile);
}
}
}
}
}
上一篇:Java线程安全与锁优化


下一篇:jQuery-品牌列表案例