遍历对象属性,成员,方法的方法

 

using System;
using System.Reflection;
using System.Web;

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpRuntime hr = new HttpRuntime();
            DisplayValues(hr);
        }
        public void DisplayValues(object o)
        {
            Type type = o.GetType();
            outPut("<hr>Methods<br>");
            MethodInfo[] methodInfos = type.GetMethods();
            foreach (MethodInfo m in methodInfos)
            {
                outPut(m.Name);           
            }

            outPut("<hr>Fields<br>");
            FieldInfo[] fieldInfos = type.GetFields();
            foreach (FieldInfo f in fieldInfos)
            {
                outPut(f.Name + ": " + f.GetValue(null).ToString());
            }

            outPut("<hr>Properties<br>");
            PropertyInfo[] propertyInfos = type.GetProperties();
            foreach (PropertyInfo p in propertyInfos)
            {
                outPut(p.Name + ": " + p.GetValue(null, null).ToString());
            }
        }
        public void outPut(string s)
        {
            ltl1.Text += s + "<br>";
        }
    }
}

 

posted @ 2013-05-30 16:44  JeanGe  阅读(301)  评论(0编辑  收藏  举报