Results 1 to 3 of 3

Thread: How does System.Runtime.Serialization update read-only properties?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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:
    1. Imports System.Runtime.Serialization
    2.  
    3. ''' <summary>
    4. ''' An event to indicate that an FX rate was priced between two currencies
    5. ''' </summary>
    6. <DataContract()>
    7. Public Class FXRatePricedEvent
    8.     Inherits AggregateIdentity.CurrencyExchangeAggregateIdentity
    9.     Implements IEvent(Of IAggregateIdentity)
    10.  
    11.     <DataMember(Name:="Rate")>
    12.     ReadOnly m_rate As Decimal
    13.     <DataMember(Name:="PriceDate")>
    14.     ReadOnly m_pricedate As Date
    15.     <DataMember(Name:="IsDerived")>
    16.     ReadOnly m_derived As Boolean
    17.  
    18.  
    19.     ''' <summary>
    20.     ''' The conversion rate between source and target currencies
    21.     ''' </summary>
    22.     ReadOnly Property Rate As Decimal
    23.         Get
    24.             Return m_rate
    25.         End Get
    26.     End Property
    27.  
    28.     ''' <summary>
    29.     ''' The date/time the FX rate was priced
    30.     ''' </summary>
    31.     ReadOnly Property PriceDate As Date
    32.         Get
    33.             Return m_pricedate
    34.         End Get
    35.     End Property
    36.  
    37.     ''' <summary>
    38.     ''' Is this rate derived from triangulation or reversal of a priced rate
    39.     ''' </summary>
    40.     ''' <remarks>
    41.     ''' For example a derived GBPJPY can exist s if a priced GBPUSD and USDJPY exist
    42.     ''' </remarks>
    43.     ReadOnly Property IsDerived As Boolean
    44.         Get
    45.             Return m_derived
    46.         End Get
    47.     End Property
    48.  
    49.     Public Sub New(ByVal currencyFrom As AggregateIdentity.CurrencyAggregateIdentity.ISO_3Digit_Currency,
    50.                    ByVal currencyTo As AggregateIdentity.CurrencyAggregateIdentity.ISO_3Digit_Currency,
    51.                    ByVal fxRate As Decimal,
    52.                    ByVal rateDate As Date,
    53.                    ByVal derived As Boolean)
    54.         ' Set the FX rate aggregate identifier
    55.         MyBase.New(currencyFrom, currencyTo)
    56.         ' and set the other properties
    57.         m_rate = fxRate
    58.         m_pricedate = rateDate
    59.         m_derived = derived
    60.     End Sub
    61.  
    62. 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?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: How does System.Runtime.Serialization update read-only properties?

    Quote Originally Posted by Merrion View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width