Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c# - Object of type "X" cannot be converted to object of type "X"

(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?

UPDATE I read somewhere that deleting all your resx files and rebuilding can help. I unthinkingly tried this. Not a good idea...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

SomeType foo = someObj.Foo;

would have the SomeType (on the left) from "A", with .Foo the SomeType from "B". Try ensuring that all your projects are using the same version of all the assemblies you rely on.

Another scenario is the same name in different namespaces, but that is a bit of an obvious one, and I suspect the 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...