2
我很關心這是如何實際工作的,因爲我只是想確保我沒有誤認爲我的測試成功。Protobuf如何設置只讀屬性
從我讀過的其他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 ?
再次感謝您的澄清 – 2012-07-25 11:41:40