0
我无法施放我的物体。我得到:“无法投射'ClassA'类型的对象来键入'ClassB'”。施法错误wcf
服务类:
Public Class svc_Insp
Implements Isvc_Insp
Public Function Test(ByVal pm_income As ClassC) As String Implements Isvc_Insp.Test
Dim lv_retVal As String
Try
For Each item As Object In pm_income.Items
Try
Logger.Log(item)
Dim lv_Item As ClassB= CType(item, ClassB)
Catch ex As Exception
Logger.Log(ex.Message)
lv_retVal = ex.Message
End Try
Next
Catch ex As Exception
lv_retVal = ex.Message
End Try
Return lv_retVal
End Function Logger.Log(ex)
End Class
接口:
<ServiceContract()> _
Public Interface Isvc_Insp
<OperationContract()> _
<WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Test")> _
Function Test(ByVal pm_c As ClassC) As String
End Interface
而我三类:
<DataContract(), KnownType(GetType(ClassB)), KnownType(GetType(ClassC)), KnownType(GetType(List(Of ClassA)))> _
Public Class ClassA
Private _Name As String
<DataMember()> _
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Age As Integer
<DataMember()> _
Public Property Age() As Integer
Get
Return _Age
End Get
Set(ByVal value As Integer)
_Age = value
End Set
End Property
End Class
<DataContract()> _
Public Class ClassB
Inherits ClassA
Private _LastName As String
<DataMember()> _
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
End Class
<DataContract()> _
Public Class ClassC
Private _Items As List(Of ClassA)
<DataMember()> _
Public Property Items() As List(Of ClassA)
Get
Return _Items
End Get
Set(ByVal value As List(Of ClassA))
_Items = value
End Set
End Property
End Class
JSON对象我送:
{
"Items": [ {
"__type": "ClassB:#",
"LastName": "Power",
"Name": "David",
"Age": "30"
},
{
"__type": "ClassA:#",
"Name": "Dave",
"Age": "20"
},
{
"__type": "ClassB:#",
"LastName": "Bullet",
"Name": "Chris",
"Age": "40"
}
]
}
每次我寄这封信给我收到以下错误铸造服务器:
24-05-2011 16:36:57 - Unable to cast object of type 'ClassA' to type 'ClassB'.
24-05-2011 16:36:57 - ClassA
24-05-2011 16:36:57 - Unable to cast object of type 'ClassA' to type 'ClassB'.
24-05-2011 16:36:57 - ClassA
24-05-2011 16:36:57 - Unable to cast object of type 'ClassA' to type 'ClassB'.
能有人帮我,我不知道我做错了吗?
我看到了问题。我将Items List更改为ClassA列表,但演员仍然失败。 – Dave 2011-05-24 18:56:30