我目前正在尝试将Xamarin.iOS应用程序库转换为PCL(配置文件78)。我有这个不能编译的代码:
public static void RegisterAllCommandHandlers(IEnumerable<Assembly> assemblies) {
// Get all types that are concrete classes which implement ICommandHandler
var commandHandlerOpenGenericType = typeof(ICommandHandler<>);
var types = new List<Type>();
foreach (var assembly in assemblies) {
types.AddRange(assembly.GetTypes()
.Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == commandHandlerOpenGenericType)));
}
}
以下是编译器错误的图像:
如何使用新的反射API执行相同的操作?
答案 0 :(得分:16)
这是由于type / typeinfo拆分造成的。请参阅Evolving the Reflection API。
试试这段代码:
assembly.DefinedTypes
.Where(x => x.IsClass && !x.IsAbstract && x.ImplementedInterfaces
.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == commandHandlerOpenGenericType))
.Select(x => x.AsType())