'Newtonsoft.Json ContractResolver CreateProperty provides wrong ReflectedType on MemberInfo?

Using a custom ContractResolver based on DefaultContractResolver that overwrites CreateProperty I want to use the type of the object that is being serialized. This should be in the MemberInfo.ReflectedType property but Newtonsoft.Json seems to provide the base class instead:

    abstract class A
    {
        public string A1 { get; set; }

    }

    class B : A
    {
        public string B1 { get; set; }
    }

    class MyResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            Console.WriteLine($"{member}: {member.ReflectedType.Name}");
            return base.CreateProperty(member, memberSerialization);
        }
    }

When using the Serializer like so:

var token = JToken.FromObject(new B(), JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new MyResolver() }));

How do I get typeof(B) for both properties?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source