visual-studio “X”类型的对象不能转换为“X”类型的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2115593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object of type "X" cannot be converted to object of type "X"
提问by Benjol
(Can't believe this hasn't already been asked, but I can't find a dup)
(不敢相信这还没有被问过,但我找不到重复的东西)
In Visual Studio with lots of projects, when I first open the solution, I sometimes get the warning Object of type "X" cannot be converted to object of type "X". Generally rebuilding seems to make it go away, but does anyone know what this is caused by, and how to avoid it?
在包含大量项目的 Visual Studio 中,当我第一次打开解决方案时,有时会收到警告Object of type "X" cannot be converted to object of type "X". 一般重建似乎使它消失,但有谁知道这是由什么引起的,以及如何避免它?
UPDATEI read somewhere that deleting all your resx files and rebuilding can help. I unthinkingly tried this. Not a good idea...
更新我在某处读到删除所有 resx 文件并重建会有所帮助。我不假思索地尝试了这个。不是个好主意...
采纳答案by Marc Gravell
This would really depend on the exact scenario (that is vague), but the most likely cause would be different assembly references / versions. Perhaps you have some "top level" code that references version "A" of a dll, and references a library which references version "B" of a similar dll; then:
这实际上取决于确切的场景(即含糊不清),但最可能的原因是不同的程序集引用/版本。也许您有一些引用 dll 版本“A”的“顶级”代码,并引用引用类似 dll 版本“B”的库;然后:
SomeType foo = someObj.Foo;
would have the SomeType(on the left) from "A", with .Foothe SomeTypefrom "B". Try ensuring that all your projects are using the same version of all the assemblies you rely on.
将具有SomeType(在左侧)从“A”,具有.Foo的SomeType从“B”。尝试确保您的所有项目都使用您所依赖的所有程序集的相同版本。
Another scenario is the same name in different namespaces, but that is a bit of an obvious one, and I suspectthe error message would make this obvious?
另一种情况是在不同的命名空间中使用相同的名称,但这有点明显,我怀疑错误消息会使这一点变得明显吗?
There are some other scenarios where types with the same names in the same namespace (but different assemblies) conflict with eachother; here "extern aliases" can help, but are a complete PITA to work with.
还有一些其他场景,同一命名空间(但不同程序集)中具有相同名称的类型相互冲突;这里“extern aliases”可以提供帮助,但它是一个完整的 PITA 可以使用。
回答by Brian
If you have a user control that exposes an object with a getter/setter, you are going to have a bad time (as they say). It is, in fact, the setter that is the problem.
如果您的用户控件使用 getter/setter 公开对象,那么您将度过一段糟糕的时光(正如他们所说)。事实上,问题在于二传手。
SO to fix it, instead of doing this:
所以要修复它,而不是这样做:
public ObjectX
{
get { return _objx; }
set
{
_objx=value;
//do some other stuff
}
}
You could do something along the lines of this:
你可以做一些类似的事情:
public ObjectX
{
get { return _objx; }
}
public void SetObjectX(ObjectX inVal)
{
_objx = inVal;
//do some other stuff
}
... which will prevent the Visual Designer from trying to assign a serialized version of ObjectX to your control at design time, which is really what was going on ...
...这将阻止可视化设计器在设计时尝试将 ObjectX 的序列化版本分配给您的控件,这确实是正在发生的事情......
回答by Carl Niedner
I found the following to be useful, as described here: Object of type 'Data.TimeLineChartedDay[]' cannot be converted to type 'Data.TimeLineChartedDay[]'?
我发现以下内容很有用,如下所述:“Data.TimeLineChartedDay[]”类型的对象无法转换为“Data.TimeLineChartedDay[]”类型?
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
回答by Paul McLean
I came across the same problem, and while Marc's answer did help me to understand what was going wrong, it did not solve my problem. Rebuilding did not help, and I was not using serialization in this case.
我遇到了同样的问题,虽然 Marc 的回答确实帮助我了解出了什么问题,但并没有解决我的问题。重建没有帮助,在这种情况下我没有使用序列化。
Turns out my problem was happening because my project had a reference to itself (not quite sure how that happened, perhaps Resharper added the reference without me noticing?), and it was comparing the .cs file with the compiled .dll file. I simply removed the reference.
结果我的问题发生了,因为我的项目有一个对自身的引用(不太确定这是怎么发生的,也许 Resharper 在我没有注意到的情况下添加了引用?),并且它正在将 .cs 文件与编译的 .dll 文件进行比较。我只是删除了参考。
Hope this helps anyone else with the same issue!
希望这可以帮助其他有同样问题的人!
回答by Back2WorkMinion
The Types indicated may have same name, same namespace, and same Assembly (DLL) but on different versions or location.
指示的类型可能具有相同的名称、相同的命名空间和相同的程序集 (DLL),但位于不同的版本或位置。
You may try the following code on those two objects you are referring to define the location of their assemblies.
您可以在您所指的这两个对象上尝试以下代码来定义其程序集的位置。
SampleObject.GetType().Assembly.CodeBase
回答by ELKALAKHI Mohammed
I had the same error, I tried some solutions above but nothing worked for me, then I removed the content of the value tag of the data tag of my property in resx file.
我遇到了同样的错误,我尝试了上面的一些解决方案,但没有任何效果,然后我删除了resx file.
For example if the property witch produce the error is MyPropin a MyUserControluser control, you will find in resx filea data tag with the name MyUserControl.MyProp, remove the content of it's value tag.
例如,如果产生错误的属性MyProp在MyUserControl用户控件中,您将在resx file具有 name 的数据标记中找到MyUserControl.MyProp,删除其 value 标记的内容。
回答by Stefania Mereut
I had the same error as you but for me removing the resx file worked.
我和你有同样的错误,但对我来说删除 resx 文件有效。
回答by GJKH
After trying all of the above, cleaning, rebuilding, removing/re-adding references and editing the designer file - in the end simply closing visual studio and re-opening the project made everything better.
在尝试了上述所有操作后,清理、重建、删除/重新添加引用和编辑设计器文件 - 最后只需关闭 Visual Studio 并重新打开项目,一切都会变得更好。
tl;dr
tl;博士
Try switching it off and on again.
尝试将其关闭并重新打开。

