Dynamic override of ToString() using Reflection

后端 未结 6 782
不知归路
不知归路 2021-01-04 14:12

I generally override the ToString() method to output the property names and the values associated to them. I got a bit tired of writing these by hand so I\'m looking for a d

6条回答
  •  孤街浪徒
    2021-01-04 14:31

    I would use JSON, Serializer will do all the hard work for you:

        public static class ObjectExtensions
        {
            public static string ToStringEx(this object obj)
            {
                return JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true });
            }
        }
    

提交回复
热议问题