C# 使用反射 遍历输出 对象的属性

时间:2023-03-09 16:37:20
C# 使用反射 遍历输出 对象的属性

代码:

            Type type = dgParent.GetType();//获取对象类型
PropertyInfo[] props = type.GetProperties();//获取属性集合 String result = string.Empty;
foreach (var item in props)//遍历属性输出结果
{
result = string.Format(@"{0}
{1}:{2}", result, item.Name, item.GetValue(dgParent) == null ? string.Empty : item.GetValue(dgParent).ToString());
}

效果:

C# 使用反射 遍历输出 对象的属性