Invalid Cast Error

  • Thread starter Thread starter JB
  • Start date Start date
J

JB

I have written a simple ASP.NET page in C#, and on that page I'm
trying to cast an interface to a class, so I can call methods on the
object that are not part of that interface, but part of the object.
Trouble is, every time I do this, I get an invalid cast exception.
I've stepped through the code, and the GetType().GUID of both the
object I'm casting to, and the object I'm casting from is the same.

Has anyone ever seen this? There are sample everywhere in VB talking
about Option Strict, but that doesn't exist in C#.

Whats even stranger is that I can cut the code out, drop it in a
WinForm, and everything works fine.
 
Ok lets see if i get this straight. You are doing something like this:

private void MyMethod(IMyInterface obj)
{
MyObject casted=(MyObject)obj;
//Do something with the object
}

Does your MyObject class implement the interface IMyInterface?
Regardless of that you should test if it is possible to cast the obj
to MyObject with the is-operator.

if(obj is MyObject)
{
MyObject casted=(MyObject)interface;
//Do something with the object
}

/Hugo
 
Could you post the code that you're having trouble with?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Ok,

After some more due diligence, it turns out that the types are in fact
not equal. Here is the code sample

TSServiceLoader _serviceManager = new TSServiceLoader();
_serviceManager.UserLogin(userName, password);

ITSServiceClient service = _serviceManager.ServiceClient;

// I can then check the type of service, and it is in fact a tsAM
// and tsAM does implement ITSServiceClient

// THis is the line that fails
tsAM localAM = (tsAM)service

// The servcieManager dynamically loads the tsAM assembly(among
others), wheras
// localAM is defined in the project. If I look at the code base for
the two types, I find that they are different. One is being loaded
from System/win32, and the other is coming from MYWebProject\bin.
Hence the CLR doesn't think they're the same types even though the
DLLs are identical and just copies of one another.

So the question now is, how does the call to Assembly.LoadFrom()
determine the path that its going to use. Why isn't it using my run
directory?
 
I'm lost. I have no idea what namespaces and/or assemblies these are
referring to, or what this "tsAM" is (class, Interface, assembly, what?).

I DID notice that in your remarks, you said that a certain class "is in
fact" a tsAM, which I will assume you mean inherits tsAM. However, you said
that tsAM implements an interface called "ITSServiceClient" - but you're
trying to cast your "service" object as a tsAM, rather than
"ITSServiceClient". That could be your problem. Just because 2 classes
implement one of the same interfaces, it doesn't mean that you can cast one
as the other, if they don't have the same signature, or one is not inherited
from the other. You could cast either one as the interface itself.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top