Json convert object which inherit dictionary

后端 未结 2 1697
日久生厌
日久生厌 2021-01-07 04:36

I have following class definition:

public class ElasticObject : Dictionary
{
    public int Id { get;set;}
}

var keyValues = new Elast         


        
2条回答
  •  轮回少年
    2021-01-07 04:52

    I made the extra properties just poke data back in to the base dictionary instead. This way you get to expose a property like "Id" but during serialisation/de-serialisation it will just work with the underlying dictionary:

    public class ElasticObject : Dictionary
    {
        public int Id
        {
            get { 
                int val;
                if (int.TryParse(this["Id"] as string, out val))
                {
                    return val;
                }
    
                return -1;
            }
            set { this["Id"] = value; }
        }
    }
    

提交回复
热议问题