C#中获得一个对象的所有属性 [英] c# getting ALL the properties of an object
本文介绍了C#中获得一个对象的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有对象是这样的:
some_object
$ P $ 。p>
该对象具有类似于1000的属性。
我想通过这样的每个属性循环:
的foreach(在some_object属性)
//输出属性
有一种简单的方法来做到这一点?
解决方案您可以。使用反射
//获取属性数组
VAR性能=的GetProperties(some_object);
的foreach(在性能变种P)
{
字符串名称= p.Name;
VAR值= p.GetValue(some_object,NULL);
}
私人静态的PropertyInfo []的GetProperties(obj对象)
{
返回obj.GetType()的GetProperties();
}
不过,这仍然没有,让你有一个对象1000解决问题属性。
i have object like this:
some_object
this object has like 1000 properties.
i would like to loop through every property like this:
foreach (property in some_object) //output the property
is there an easy way to do this?
解决方案You can use reflection.
// 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(); }
However, this still does not solve the problem whereby you have an object with 1000 properties.
这篇关于C#中获得一个对象的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文