vendredi 4 octobre 2019

Why does interface comparison fail with Assembly.ReflectionOnlyLoadFrom() and not with Assembly.LoadFrom()?

My code is scanning various directories for plugin DLLs. They are recognisable because they have an exported type implementing IBehaviourPlugin.

Following several online posts, I've tried loading the assemblies with the following code (file loop stripped out for readability):

    // Plugin interface
    Type behaviourPlugin = typeof(OrganShared.IBehaviourPlugin);

    // Load assembly from file
    Assembly a = Assembly.ReflectionOnlyLoadFrom(f);
    Type[] exports = a.GetExportedTypes();
    int validTypes = (from Type t in exports
                          where behaviourPlugin.IsAssignableFrom(t)
                          && behaviourPlugin.FullName != t.FullName
                          select t).Count();
    if (validTypes > 0) { files.Add(new FileInfo(f)); }

This fails to identify any assemblies which fit the criteria. However, if I replace Assembly a = Assembly.ReflectionOnlyLoadFrom(f); with Assembly a = Assembly.LoadFrom(f);, the types compare perfectly, and all my plugin DLLs are identified correctly.

Can anyone explain to me the reason for this different behaviour? It's not my first outing with reflection, but this one is a bit beyond me.

Incidentally, behaviour is the same if I use the alternate method of checking: t.GetInterfaces().Contains(IBehaviourPlugin)

Lastly, I'm not currently creating a new AppDomain for the reflection - I suspect I can / should / will need to, but I wanted to isolate this behaviour before potentially adding further complication.





Aucun commentaire:

Enregistrer un commentaire