无法建立配置文件IEnumerable. Mapper.Initialize需要只运行一次项目中的所有配置文件.尝试设置profiles = new List<Profile>(),但配置文件计数始终为0.
IEnumerable<Profile> profiles = null;
var profileType = typeof(Profile);
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.FullName.Contains("Cars.Data"));
foreach (var assembly in assemblies)
{
profiles.Concat(
assembly.GetTypes()
.Where(t => profileType.IsAssignableFrom(t) &&
t.GetConstructor(Type.EmptyTypes) != null)
.Select(Activator.CreateInstance)
.Cast<Profile>());
}
Mapper.Initialize(c => profiles.ForEach(c.AddProfile));
Run Code Online (Sandbox Code Playgroud)
IEnumerable<T> 是不可改变的.
.Concat()返回IEnumerable<T>带有连接序列的new ; 你忽略了这个结果.