开发者

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...


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.


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.

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 ...


I found the following to be useful, as described here: Object of type 'Data.TimeLineChartedDay[]' cannot be converted to type 'Data.TimeLineChartedDay[]'?

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]


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.

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.

Hope this helps anyone else with the same issue!


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.

tl;dr

Try switching it off and on again.


The Types indicated may have same name, same namespace, and same Assembly (DLL) but on different versions or location.

You may try the following code on those two objects you are referring to define the location of their assemblies.

SampleObject.GetType().Assembly.CodeBase


I had the same error as you but for me removing the resx file worked.


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.

For example if the property witch produce the error is MyProp in a MyUserControl user control, you will find in resx file a data tag with the name MyUserControl.MyProp, remove the content of it's value tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜