.net 结果lambda表达式执行;ToList();这种方法与以前不同
这是我的密码: 项目1:.net 结果lambda表达式执行;ToList();这种方法与以前不同,.net,reflection,lambda,.net,Reflection,Lambda,这是我的密码: 项目1: public interface IComponent{} public class ComponentTest:IComponent{} public class Bootstraper { public virtual IEnumerable<IComponent> GetComponents() { return Directory.GetFiles(AppDomain.CurrentDomain.BaseDirector
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>();
}
}
公共接口IComponent{}
公共类ComponentTest:IComponent{}
公共类引导器
{
公共虚拟IEnumerable GetComponents()
{
返回目录.GetFiles(AppDomain.CurrentDomain.BaseDirectory,“*.dll”)
.Select(Assembly.LoadFile).SelectMany(m=>m.GetTypes()).Select(m=>m.GetTypeInfo())
.其中(m=>!m.isastract&!m.IsInterface
&&typeof(IComponent).IsAssignableFrom(m)).ToList()
.Select(Activator.CreateInstance.Cast();
}
}
项目单元测试:
[TestMethod]
public void GetComponenentsIsSuccess()
{
Bootstraper testObj=new Bootstraper();
IEnumerable<IComponent> ienumerable = testObj.GetComponents();
Assert.IsTrue(ienumerable.Any(m=>m.GetType()==typeof(ComponenentTest)));
}
[TestMethod]
public void GetComponenentsIsSuccess()
{
Bootstraper testObj=new Bootstraper();
IEnumerable IEnumerable=testObj.GetComponents();
Assert.IsTrue(ienumerable.Any(m=>m.GetType()==typeof(ComponenentTest));
}
执行单元测试时,它找不到组件测试
我有两个问题:
1.在VS Debugger视图中,代码
其中(m=>!m.isastract&&!m.IsInterface&&typeof(IComponent).IsAssignableFrom(m))
可以获取类型组件测试
,但在执行ToList()
方法时,该类型被分发
2.code为typeof(IComoponent)
时,类型ComponentTest
是分散的,但我使用typeof(object)
代替,在执行“ToList()”之后,ComponentTest
仍在集合中对于ToList(),添加命名空间
使用System.Linq 命名空间已添加运行时为.net core 2.0