便携式类库reflection
我目前正在尝试将Xamarin.iOS应用程序库转换为PCL(配置文件78)。 我有这个不能编译的代码:
public static void RegisterAllCommandHandlers(IEnumerable assemblies) { // Get all types that are concrete classes which implement ICommandHandler var commandHandlerOpenGenericType = typeof(ICommandHandler); var types = new List(); foreach (var assembly in assemblies) { types.AddRange(assembly.GetTypes() .Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == commandHandlerOpenGenericType))); } }
这是编译器错误的图像:
如何使用新的reflectionAPI执行相同的操作?
这是由于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())