Question

I'm working with some, unfortunately largely undocumented, existing code and I'm having trouble understanding how it calls upon the methods of the plugins it loads.

My aim at the moment is simply to step into one of the methods loaded via the plugin manager, as it's causing an exception. However I had to rebuild the pluginManager from the source to get debug symbols and when I reference this new DLL version the compiler throws up arms.

The code appears to load the plugin into plug.Instance and then access the specific methods like so plug.Instance.ReturnLeaNumber(); This compiler error makes sense, because it doesn't know the details of the plugins. What confuses me is how the compiler ever knew these where valid before run time, when no plugins are initialized. I can step through the code that doesn't work now with the older DLL!

This is an example of where the program loads up a plugin.

plug = GenericServicePlugins.AvailablePlugins.Find(Application.StartupPath + "\\Dlls\\SchoolInterface.dll");
// Compiler doesn't like this next line anymore though
plug.Instance.Initialize(null, null); 

If there are any differences between my rebuilt library and the previously working one, I can't tell how as the versions match up with the ones in our source control. Would appreciate some advice on where to start looking!

public interface IGenericPluginMasterInterface
{
    String returnName();
    void Initialize(ExceptionStringResources.Translate ExceptionStrings);
    Object ExecuteFunction(String macAddress, bool log, String functionName, LoginCredentials logonCredentials, WebConfiguration webConfig,
                           Int64 dataLinkId, DataLinkParam[] dataLinkParams, String dataName, 
                           DataParam[] dataParams, Object[] additionalParams);
}

Rest of Manager code on PasteBin

How does the compiler know about these plug.Instance.Method() methods before runtime?

Edit:

I've not quite worked this out yet, but there was a "PluginsService" file I missed which partly mirrors the "GenericPluginServices". I think this error could have been caused when I removed parts of this class that related to an now defunct plugin, which I am looking into. However I figured posting this other code snippet would help the question.

PluginService.cs code GenericPluginService code

Was it helpful?

Solution

Find returns AvailablePlugin, so .Instance is of type IGenericPluginMasterInterface; if so, indeed; that .Instance.ReturnLeaNumber() can't possibly work...

The only way that could work (without introducing some generics etc) is if .Instance actually returned dynamic. With dynamic the name/method resolution is happening at runtime. The compiler treats dynamic very deliberately such as to defer all resolution to runtime, based on either reflection (for simple cases) or IDynamicMetaObjectProvider (for more sohpisticated cases).

However, if the code you have doesn't match what was compiled, then: we can't tell you what it was. IMO, the best option is to get hold of the working dll, and look at it in reflector to see what it is actually doing, and how it is different to the source code that you have.

Actually, strictly speaking it could still do that with the code you've pasted, but only if plug is typed as dynamic, i.e. dynamic plug = ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top