WCF DataContracts and Polymorphism

戏子无情 提交于 2019-12-11 12:28:24

问题


Here's what I want to do. This should (theoretically) be very simple.

Say I have a WCF service with the following code (bare bones functionality):

<DataContract()>
Public Class BaseObj
    <DataMember()>
    Public ID As Integer
End Class


<DataContract()>
Public Class TestObj1
    Inherits BaseObj

    Public Sub New(ByVal idval As Integer)
        ID = idval
    End Sub

End Class


<DataContract()>
Public Class TestObj2
    Inherits BaseObj

    Public Sub New(ByVal idval As Integer)
        ID = idval
    End Sub

    <DataMember()>
    Public DoNotShow As String = "fail"

End Class

Here is the code I want to write:

<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, UriTemplate:="Test?reqReportID={reqReportID}")>
Public Function GetCollection(ByVal reqReportID As Integer) As List(Of BaseObj)

    Dim myObjs as New List(Of BaseObj)
    myObjs.add(new TestObj1(1))
    myObjs.add(new TestObj2(2))
    return myObjs

End Function

I want the JSON response to look exactly like this: [{"ID":1},{"ID":2}]

Now the code that I have currently returns an empty response (no error thrown, no information passed). I can get it to return something by doing this:

<DataContract(), KnownType(GetType(TestObj1)), KnownType(GetType(TestObj2))>
Public Class BaseObj
    <DataMember()>
    Public ID As Integer
End Class

However the response adds a "__type" to the JSON object, and also adds "DoNotShow" to the JSON object (not a good thing).

The problem here is that I do not want to pass information that is unique to each object. I only want information that is common to each object through the base class. Nothing I can do will change it, and unless I am missing something, it seems as though the authors of WCF had a very bassackwards view of OO programming when they created this.

Any insight you here at SO might have would be greatly appreciated.


回答1:


Out of curiosity, why does it matter? With the example you gave I'm not sure I'm following the why it's important.

One option I have off of the top of my head is to inherit TestObj2 from TestObj1. Then I would take off the knowntype(gettype(testobj2)).

If you want to dynamically add known types then you should call the datacontractjsonserializer yourself.

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

Edit: this will result in two TestObj1 objects on the client side. Not sure if you want that behavior.




回答2:


If your collection is only typed to BaseObj I don't think it's possible for the serializer to produce the JSON you are looking for.

If it were to simply produce [{"ID":1},{"ID":2}], there is no way for the recipient to distinguish between the types. How would the other end determine that the first object is of type TestObj1 but the second object is of type TestObj2?



来源:https://stackoverflow.com/questions/5641750/wcf-datacontracts-and-polymorphism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!