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());
            }

效果:

原文地址:https://www.cnblogs.com/yilinyangyu/p/9012674.html