Compact Framework - can't cast across DLLs??

D

Derrick

Hopefully someone can explain to me why this doesn't work:

- I have an interface ("IFoo"), the source of which lives in "Assembly A"
- A class ("Foo") which implements this interface lives in "Assembly B",
which obviously has a reference to "A"
- In "Assembly C", which has references to both "A" and "B", I'm trying to
cast an instance of the class to the interface, like so:

private void SomeFunction (object o)
{
IFoo f = (IFoo) o; //looking in the debugger, I can see that "o" is an
instance of "Foo"
}

(Tthere are actually 2 classes which implement the interface, and when the
time comes to do the cast, I don't know which class it is - hence my using
an interface :)

When this runs, I get an invalid cast exception. Any idea why?? In the
assembly info files, I have the version numbers fixed, so they don't change
on every build. Also, these are Compact Framework assemblies.

Any input is appreciated!!

Derrick
 
J

Jon Skeet [C# MVP]

Derrick said:
Hopefully someone can explain to me why this doesn't work:

- I have an interface ("IFoo"), the source of which lives in "Assembly A"
- A class ("Foo") which implements this interface lives in "Assembly B",
which obviously has a reference to "A"
- In "Assembly C", which has references to both "A" and "B", I'm trying to
cast an instance of the class to the interface, like so:

private void SomeFunction (object o)
{
IFoo f = (IFoo) o; //looking in the debugger, I can see that "o" is an
instance of "Foo"
}

(Tthere are actually 2 classes which implement the interface, and when the
time comes to do the cast, I don't know which class it is - hence my using
an interface :)

When this runs, I get an invalid cast exception. Any idea why?? In the
assembly info files, I have the version numbers fixed, so they don't change
on every build. Also, these are Compact Framework assemblies.

Are you sure IFoo isn't defined in other assemblies? That's usually the
problem with this kind of thing - see
http://www.pobox.com/~skeet/csharp/plugin.html
 

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