System.InvalidCastException: Unable to cast object of type ‘x’ to type ‘x’

Visual Studio Visualizers VS2005 (not sure about VS2008)

This posting covers the following exception that can be thrown while debugging or running a visualizer during development:

Microsoft.VisualStudio.DebuggerVisualizers. DebugViewerShim.RemoteObjectSourceException: Unable to cast object of type ‘x’ to type ‘x’ System.InvalidCastException: Unable to cast object of type ‘x’ to type ‘x’. 

Background 

While developing Visual Studio Visualizers there are a number of ways to set up your visualizer solution. 

For the purpose of this post, this developer has a solution with two projects, the visualizer project and the test project. 

There are two ways to launch the visualizer.  The first method allows you to debug the visualizer.  The second just runs the visualizer.

  1. Developer can launch the visualizer by using VisualizerDevelopmentHost class.  This is how you actually debug the visualizer.  You can read in detail how to do this here.
  2. Developer can place breakpoints within the test project’s code and launch the visualizer using the right click method or from one of the variable watch windows when the breakpoint is reached.

Debugging Visualizer

What I’m about to share is a true mess that cost me hours to figure out.

When you set up your solution as I have discussed above, if you want to actually debug your visualizer using the VisualizerDevelopmentHost class, you MUST place a reference in the test project to the visualizer project and you MUST set the property “Copy Local” = True.  If you don’t the visualizer will never launch and you won’t be able to debug it.  In fact, Visual Studio 2005 won’t stop and any breakpoint in any code.

Running Visualizer

Now, if you set you project up to “Copy Local” = True and you simply run the visualizer using method one above, you can get the following exception during a casting operation after the debugger side and the debuggee sidee have exchanged data:

Microsoft.VisualStudio.DebuggerVisualizers. DebugViewerShim.RemoteObjectSourceException: Unable to cast object of type ‘x’ to type ‘x’ System.InvalidCastException: Unable to cast object of type ‘x’ to type ‘x’.

In order to run your visualizer you MUST change the reference to VisualizerDevelopmentHost and set the property “Copy Local” = False.

Details

Here is the best I can figure.  In the second scenario, two instances of your visualizer are being loaded, debugger side and debuggie side.  On top of that if you specified “Copy Local” = True, one of the copies is actually different from one of the copies that Visual Studio loads.  If you specifiy “Copy Local” = False then the same copy is being loaded by Visual Studio and you won’t get the exception.

Closing

You can read about debugging visualizers in this article on Code Project: http://www.codeproject.com/useritems/MoleForWPF.asp

Just a grain of sand on the worlds beaches.

Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 241 other followers