2013-12-11 143 views
7

我目前正試圖將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?

+0

你在你的PCL指定哪些平臺? – Markus

+0

@Markus Profile 78(Xamarin.IOS,Xamarin.Android,.net 4.5,windows store,windows phone 8 –

+0

你得到的編譯器錯誤是什麼?(該圖不顯示編譯器錯誤) – elgonzo

回答

16

這是由於type/typeinfo split。見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()) 
+0

非常感謝! –