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