How does System.Runtime.Serialization update read-only properties?
If I have a class with read-only properties that I only want to be created at constructor time e.g.:-
vb.net Code:
Imports System.Runtime.Serialization
''' <summary>
''' An event to indicate that an FX rate was priced between two currencies
''' </summary>
<DataContract()>
Public Class FXRatePricedEvent
Inherits AggregateIdentity.CurrencyExchangeAggregateIdentity
Implements IEvent(Of IAggregateIdentity)
<DataMember(Name:="Rate")>
ReadOnly m_rate As Decimal
<DataMember(Name:="PriceDate")>
ReadOnly m_pricedate As Date
<DataMember(Name:="IsDerived")>
ReadOnly m_derived As Boolean
''' <summary>
''' The conversion rate between source and target currencies
''' </summary>
ReadOnly Property Rate As Decimal
Get
Return m_rate
End Get
End Property
''' <summary>
''' The date/time the FX rate was priced
''' </summary>
ReadOnly Property PriceDate As Date
Get
Return m_pricedate
End Get
End Property
''' <summary>
''' Is this rate derived from triangulation or reversal of a priced rate
''' </summary>
''' <remarks>
''' For example a derived GBPJPY can exist s if a priced GBPUSD and USDJPY exist
''' </remarks>
ReadOnly Property IsDerived As Boolean
Get
Return m_derived
End Get
End Property
Public Sub New(ByVal currencyFrom As AggregateIdentity.CurrencyAggregateIdentity.ISO_3Digit_Currency,
ByVal currencyTo As AggregateIdentity.CurrencyAggregateIdentity.ISO_3Digit_Currency,
ByVal fxRate As Decimal,
ByVal rateDate As Date,
ByVal derived As Boolean)
' Set the FX rate aggregate identifier
MyBase.New(currencyFrom, currencyTo)
' and set the other properties
m_rate = fxRate
m_pricedate = rateDate
m_derived = derived
End Sub
End Class
Then System.Runtime.Serialization can serialise this to/from XML with no problem. However I want to do something similar but to a key-value pair dictionary....my attempts so to do hit a brick wall unless the class has a constructor with no parameters.
Any ideas how I (or they) get around this?
Re: How does System.Runtime.Serialization update read-only properties?
Quote:
Originally Posted by
Merrion
However I want to do something similar but to a key-value pair dictionary.
Can you give some details on what you mean by this ? I ask because people generally don't care how an object is serialized, only that we can serialize and deserialize it correctly.
Re: How does System.Runtime.Serialization update read-only properties?
You could try this and see if it works for you:
Code:
Option Strict On
Option Explicit On
Imports System.ComponentModel
Public Class Class1
Private dic As Dictionary(Of String, String)
<Xml.Serialization.XmlIgnore()> _
Public Property Dictionary() As Dictionary(Of String, String)
Get
Return dic
End Get
Set(ByVal value As Dictionary(Of String, String))
dic = value
End Set
End Property
Private key() As String
<Browsable(False)> _
Public Property Keys() As String()
Get
Return key
End Get
Set(ByVal value() As String)
key = value
End Set
End Property
Private mValue() As String
<Browsable(False)> _
Public Property Values() As String()
Get
Return mValue
End Get
Set(ByVal value() As String)
mValue = value
End Set
End Property
Public Event dictionaryChanged As Action
Private Sub Class1_dictionaryChanged() Handles Me.dictionaryChanged
ReDim key(dic.Count - 1)
ReDim mValue(dic.Count - 1)
For i As Integer = 0 To dic.Count - 1
key(i) = dic.Keys(i)
mValue(i) = dic.Values(i)
Next
End Sub
Sub New()
For i As Integer = 0 To mValue.Length
dic.Add(key(i), mValue(i))
Next
End Sub
End Class