473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dynamically Loaded Assemblies and InvalidCastException

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..
Nov 15 '05 #1
3 2042
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
"Max Bolingbroke" <ba************@hotmail.com> wrote in message
news:bi**********@newsg2.svr.pol.co.uk...
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..

Nov 15 '05 #2
Thank you both for your answers! I'll take a look at what you suggested.

Max Bolingbroke
Nov 15 '05 #3
Max Bolingbroke <ba************@hotmail.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lanky_tx | last post by:
Hi All, We have an automated build and test environment using NAnt and Nunit. Some of our assemblies are being strong named by modifying the AssemblyInfo.cs and having csc compile it. Some of...
1
by: Robert Vasquez | last post by:
I would like my application to be able to load modules dynamically and release them once they aren't needed. For example in c++ I would load a dll containing the required function, run it, then...
3
by: lanky_tx | last post by:
Hi All, We have an automated build and test environment using NAnt and Nunit. Some of our assemblies are being strong named by modifying the AssemblyInfo.cs and having csc compile it. Some of...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.