【发布时间】:2016-11-21 10:51:18
【问题描述】:
使用反射,我如何获得所有在 .NET Core 中实现特定接口的类型?我注意到 .NET 4.6 中可用的方法不再可用。
例如,此代码不起作用。
var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
它会抛出 The name 'AppDomain' does not exist in the current context 错误。
【问题讨论】:
-
我确信代码可以正常工作,只是你没有
AppDomain。 -
@BlueEyedBehemoth 所以如何包含 AppDomain? :D
-
你需要它做什么?这通常不是你惹的祸。
-
@BlueEyedBehemoth 这是一个非常标准的插件支持模式。
-
@HansPassant 这不是真的。人们可能会在不会移植到 CoreRT 的环境中使用 Core。假设每个人都会将他们的代码移植到 CoreRT 是不屑一顾的。
标签: c# .net reflection lambda .net-core