开发者

Invalid cast from 'System.String' to 'WebServiceWrapper.ItemMaster'

I am getting an invalid cast error from 'System.String' to 'WebServiceWrapper.ItemMaster'. This is my code:

public class ItemMaster
{
    publi开发者_如何学Cc static ItemMaster loadFromReader(string oReader)
    {
        ItemMaster i = (ItemMaster)Convert.ChangeType(oReader, typeof(ItemMaster));
        return i;
    }
}


A couple of points...

You're talking about readers and strings as if they're interchangeable... they're not.

You are casting after changing the type... this is redundant.

You can't cast ItemMaster to string unless ItemMaster has an explicit cast defined.

Do you simply want ItemMaster.ToString()?


You can only cast from string to ItemMaster if there is an explicit operator implemented on ItemMaster, which does the work.

See the example on MSDN

Edit: implicit -> explicit, updated link.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜