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

后端 未结 8 1120
误落风尘
误落风尘 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:36

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

提交回复
热议问题