zoukankan      html  css  js  c++  java
  • 反射的用法

    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
  • 相关阅读:
    前端开发—HTML
    初识 Django
    前端开发—BOM对象DOM文档对象操作
    前端开发—jQuery
    前端开发—Javascript
    前端开发—CSS 盒子、浮动、定位
    前端开发—CSS
    html模拟手机页面
    人类简史读书笔记
    正则表达式
  • 原文地址:https://www.cnblogs.com/starcrm/p/1444646.html
Copyright © 2011-2022 走看看