【发布时间】: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