Object of type “X” cannot be converted to object of type “X”

后端 未结 8 1100
误落风尘
误落风尘 2020-12-09 16:10

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

8条回答
  •  春和景丽
    2020-12-09 16:30

    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.

提交回复
热议问题