Reflection Load Interface Instance
so i am loading plugins from multiple assemblies and i look for types Implementing the IPlugin interface. I then create an instance of that class (i tried both using activator and manual constuctor call) and when a look at the GetType from the instance i get back, i see the IPlugin and IDisposalbe interfaces listed as implemented on the Type Info, but when i try to cast/use the interface methods i simply get an exception or null (System.InvalidCastException: Unable to cast object of type 'TestPlugin.TestPluginClass' to type 'PluginBase.IPlugin'.
7 Replies
for reference, this is the code snippet i am currently working on: https://paste.mod.gg/ghmsaidmnyrr/0
BlazeBin - ghmsaidmnyrr
A tool for sharing your source code with the world!
First off, and this is more of a nitpick, why not search for your plugins by using
Type.IsAssignableFrom(baseType)
?
And secondly my guess is that you're running into some wonky assembly issues, take a look at this: https://stackoverflow.com/questions/1596796/net-unable-to-cast-object-to-interface-it-implementsthanks so much @ded, this way exactly the issue i had been facing! it nearly drove me insane xD. its also cool how dotnet doest even give you a warning or something if you load the same assembly 10 times or so
not the runtime's job to catch user errors like that ¯\_(ツ)_/¯
fair enough...
Where is IPlugin defined? In a third assembly that both you and the plugins reference?
yep