在做控件的时候经常会遇到一个很烦人的问题,就是在测试工程中加入自己的控件,然后编辑属性的总是提示"Unable to cast object of type Xto type X"。看到这个问题,真是叫人哭笑不得啊。两个类名完全一样,何谓“无法转换” 。
既然出了问题,就得解决,一步一步跟吧。设好环境,调试控件的设计时行为,运行的结果竟然是完全没有问题。没有问题却成了最大的问题,无从下手了,上网查查吧,还好,发现很多人也都碰到这个问题。在微软的论坛里有一个解决的方法:
This particular problem apparently arises because of several factors:
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.
If you attach to a running instance of VS2005 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.
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.
自己动手实践一下,删掉了cache的汇集,重新运行VS2005,问题解决了。
既然出了问题,就得解决,一步一步跟吧。设好环境,调试控件的设计时行为,运行的结果竟然是完全没有问题。没有问题却成了最大的问题,无从下手了,上网查查吧,还好,发现很多人也都碰到这个问题。在微软的论坛里有一个解决的方法:
This particular problem apparently arises because of several factors:
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.
If you attach to a running instance of VS2005 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.
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.
自己动手实践一下,删掉了cache的汇集,重新运行VS2005,问题解决了。
本文转自纶巾客博客园博客,原文链接:http://www.cnblogs.com/guanjinke/archive/2006/12/04/581116.html,如需转载请自行联系原作者