wcf 转换错误

发布于 2024-11-09 07:08:05 字数 2911 浏览 0 评论 0原文

我无法投射我的物体。我得到:“无法将‘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

接口:

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

有人可以帮助我吗?我不知道我做错了什么?

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) 
    End 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-11-16 07:08:05

ClassC 是 ClassB 的列表,这就是它尝试将 ClassA 转换为 ClassB 的原因。

ClassB继承了ClassA,因此ClassB拥有ClassA所需的所有信息。但是 ClassA 没有 ClassB 中的所有信息,因此转换失败。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文