Why can't Json.NET serialize X509Certificate2?

Whenever I try to serialize an X509Certificate2 instance with Json.NET (without using its implementation of ISerializable, but because of ignoring it), Json.NET throws an exception.

Exception message: “A member named CertContext already exists in System.Security.Cryptography.X509Certificates.X509Certificate2. Use JsonPropertyAttribute to specify a different name.

I wrote a program that reproduces it:

using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

internal class Program
{
    private static void Main(string[] args)
    {
        var resolver = new DefaultContractResolver
        {
            IgnoreSerializableInterface = true,
            DefaultMembersSearchFlags =
                BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.GetProperty
        };

        JsonConvert.SerializeObject(new X509Certificate2(), new JsonSerializerSettings {ContractResolver = resolver});
    }
}

After studying, I noticed that X509Certificate2 implements a property called "CertContext" that hides a method with the same name in the X509Certificate base class. How can I tell Json.NET to just take the most derived property, as is usually the case?

+3

All Articles