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
Max Bolingbroke
P.S Sorry for the long post..
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
Max Bolingbroke
P.S Sorry for the long post..