Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'.

M

mypetrock

Has anyone run into this error message?

Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'.

I'm trying to cast an object of type Foo.Bar that I got out of a hash
table into a variable of type Foo.Bar.

Foo.Bar data = (For.Bar)input["Data"];

Thanks,

mypetrock
 
K

Karsten Schramm

mypetrock said:
Foo.Bar data = (For.Bar)input["Data"];

'Cos Foo.Bar is not For.Bar.

Just kidding

What happens if you code

if(input["Data"] is Foo.Bar)
{
System.Diagnostics.Writeline("Should be castable");
}
else
{
System.Diagnostics.Writeline(typeof(Foo.Bar).FullName);
System.Diagnostics.Writeline((input["Data"]).GetType().FullName);
}

?
 
B

Barry Kelly

mypetrock said:
Has anyone run into this error message?

Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'.

I'm trying to cast an object of type Foo.Bar that I got out of a hash
table into a variable of type Foo.Bar.

Foo.Bar data = (For.Bar)input["Data"];

You may have two different Foo.Bar's in different assemblies. Perhaps
you have the same source code file included in both?

-- Barry
 
D

David Browne

Barry Kelly said:
mypetrock said:
Has anyone run into this error message?

Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'.

I'm trying to cast an object of type Foo.Bar that I got out of a hash
table into a variable of type Foo.Bar.

Foo.Bar data = (For.Bar)input["Data"];

You may have two different Foo.Bar's in different assemblies. Perhaps
you have the same source code file included in both?

Or side-by-side assemblies in the GAC, or multiple Assembly.LoadFrom?

David
 
M

Michael C

David Browne said:
Or side-by-side assemblies in the GAC, or multiple Assembly.LoadFrom?

Surely the 1 line of code wouldn't get it's reference from 2 different
places though.

Michael
 
A

Adam Clauss

I've had errors like this occur while working in ASP.NET. It makes for a rather humorous error message...
I forget exactly what the situation was... the error started happening, and then it just went away on it's own... I think it was
something to do w/ accessing the Master Page from the actual page itself, or vice-versa. In either case, there was definately NOT
two declarations of the class in my code, and the cast most definately should have worked. It was like ASP.NET decided to recompile
"part" of the application, while leaving the rest of it in-memory or something. And apparently that class got recompiled, so when
it tried to access it, all of a sudden the types didn't match.
 
J

Jon Skeet [C# MVP]

Michael said:
Surely the 1 line of code wouldn't get it's reference from 2 different
places though.

The two Foo.Bars in the message are the type it's trying to cast to,
and the runtime type of the object. The object is not being created in
the line of code we're looking at. The two Foo.Bars in the line are
indeed the same, but it's not a mismatch there that is causing the
problem.

Jon
 
M

Marc Gravell

Surely the 1 line of code wouldn't get it's reference from 2 different
places though
Thankfully not; the exception (runtime, I take it) is likely purely on the
RHS of this expression - i.e. in the cast: (For.Bar)input["Data"];

So it is possible that the bit of code that injects the object into input
with key "Data" is looking at a different Foo.Bar; unfortunately this line
is not shown... I would have to go with Karsten here... when it barfs, find
out what the full names of input["Data"] and typeof(Foo.Bar) are...

Marc
 
M

mypetrock

Here's what I've found out so far -

*The compile time type and the run-time type have different assemblies.

*Despite the fact that I reference the Foo.Bar project from my WebSite
project, VS copies the Foo.Bar.dll to the WebSite/Bin folder where the
run-time library grabs the code from.
*Creating an explict reference to the dll created by the Foo.Bar
project does not solve the issue as the WebSite project creates a
Foo.Bar.refresh reference in the WebSite/Bin folder that it uses for
its run-time type.

I could change my build from Foo.Bar to create its dll in WebSite/Bin,
but that's a hack. Does anyone have any other thoughts?

mypetrock
 
M

Michael C

Jon Skeet said:
The two Foo.Bars in the message are the type it's trying to cast to,
and the runtime type of the object. The object is not being created in
the line of code we're looking at.

That's true but if it's a compile time error it is comparing the Foo.Bar on
the right to the one on the left and basing the error on that. I was
presuming a compile time error which I guess it might not be.
The two Foo.Bars in the line are
indeed the same, but it's not a mismatch there that is causing the
problem.

Then it's likely that input["Data"]; isn't returning a foo.bar object.

Michael
 
J

Jon Skeet [C# MVP]

Michael C said:
That's true but if it's a compile time error it is comparing the Foo.Bar on
the right to the one on the left and basing the error on that. I was
presuming a compile time error which I guess it might not be.

Ah - I'd been assuming it was a runtime error. It's certainly the
message you get at runtime from an InvalidCastException.
The two Foo.Bars in the line are
indeed the same, but it's not a mismatch there that is causing the
problem.

Then it's likely that input["Data"]; isn't returning a foo.bar object.

No, it's returning a Foo.Bar object, but a different Foo.Bar - one
loaded from a different assembly to the one used for casting.
 

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