【转】C#怎么遍历一个对象里面的所有属性?

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

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

 

Student s=new......
            foreach (System.Reflection.PropertyInfo p in s.GetType().GetProperties())
            {
                Console.WriteLine("Name:{0} Value:{1}", p.Name, p.GetValue(s));
            }
相关文章
相关标签/搜索