• .net 反射访问私有变量和私有方法


    以下为本次实践代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                //反射读取类私有属性
                Person per = new Person("ismallboy", "20102100104");
                Type t = per.GetType();
                //获取私有方法
                MethodInfo method = t.GetMethod("GetStuInfo", BindingFlags.NonPublic | BindingFlags.Instance);
                //访问无参数私有方法
                string strTest = method.Invoke(per, null).ToString();
                //访问有参数私有方法
                MethodInfo method2 = t.GetMethod("GetValue", BindingFlags.NonPublic | BindingFlags.Instance);
                object[] par = new object[2];
                par[0] = "ismallboy";
                par[1] = 2;
                string strTest2 = method2.Invoke(per, par).ToString();
    
                //获取私有字段
                PropertyInfo field = t.GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance);
                //访问私有字段值
                string value = field.GetValue(per).ToString();
                //设置私有字段值
                field.SetValue(per, "new Name");
                value = field.GetValue(per).ToString();
            }
        }
    
        /// <summary>
        /// 个人信息
        /// </summary>
        class Person
        {
            private string Name { get; set; }
            private string StuNo { get; set; }
    
            public Person(string name, string stuNo)
            {
                this.Name = name;
                this.StuNo = stuNo;
            }
    
            private string GetStuInfo()
            {
                return this.Name;
            }
    
            private string GetValue(string str, int n)
            {
                return str + n.ToString();
            }
        }
    }

    如果使用dynamic的话,也可以如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                //反射读取类私有属性
                dynamic per = new Person("ismallboy", "20102100104");
                Type t = per.GetType();
                //获取私有方法
                MethodInfo method = t.GetMethod("GetStuInfo", BindingFlags.NonPublic | BindingFlags.Instance);
                //访问无参数私有方法
                string strTest = method.Invoke(per, null);
                //访问有参数私有方法
                MethodInfo method2 = t.GetMethod("GetValue", BindingFlags.NonPublic | BindingFlags.Instance);
                object[] par = new object[2];
                par[0] = "ismallboy";
                par[1] = 2;
                string strTest2 = method2.Invoke(per, par);
    
                //获取私有字段
                PropertyInfo field = t.GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance);
                //访问私有字段值
                string value = field.GetValue(per);
                //设置私有字段值
                field.SetValue(per, "new Name");
                value = field.GetValue(per);
            }
        }
    
        /// <summary>
        /// 个人信息
        /// </summary>
        class Person
        {
            private string Name { get; set; }
            private string StuNo { get; set; }
    
            public Person(string name, string stuNo)
            {
                this.Name = name;
                this.StuNo = stuNo;
            }
    
            private string GetStuInfo()
            {
                return this.Name;
            }
    
            private string GetValue(string str, int n)
            {
                return str + n.ToString();
            }
        }
    }

     

    以上图片由“图斗罗”提供

  • 相关阅读:
    把SVN和jenkins连起来--有人提代码就能自动build!
    Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码
    JwtAuthenticationTokenFilter 实现shiro 利用 token 信息完成令牌登录
    Jenkins war deploy Shell
    jenkins 不执行部署 tomcat
    CentOS 7 设置 svn 开机启动
    502 Bad Gateway nginx/1.12.2 tomcat
    shiro 集成 JWT 自动获取token对应的用户信息
    org.apache.shiro.session.UnknownSessionException: There is no session with
    多角色分库情况下shiro开发
  • 原文地址:https://www.cnblogs.com/ldyblogs/p/net.html
Copyright © 2020-2023  润新知