Private method of the Invoke reflection class

Source: Internet
Author: User
Tags reflection

Private method of the Invoke reflection class

Some time ago need to write a method to handle a class of all private methods, night check a large tracts have not found, some boring people unexpectedly alarmist said impossible handle to private method. Finally, I wrote one out for the people in need to share.




Using System;

Using System.Collections.Generic;

Using System.Reflection;

Using System.Reflection.Emit;



Using System.Linq; Namespace Brucereflection {class Program {static void Main (string[] args) {Br

            Ucetest.show ();

            Brucetest.testprivate ();

        Console.read (); } public class Brucetest {///<summary>///Test Reflection private Method//

            /</summary> public static void Testprivate () {Bruce B = new Bruce ();

            Type t = B.gettype ();

            MethodInfo [] MS;

            MethodInfo m; BindingFlags bf = BindingFlags.InvokeMethod | BindingFlags.Instance |

            BindingFlags.NonPublic;

            ms = T.getmethods (BF); m = (from P in ms where P.name = = "Privatemethod" select p). If you use a version of VS that does not support LINQ, this can be changed to use loops to filter//reflect Private method M.invoke (b, new object[] {"BrucE Lee "});

            public static void Show () {Bruce B = new Bruce ();

            Type t = B.gettype ();

            MethodInfo [] MS;



            BindingFlags BF;

                Traverses all instantiated methods foreach (BindingFlags F in Enum.getvalues (typeof (BindingFlags))) { BF = bindingflags.invokemethod|

                BindingFlags.Instance;

                BF |= F;

                ms = T.getmethods (BF); if (MS!= null && Ms. Length > 0) {Console.WriteLine ("Bindingflag is" + bf.)

                    ToString ());

                Showmethodinfo (MS);

            }///Traverse all static method foreach (BindingFlags f in Enum.getvalues (typeof (BindingFlags))) {BF = BindingFlags.InvokeMethod |

                bindingflags.static;

                BF |= F;

                ms = T.getmethods (BF); if (MS!= null && Ms. LengTh > 0) {Console.WriteLine ("Bindingflag is" + bf.

                    ToString ());

                Showmethodinfo (MS); }} public static void Showmethodinfo (methodinfo[] ms) {foreach

            (MethodInfo m in ms)

                {Console.WriteLine ("-------------------------------------------------------------");

                Console.WriteLine ("/tmethod name: {0}/n/treturn type: {1}", M.name, M.returntype);

            Showparameterinfo (M.getparameters ());

        } Console.WriteLine ("*************************************************************/n");  public static void Showparameterinfo (ParameterInfo [] PS) {foreach (ParameterInfo p in

            PS) {Console.WriteLine ("/t/tparam name: {0}, type: {1}", P.name, P.parametertype); }} public class Bruce {private String _privateattribute= "This is a private attribute";



        public string publicattribute= ' This is a public attribute '; private void Privatemethod (string arg) {Console.Write ("This is a private method!

        ARG is "+ arg); } public void Publicmethod (string arg1,int arg2) {Console.Write ! Arg1 is {0},arg2 is {1}.

        ", ARG1,ARG2);  private static string Static_private_method (String arg) {return (' This is a static and Private method!

        ARG is "+ arg); public static string Static_public_method (String arg) {return (' This is ' a static and P Ublic method!

        ARG is "+ arg); } protected string Protected_method (string arg) {return (' This is a protected!

        ARG is "+ arg);

 }

    }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.