Serialize only interface properties to JSON with Json.net

前端 未结 9 1969
孤街浪徒
孤街浪徒 2020-12-08 19:50

With a simple class/interface like this

public interface IThing
{
    string Name { get; set; }
}

public class Thing : IThing
{
    public int Id { get; se         


        
9条回答
  •  悲哀的现实
    2020-12-08 20:17

    Inspired by @user3161686, here's a small modification to InterfaceContractResolver:

    public class InterfaceContractResolver : DefaultContractResolver where TInterface : class
    {
        protected override IList CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            IList properties = base.CreateProperties(typeof(TInterface), memberSerialization);
            return properties;
        }
    }
    

提交回复
热议问题