Search code examples
c#.netwindows-8.1windows-phone-8.1.net-assembly

Get all classes in a referenced assembly for Windows 8.1 Universal app


I have a project that I am moving from Windows Phone 8 to Universal Windows 8.1.

Scenario

In the Solution I have multiple assemblies that implements interfaces that are defined in common assembly (but not all in each, they vary), but all implements or extends the interface IResolver.

I used reflection in my "main" project/assembly to register the classes that are in the referenced assemblies by passing it an instance of an implementation of the IResolver (interface) of that assembly.

The code

using System.Reflection;
...
public void Register(IResolver resolver)
{
   foreach (Type classType in resolver.GetType().Assembly.GetTypes())
   {
      // Register the class type with the ioc container
   }
}

And it worked like a charm :-)

The problem

System.Type does not contain a definition for Assembly and no extension method Assembly accepting a first argument of type System.Type could be found

In the documentation of Type.Assembly it states:

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Thus not supported in Windows 8.1, but even if it is in the Windows Phone 8.1 project, still says does not contain a definition for Assembly.

And if that's not enough, the class Assemly does not implement the method GetTypes()

The question

How do I get all of the classes defined in a referenced assembly for Universal App from an instance of an interface?

Any suggestions or alternatives?

Things that don't work

Assembly.GetExecutingAssembly()

Error: System.Reflection.Assembly does not contain a definition for GetExecutingAssembly


Solution

  • Well, this was annoying...

    GetType().GetTypeInfo()

    public void Register(IResolver resolver)
    {
       foreach (Type classType in resolver.GetType().GetTypeInfo().Assembly.GetExportedTypes())
       {
          // Register the class type with the ioc container
       }
    }