自动输出类的字段值实用代码分享

复制代码 代码如下:

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
                                    (p => p.Name + ":" + p.GetValue(obj).ToString()).ToArray()
                                );
        }
    }
}

你可能感兴趣的:(自动输出类的字段值实用代码分享)