Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 994131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:31:56+00:00 2026-05-16T06:31:56+00:00

I’ve got a class that has a read-only property defined that is actually a

  • 0

I’ve got a class that has a read-only property defined that is actually a reference to a very mutable object, and I’m wondering what the best way to handle the serialization of it is.

For example:

public class classA {
  public readonly classB B = new classB();
}

public class classB {
  public string Name = "Test";
}

This can be serialized to (if it wasn’t for the fact the ‘B’ field is readonly)

<classA>
  <B>
    <Name>Test</Name>
  </B>
</classA>

but I can’t deserialize it, as the serializer would attempt to create a new instance of classB, and set the field on classA to that new object.

If I make the property not read-only, this opens up the possibility of client code changing the instance of classB that’s used, which isn’t acceptable (many other objects will have sunk events on that instance, so it can’t just be replaced like this). Alternatively, I could have the setter simply copy the values from the object given into the instance that’s already there, but this seems excessively cumbersome just to accommodate the serializer.

I considered having a separate property with a setter to handle the serialization, and setting it to [Browsable(BrowsableState.Never)], but this still exposes the ability to change the instance to anyone who gets clever with reflection.

Is there a way of telling the serializer to populate the existing object on deserialization, or am I going to have to create a custom serializer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:31:56+00:00Added an answer on May 16, 2026 at 6:31 am

    Well, IXmlSerializable is an option, but it is not a desirable one; it is pretty hard to write a robust implementation of anything non-trivial.

    If having an immutable property is that important, then maybe consider having a separate DTO implementation – i.e. one set of simple, mutable classes for persistence, which you translate into your domain entities.

    Alternatively, some other serializers may help; for example, DataContractSerializer will work on private fields:

    [DataContract]
    public class classA {
        [DataMember]
        private classB b = new classB();
    
        public classB B { get {return b; } }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.