【问题标题】:the result lambda expression excuted "ToList()" method is diffrent than bofore执行“ToList()”方法的结果 lambda 表达式与以前不同
【发布时间】:2018-04-25 03:57:53
【问题描述】:

这是我的代码:

项目一:

public interface IComponent{}

public class ComponentTest:IComponent{}

public class Bootstraper
{
   public virtual IEnumerable<IComponent> GetComponents()
   {
       return Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll")
       .Select(Assembly.LoadFile).SelectMany(m => m.GetTypes()).Select(m => m.GetTypeInfo())
       .Where(m => !m.IsAbstract && !m.IsInterface 
                   && typeof(IComponent).IsAssignableFrom(m)).ToList()
       .Select(Activator.CreateInstance).Cast<IPlatformComponent>();
   }
}

项目单元测试:

[TestMethod]
public void GetComponenentsIsSuccess()
{
    Bootstraper testObj=new Bootstraper();

    IEnumerable<IComponent> ienumerable = testObj.GetComponents();
    Assert.IsTrue(ienumerable.Any(m=>m.GetType()==typeof(ComponenentTest)));
}

执行单元测试时找不到ComponenetTest

我有两个问题:

1.on VS Debugger View,代码 Where(m=>!m.IsAbstract&&!m.IsInterface&&typeof(IComponent).IsAssignableFrom(m)) 可以得到类型ComponenentTest,但是当ToList()方法执行时类型消失了。

2.当代码为 typeof(IComoponent) 时,ComponentTest 类型消失,但我使用 typeof(object) 代替,在执行 'ToList()' 后,ComponentTest 仍在收集中

【问题讨论】:

  • 运行时是.net core 2.0

标签: .net reflection lambda


【解决方案1】:

为 Tolist() 添加命名空间 使用 System.Linq;

【讨论】:

  • 命名空间已添加
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 2013-07-16
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多