C# 从“System.String”到“WebServiceWrapper.ItemMaster”的强制转换无效

C# 从“System.String”到“WebServiceWrapper.ItemMaster”的强制转换无效,c#,C#,我从“System.String”到“WebServiceWrapper.ItemMaster”的转换错误无效。这是我的代码: public class ItemMaster { public static ItemMaster loadFromReader(string oReader) { ItemMaster i = (ItemMaster)Convert.ChangeType(oReader, typeof(ItemMaster)); re

我从“System.String”到“WebServiceWrapper.ItemMaster”的转换错误无效。这是我的代码:

public class ItemMaster
{
    public static ItemMaster loadFromReader(string oReader)
    {
        ItemMaster i = (ItemMaster)Convert.ChangeType(oReader, typeof(ItemMaster));
        return i;
    }
}
如果ItemMaster上实现了显式运算符,则只能从字符串强制转换为ItemMaster,而ItemMaster可以执行此操作

请参见上的示例

编辑:隐式->显式,更新链接。

几点

你在谈论读卡器和字符串,好像它们是可以互换的。。。他们不是

您正在更改类型后进行铸造。。。这是多余的

除非ItemMaster定义了显式强制转换,否则无法将ItemMaster强制转换为字符串


你只是想要ItemMaster.ToString吗?

你能重新发布你的示例代码吗。谢谢。为什么你会认为从字符串到ItemMaster的转换类型会起作用呢?这里写的类型是ItemMaster我想从字符串转换到ItemMaster,不是另一种方式对,我倒过来了,但它仍然是多余的。。。调用ChangeType将字符串更改为ItemMaster,然后将结果转换为ItemMaster。您仍然需要对ItemMaster上定义的字符串进行隐式转换。我同意Pradeep的其余代码存在问题,但调用ChangeType后的显式转换不是其中之一。ChangeType返回对象;为什么不需要显式强制转换?只有在实现显式运算符时才需要强制转换。如果运算符是隐式的,则甚至不需要强制转换。当然,你是对的:误读了问题的用法要求。