Skip to content

[NativeAOT] Data Contract Serializer #71350

Description

@AhmedZero

Description

in an older version of NativeAOT it works well, but when I upgrade NativeAOT to the last version. it doesn't work well, I don't know what's the reason if it's problem with .NET 7 or NativeAOT.

Reproduction Steps

using System.Drawing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace MyApp // Note: actual namespace depends on the project name.
{
  
    public unsafe class Program
    {
        [DataContract]
        public class DrawingProperties
        {
            [DataMember]
            public Color TestColor = Color.Red;

        }
        public static void Save(DrawingProperties obj, string path)
        {
            
                using FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DrawingProperties));

                serializer.WriteObject(stream, obj);

            
        }
        public static DrawingProperties Load(string path)
        {
            using FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DrawingProperties));

            return (DrawingProperties)serializer.ReadObject(stream);

        }
        public static DrawingProperties DrawProp = new();

        static void Main(string[] args)
        {
            Console.WriteLine($"R: {DrawProp.TestColor.R} G: {DrawProp.TestColor.G} B: {DrawProp.TestColor.B}");
            Save(DrawProp, "config.json");
            DrawProp = Load("config.json");
            Console.WriteLine($"R: {DrawProp.TestColor.R} G: {DrawProp.TestColor.G} B: {DrawProp.TestColor.B}");

            Console.ReadKey();
        }
 
    }
}

Expected behavior

R: 255 G: 0 B: 0
R: 255 G: 0 B: 0

Actual behavior

R: 255 G: 0 B: 0
R: 0 G: 0 B: 0

Regression?

No response

Known Workarounds

No response

Configuration

.NET NativeAOT 7.0.0-preview.6.22327.1
Windows 11 X64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions