7
votes

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: enter image description here

How can I do the same thing with the new reflection API?

1
Which platforms do you target in your PCL?Markus
@Markus Profile 78 (Xamarin.IOS, Xamarin.Android, .net 4.5, windows store, windows phone 8Chris Kooken
What are the compiler errors you get? (The image does not show the compiler errors).user2819245
@elgonzo "Cannot resolve symbol" for each item in redChris Kooken

1 Answers

16
votes

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