DCSIMG

 Subscribe in a reader

Unable to cast object from type 'x' to type 'x' - Guy kolbis
Sunday, June 29, 2008 8:51 PM kolbis

Unable to cast object from type 'x' to type 'x'

Here is a problem I encountered while working with Visual Studio 2005 and 2008....

The last time this happened to me was while working on TFS custom control. So in order to create a custom control, we must add the assembly we have created and a wicc file to the following folder:

"C:\Documents and Settings\All Users\Application Data\Microsoft\Team Foundation\Work Item Tracking\Custom Controls\9.0"

This is the path where TFS (Team Explorer) will look at when using custom controls.

When I deployed a new version of the custom control to that location I suddenly started receiving the InvalidCastException telling me that it was:

"Unable to cast object of type 'x' to object of type 'x'"

So, what happened here? The common sense tells us that this should be a version issue.

What did I find out?

If you attach to a running instance of VS2005/8 and check what Modules are loaded (you may need to add the Modules window command from the Debug category in Tools --> Customize...) you'll probably see two versions of your assembly are loaded, or even the same version of the assembly loaded twice: in both cases you'll see the same error message.

Why does it happen?

The IDE uses cache to store the assemblies for its projects. The IDE caches versions of assemblies in the following folder:

"x\Documents and Settings\user\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies"

And at least in my case doesn't detect the particular assembly containing the type has a new version and it doesn't update the cache.

How do I solve it?

So first step, delete all folders in ProjectAssemblies. Next, make sure the IDE can only find ONE copy of your assembly, which is easier if you only have one copy of the DLL in your disk, and only one reference to it (or exactly the same reference from all projects) in your solution.

תגים:, , ,

תוכן התגובה

# re: Unable to cast object from type 'x' to type 'x'

Shai Raiten כתב/ה

Hello Guy,

I had the same problem,

If you are working on custom control for 2005 change all References version to 8.0 and build again.

For 2008 is 9.0

This will word as well.

Sunday, June 29, 2008 11:09 PM

# re: Unable to cast object from type 'x' to type 'x'

PAUL VARGHESE כתב/ה

HEllo Guy ,

  i have an error

Error of creating a rad control in VS 2005

Tuesday, June 23, 2009 5:47 PM

שלח תגובה

(שדה חובה) 
(שדה חובה) 
(אופציונלי)
(שדה חובה) 

Enter the numbers above: