【问题标题】:Portable Class Library Reflection可移植类库反射
【发布时间】:2013-12-13 22:58:48
【问题描述】:

我目前正在尝试将 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 做同样的事情?

【问题讨论】:

  • 您在 PCL 中针对哪些平台?
  • @Markus Profile 78(Xamarin.IOS、Xamarin.Android、.net 4.5、windows store、windows phone 8
  • 你得到什么编译器错误? (图片未显示编译器错误)。
  • @elgonzo “无法解析符号”为每个红色项目

标签: c# reflection portable-class-library


【解决方案1】:

这是由于类型/类型信息拆分造成的。见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())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多