获取对象的所有属性 [英] Getting ALL the properties of an object

查看:37
本文介绍了获取对象的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的对象:

some_object

该对象具有1000个属性.

this object has like 1000 properties.

我想像这样遍历每个属性:

i would like to loop through every property like this:

foreach (property in some_object)
//output the property

有一种简单的方法吗?

推荐答案

您可以使用反射.

// Get property array
var properties = GetProperties(some_object);

foreach (var p in properties)
{
    string name = p.Name;
    var value = p.GetValue(some_object, null);
}

private static PropertyInfo[] GetProperties(object obj)
{
    return obj.GetType().GetProperties();
}

但是,如果您的对象具有1000个属性,这仍然不能解决问题.

However, this still does not solve the problem where you have an object with 1000 properties.

这篇关于获取对象的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆