Dynamically Loaded Assemblies and InvalidCastException

M

Max Bolingbroke

Hi!

I've run into a problem with my app, and I feel sure I'm missing something
really basic!

Essentially, I have a plugin architecture, so my main application searches a
directory for DLLs, then uses System.Reflection.Assembly.LoadFrom to load
them up. These instances are scanned for classes which implement the core
interface Component (which is referenced at design time, and is in another
DLL called Core), then thoses class names are stored in an AvailablePlugin
class. This class has a method called CreateInstance which makes an instance
of the object in question.

This all seems to work OK, but the problem comes later. I have some code
that looks like this (in a non-interface class in the Core DLL called
Computer):

// Methods
public bool PowerOn()
{
// Power all components on
foreach(Component component in components)
{ if (component.PowerOn() == false) { return false; } }

But on the foreach line I get System.InvalidCastException, and the Locals
window reports the single item in component to be of the type:

System.Object {Batterseapower.ErstazPC.Components.Monitor}.

This object is defined as:

public class Monitor : Component

in the Plugin DLL (which also has a reference to the Core DLL), so surely it
should cast successfully??

I am adding the item to the components collection of Computer from the
Emulator project using the CreateInstance method. Please tell me what I've
done wrong, as I thought I was being really clever implementing it this way
:D

Max Bolingbroke

P.S Sorry for the long post..
 
H

Herman Eldering

Hi,

I'm not sure what's going wrong in your case. I suggest you try replacing
the foreach loop with a for loop, and then set a breakpoint on the
assignment where the exception occurs. Then you should look at the classname
of the object in the 'locals window'.

First of all you should check that the class can be assigned to the
particular variable. If the types match it might be that the classes/types
(with the same name) are loaded from different assemblies.

You should also make sure that the *same* interface is used in both
assemblies. So the plugin should reference the interface in the application.

I hope this helps you in solving the problem.

Herman Eldering
 
M

Max Bolingbroke

Thank you both for your answers! I'll take a look at what you suggested.

Max Bolingbroke
 
J

Jon Skeet

Max Bolingbroke said:
I've run into a problem with my app, and I feel sure I'm missing something
really basic!

<snip>

I suspect you've run into the same problem that most plugin framwork
authors run into - types loaded from different assemblies are
different, even if they have the same name.

Your Component interface should *only* be in the core DLL, not in the
DLL containing the implementing class. This kind of problem almost
*always* comes from people including the class in the plugin assembly
as well.

Of course, this may not be your problem - in which case please post
again, preferably with a short but complete program (it only needs to
be 3 classes and about 30 lines long).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top