DataContractSerializer Not Serializing Properties with Private Setters in .NET 6
I'm trying to debug I'm confused about I'm writing unit tests and I've tried everything I can think of but I've encountered a strange issue with I'm having trouble with the `DataContractSerializer` in .NET 6 when trying to serialize an object that contains properties with private setters..... I expected the serializer to include these properties in the output, but it seems to ignore them altogether. Here's a simplified version of my code: ```csharp [DataContract] public class User { [DataMember] public string Name { get; private set; } [DataMember] public int Age { get; private set; } public User(string name, int age) { Name = name; Age = age; } } var user = new User("John Doe", 30); var serializer = new DataContractSerializer(typeof(User)); using (var stream = new MemoryStream()) { serializer.WriteObject(stream, user); var result = Encoding.UTF8.GetString(stream.ToArray()); Console.WriteLine(result); } ``` When I run this code, the output is an empty XML document. I've also tried changing the access modifier of the setters to public, and that worked, but it's not ideal for my design as I want to keep those setters private. I've read that `DataContractSerializer` should serialize properties with private setters if they are marked with `[DataMember]`. Is there a way to configure `DataContractSerializer` to include properties with private setters, or am I missing something in my implementation? Should I consider using an alternative serializer like JSON.NET for this case? For context: I'm using C# on Windows. For context: I'm using C# on Linux. Any help would be greatly appreciated! This is for a CLI tool running on macOS. This is my first time working with C# latest. I'd be grateful for any help. This is my first time working with C# stable. I'd be grateful for any help. I'm using C# latest in this project. Has anyone dealt with something similar?