Skip to content

BinaryFormatter in deep copy is deprecated because it contains security vulnerabilities #6

Open

Description

@azrng

Whether to consider replacement, official original: Information: https://learn.microsoft.com/zh-cn/dotnet/standard/serialization/binaryformatter-security-guide#binaryformatter-security- vulnerabilities
Controls

Activity

  1. azrng commented on Sep 10, 2023

    @azrng
    Author

    example

    public static class CloneExtensions
    {
        public static T DeepCopy<T>(this T obj)
            where T : class
        {
            using var ms = new MemoryStream();
            var options = new JsonSerializerOptions
            {
                ReferenceHandler = ReferenceHandler.Preserve,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };
            JsonSerializer.SerializeAsync(ms, obj, options);
            ms.Position = 0;
            return JsonSerializer.Deserialize<T>(ms, options);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions