反射的用法

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;
        }


    }
}

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1444646.html