反射的用法

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace CrmWinLib.Resources
{
    public class ReflectTool
    {
        public R execute<R, T>(T executeObject, string methodName, object[] parameters)
        {

            System.Reflection.MethodInfo method = executeObject.GetType().GetMethod(methodName);

            object ro = method.Invoke(executeObject, parameters);

            R r = (R)(ro);

            return r;

        }

        public object execute(object executeObject, string methodName, object[] parameters)
        {

            System.Reflection.MethodInfo method = executeObject.GetType().GetMethod(methodName);

            object ro = method.Invoke(executeObject, parameters);

            return ro;

        }


        public PropertyInfo[] GetPropertys(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] infos = type.GetProperties();
            return infos;
        }


        public void SetPropertyValue(object obj, string property, object value)
        {
            Type type = obj.GetType();
            PropertyInfo info = type.GetProperty(property);
            if (info != null)
            {
                info.SetValue(obj, value, null);
            }
        }

 
        public object GetPropertyValue(object obj, string property)
        {
            Type type = obj.GetType();
            PropertyInfo info = type.GetProperty(property);
            if (info == null)
            {
                return null;
            }
            return info.GetValue(obj, null);
        }


    }
}

 

调用

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrmWinLib.Resources;
using System.Reflection;

namespace Test
{
    public partial class ReflectTooltest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ReflectTool t = new ReflectTool();
            test obj1 = new test();
            string s1 = t.execute<string, test>(obj1, "add", new object[] { 8, "aasd" });
            Response.Write(s1);
            test obj2 = new test();
            object s2 = t.execute(obj2, "add", new object[] { 9, "DGFH" });
            Response.Write(s2);


            Response.Write("实例的所有公共属性:");
            PropertyInfo[] pInfos = t.GetPropertys(obj1);
            foreach (PropertyInfo info in pInfos)
            {
                Response.Write(info.GetType() + ":" + info.Name);
            }

            t.SetPropertyValue(obj1, "UserId", 1);
            t.SetPropertyValue(obj1, "UserName", "admin");
            Response.Write("设置属性值后的值");
            Response.Write("UserId:" + t.GetPropertyValue(obj1, "UserId"));
            Response.Write("UserName:" + t.GetPropertyValue(obj1, "UserName"));


        }
    }

    public class test
    {
        private int userId;

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public string add(int i, string s)
        {
            return i.ToString() + s;
        }


    }
}

posted @ 2009-04-27 15:36  昕友软件开发  阅读(275)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯