我对这实际上是如何工作的很感兴趣,因为我只是想确保我没有弄错我的测试成功的事实。
从我读过的其他 stackoverflow 问题中,protobuf 无法设置只读属性。但它似乎做得很好。
我有以下课程(修剪以演示)
<
ProtoContract()
> _
Public Class WebOrder
Implements IWebOrder
Friend Sub New()
MyBase.New()
mItems = New Ordered.StockItemsCollection
end sub
Private mItems As Ordered.StockItemsCollection
<ProtoMember(4)>
Public ReadOnly Property Items As Ordered.StockItemsCollection
Get
Return mItems
End Get
End Property
Private ReadOnly Property COMItems As Ordered.StockItemsCollection Implements IWebOrder.Items
Get
Return mItems
End Get
End Property
end class
当我使用以下代码对其进行测试时,它是成功的。
Dim si As New WebConnector.Ordered.StockItem With {.ItemType = WebConnector.StockItemType.Instruction,
.Description = "test", .Quantity = 5}
activeOrder.Items.Add(si)
Using ms As New MemoryStream
ProtoBuf.Serializer.Serialize(Of WebConnector.WebOrder)(ms, activeOrder)
ms.Seek(0, SeekOrigin.Begin)
ch = ProtoBuf.Serializer.Deserialize(Of WebConnector.WebOrder)(ms)
End Using
For Each si In ch.Items
Debug.Print(si.ItemType.ToString & " --- " & si.Description & " --- x" & si.Quantity)
Next
结果:
说明 --- 测试 --- x5
protobuf 是进入我的财产并找到支持字段并设置它,还是只是.Add
针对集合或 StockItems?