0

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?

Guapo
  • 3,446
  • 9
  • 36
  • 63

1 Answers1

1

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
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • Yes, but I don't know the main class name hence why I had the loop. In short, I need an automated way to find the class that inherits from a given interface without using GetTypes and knowing the class name, unlike your example... – Guapo Jan 05 '16 at 13:48
  • Then there's no way to know the types on the assembly unless you use GetTypes. Surround the GetTypes() with try/catch and if its a reflectiontypeloadexception use the list of types in the exception arguments, it will just have null for the failed types. Check this answer for more info: http://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes – Gusman Jan 05 '16 at 13:53
  • bullseye that answer in your comment works ;) – Guapo Jan 05 '16 at 13:59