Я не могу разыграть свои предметы.Я получаю: "Невозможно привести объект типа 'ClassA' к типу 'ClassB'".
Класс обслуживания:
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
InterFace:
<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'.
Может кто-нибудь помочь мне, я не знаю, что я делаю неправильно?