2010-08-31 31 views
0

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

public class ItemMaster 
{ 
    public static ItemMaster loadFromReader(string oReader) 
    { 
     ItemMaster i = (ItemMaster)Convert.ChangeType(oReader, typeof(ItemMaster)); 
     return i; 
    } 
} 
+1

你可以重新发布你的示例代码,因为那里没有任何意义。谢谢。 – ChrisBD 2010-08-31 15:47:41

+1

你为什么会认为从String到ItemMaster的ChangeType会起作用? – 2010-08-31 15:49:09

+0

这里写的类型是ItemMaster – Pradeep 2010-08-31 16:38:31

回答

0

只能从string转换为ItemMaster如果在ItemMaster实现一个明确的运营商,它做的工作。

查看MSDN

编辑例如:隐 - >明确,更新的链接。

+1

只有在执行* explicit *操作符时才需要强制转换。如果操作者是隐含的,那么演员甚至不是必需的。 – 2010-08-31 17:09:52

+0

当然你是对的:)误读问题的使用需求。 – JanW 2010-08-31 17:59:02

0

几个百分点......

你说的是读者和字符串作为如果他们互换......他们不是。

更改类型后您正在投射......这是多余的。

您不能投ItemMasterstring除非ItemMaster已明确转换定义。

你只需要ItemMaster.ToString()

+0

我想从字符串投射到ItemMaster,而不是其他方式 – Pradeep 2010-08-31 16:37:38

+0

对,我得到了这个,但它仍然是多余的......您调用'ChangeType'将字符串更改为'ItemMaster',然后投射结果作为'ItemMaster'。而且你仍然需要对'ItemMaster'上定义的字符串进行隐式转换。 – 2010-08-31 16:53:03

+1

我同意Pradeep的其余代码有问题,但调用ChangeType后的显式转换不是其中的一种。 ChangeType返回**对象**;为什么*不会*你需要明确的演员? – 2010-08-31 17:08:15

相关问题