如何建立一个IEnumerable?

O.O*_*O.O 0 c# collections

无法建立配置文件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)

SLa*_*aks 7

IEnumerable<T> 是不可改变的.

.Concat()返回IEnumerable<T>带有连接序列的new ; 你忽略了这个结果.

  • @OO,嗯,确实如此.完全和完全,除了你的第一个`NullPointerException`. (2认同)