在我的'MyOtherViewModel
'内,我有以下代码
投掷错误:Argument Type myStoredProcedure not Assignable to parameter type MyDbTable
抛出@ (this, c)
编译时错误
var query = myWCFClient.myStoredProcedure();
//Some Other Stuff
query.ToList().ForEach(c => myCollection.Add(new MyViewModel(this, c)));
内部MyViewModel
我有:
public MyViewModel(MyOtherViewModel owner, MyDbTable table)
:base(owner, "String")
{
//other stuff
}
然后我尝试
var t = ((IEnumerable)query).Cast<MyDbTable>().ToList();
t.ToList().ForEach(c => mCollection.Add(new MyViewModel(this, c)));
获取:Unable to cast object of type 'myStoredProcedure' to type 'MyDbTable'.
运行时错误
使用WCF在VS 2012
,c#
.Net 4.5
中处理此问题的最佳方法是什么?
程序为myStoredProcedure[]
,这将返回0
var t = query.OfType<MyDbTable>().ToList();
答案 0 :(得分:3)
在第一段代码中,您将myStoredProcedure
类型的对象放在MyViewModel
的构造函数中,而该构造函数需要类型为MyDbTable
的对象。这就是为什么你在这段代码上得到编译时错误。
您尝试将myStoredProcedure
投射到MyDbTable
的第二段代码。这就是为什么你在这段代码上得到运行时异常的原因。
您的WCF服务正在返回超出预期的其他对象。调查服务或更改MyViewModel
的构造函数以接受类myStoredProcedure
的对象。