lundi 16 octobre 2017

Types from Assembly.GetExportedTypes do not show interfaces from other assembly

I've got a solution that has three projects. In the first project, I've got a library that exports an interface type named IHandler. In the second project, I've got a library that exports a class that subscribes to the IHandler interface from the first project. In the third and final project, I've got a console application that loads the second project, at run-time, and creates an instance of the mentioned class.

I'm trying to add some code to the third project, the console app, that makes sure that the class from the second project actually subscribes to IHandler. To do this, I've got the following code ...

public static IHandler Load(string path)
{
    var absolute = Path.GetFullPath(path);

    var library = Assembly.LoadFile(absolute);

    foreach (var type in library.GetExportedTypes())
    {
        if (type.GetInterface(typeof(IHandler).FullName) == null)
        {
            continue;
        }

        return Activator.CreateInstance(type) as IHandler;
    }
}

So, this code runs inside my console project. It loads my library project and makes sure the exported type subscribes to the IHandler interface. The IHandler interface is from a third project.

The problem is that every method I try of checking the interfaces on the exported class of mine, that does subscribe to IHandler, shows that the type has no interfaces. I can't seem to ensure that my loaded type has the interface it needs.

Any ideas?





Aucun commentaire:

Enregistrer un commentaire