可移植类库反射 [英] Portable Class Library Reflection
本文介绍了可移植类库反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试将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做同样的事情?
解决方案
这是由于type/typeinfo拆分所致.请参见演进反射API .>
尝试以下代码:
assembly.DefinedTypes
.Where(x => x.IsClass && !x.IsAbstract && x.ImplementedInterfaces
.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == commandHandlerOpenGenericType))
.Select(x => x.AsType())
I am currently trying to convert a Xamarin.iOS app library to a PCL (Profile 78). I have this code that will not compile:
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)));
}
}
Here is an image of the compiler errors:
How can I do the same thing with the new reflection API?
解决方案
This is due to the type/typeinfo split. See Evolving the Reflection API.
Try this code:
assembly.DefinedTypes
.Where(x => x.IsClass && !x.IsAbstract && x.ImplementedInterfaces
.Any(i => i.GetTypeInfo().IsGenericType && i.GetGenericTypeDefinition() == commandHandlerOpenGenericType))
.Select(x => x.AsType())
这篇关于可移植类库反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文