【轉】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));
            }
相關文章
相關標籤/搜索