Automatic output class field value utility code sharing

  • 2020-05-30 20:59:58
  • OfStack


using System;
using System.Linq;
using System.Reflection;
namespace LucienBao.Common
{
    public static class ToStringHelper
    {
        public static string ToString(object obj)
        {
            Type t = obj.GetType();
            FieldInfo[] fis = t.GetFields();
            return string.Join(Environment.NewLine,
                                fis.Select<FieldInfo, string>
                                    (p => p.Name + ":" + p.GetValue(obj).ToString()).ToArray()
                                );
        }
    }
}


Related articles: