开发者

Casting error wcf

I can't cast my objects. I get: "Unable to cast object of type 'ClassA' to type 'ClassB'".

The service Class:

 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) 
    En开发者_运维问答d Class

The 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

And my three Classes:

<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

The json object I'm sending:

{ 
 "Items": [  {
   "__type": "ClassB:#",
   "LastName": "Power",
   "Name": "David",   
   "Age": "30"
  },
  {
   "__type": "ClassA:#",
   "Name": "Dave",
   "Age": "20"
  },
  {
   "__type": "ClassB:#",
   "LastName": "Bullet",
   "Name": "Chris",
   "Age": "40"
  }
 ]
}

Everytime I send this to the server I get the following casting error:

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'.

Can someone please help me I don't know what I'm doing wrong?


ClassC is a list of ClassB, that is why it is trying to cast ClassA to ClassB.

ClassB inherits ClassA, therefore ClassB has all information required by ClassA. But ClassA does not have all the information in ClassB, therefore the cast fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜