Search code examples
c#pluginsobfuscationappdomainmarshalbyrefobject

Find main class that inherits from X without using GetTypes?


I have an application that load plugins on-demand(uses AppDomain and MarshalByRefObject) and I am trying to find a different way to validate a plugin.

Right now, I identify run-able plugins like this:

_pluginAassembly = AppDomain.CurrentDomain.Load(assemblyName);
foreach (var type in _pluginAassembly.GetTypes().Where(type => type.GetInterface("IPluginChat") != null || type.GetInterface("IPlugin") != null))
{
    _instance = Activator.CreateInstance(type, null, null);
    return ((IPlugin)_instance).Check();
}

However, if one of the plugins is obfuscated GetTypes will fail instantly:

Could not load type 'Invalid_Token.0x01003FFF' from assembly 'SimplePlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Hence this method would not allow me to run both obfuscated and non-obfuscated plugins.

I understand that I would require the plugins to have the interface imported methods and main class to not be obfuscated for this to work.

In short, is there a way to find my main class which inherits from one of the 2 mentioned interfaces(IPlugin or IPluginChat) without having to use GetTypes which fails to read the obfuscated types?


Solution

  • Indeed you can, use the "is" keyword, it will return true if the specified instance is of a certain type.

    Here is a very simple example:

        interface a
        {
    
            int doIt();
    
        }
    
        interface b
        {
    
            int doItAgain();
    
        }
    
        class c : a, b
        {
    
            public int doIt() { return 1;  }
            public int doItAgain() { return 2;  }
    
        }
    
        var instance = new c();
    
        var isA = instance is a; //true
        var isB = instance is b; //true
        var isAForm = instance is Form; //false