C
C#2mo ago
Jordan

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
Jordan
JordanOP2mo ago
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!
Keswiik
Keswiik2mo ago
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-implements
Jordan
JordanOP2mo ago
thanks 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
Keswiik
Keswiik2mo ago
not the runtime's job to catch user errors like that ¯\_(ツ)_/¯
Jordan
JordanOP2mo ago
fair enough...
canton7
canton72mo ago
Where is IPlugin defined? In a third assembly that both you and the plugins reference?
Jordan
JordanOP2mo ago
yep
Want results from more Discord servers?
Add your server