便携式类库反射

时间:2013-12-11 16:13:36

标签: c# reflection portable-class-library

我目前正在尝试将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)));
            }
    }

以下是编译器错误的图像: enter image description here

如何使用新的反射API执行相同的操作?

1 个答案:

答案 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())