自动输出类的字段值实用代码分享
本文导语: 代码如下: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[...
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()
);
}
}
}