C#怎么遍历一个对象里面的全部属性?

Posted 翻滚吧炒鸡蛋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#怎么遍历一个对象里面的全部属性?相关的知识,希望对你有一定的参考价值。

原帖地址:https://www.cnblogs.com/soundcode/p/6227739.html

比如我现在有一个Student的对象,里面有属性stuName,stuAge,stuGender,我现在该怎么写循环才能遍历这几个属性?

 

Student s=new......
            foreach (System.Reflection.PropertyInfo p in s.GetType().GetProperties())
            {
                Console.WriteLine("Name:{0} Value:{1}", p.Name, p.GetValue(s));
            }

  

  

以上是关于C#怎么遍历一个对象里面的全部属性?的主要内容,如果未能解决你的问题,请参考以下文章