zoukankan      html  css  js  c++  java
  • Reflection with Private Members

    http://www.codingday.com/reflection-with-private-members/

    By using reflection we can call any function including the private and protected ones. This includes private static methods, constructor methods, normal methods. What we need to do is to get the method by the BindingFlags.Nonpublic flag and then we just use it like a reflected type.

    Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
     
    namespace ReflectPrivateMembers
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                ConstructorInfo ci 
    = typeof(Hello).GetConstructor(BindingFlags.NonPublic| BindingFlags.Instance ,null,System.Type.EmptyTypes,null);
                
    object helloObject = ci.Invoke(System.Type.EmptyTypes);
                MethodInfo[] helloObjectMethods 
    = helloObject.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly| BindingFlags.Instance );
     
                
    foreach (MethodInfo mi in helloObjectMethods)
                {
                    mi.Invoke(helloObject, System.Type.EmptyTypes);
                }
                Console.ReadLine();
            }
        }
        
    public class Hello
        {
            
    private Hello()
            {
                Console.WriteLine(
    "Private Constructor");
            }
            
    public void HelloPub()
            {
                Console.WriteLine(
    "Public Hello");
            }
            
    private void HelloPriv()
            {
                Console.WriteLine(
    "Private Hello");
            }
        }
    }
  • 相关阅读:
    可图性判定HavelHakimi定理
    并查集入门
    js数组和函数应用
    DOM用法及应用
    javascript基础知识
    表单
    PHP变量
    30天自制操作系统开发笔记——IDT中断描述符表
    《30天自制操作系统》学习笔记——汇编程序磁盘BIOS调用
    汇编指令: LGDT、LIDT、LLDT、LMSW、LOADALL、LOADALL286、LOCK、LODSB、LODSW、LODSD
  • 原文地址:https://www.cnblogs.com/smwikipedia/p/1562112.html
Copyright © 2011-2022 走看看