且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-21 20:20:39

比如我现在有一个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));
}
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6227739.html,如需转载请自行联系原作者