1 class PRogram 2 { 3 static void Main(string[] args) 4 { 5 string content = "main"; //#1 variable 6 MethodInfo testMethod = typeof(Program).GetMethod("TestMethod", 7 BindingFlags.Static | BindingFlags.NonPublic); 8 if (testMethod != null) 9 {10 // Following way can not take content back.11 // -------------------------------------12 testMethod.Invoke(null, new object[] { content /* #1 variable */ });13 Console.WriteLine(content); // #1 variable, Output is: main14 // -------------------------------------15 16 17 object[] invokeArgs = new object[] { content /* #1 variable */ };18 testMethod.Invoke(null, invokeArgs);19 content = (string)invokeArgs[0]; // #2 variable, bypass from invoke, set to content.20 Console.WriteLine(content); // #2 variable, Output is: test21 }22 }23 24 static void TestMethod(ref string arg)25 {26 arg = "test"; // #2 variable, wanna bypass to main process.27 }28 }
To be the apostrophe which changed “Impossible” into “I’m possible” ---------------------------------------------------- WinkingZhang's Blog (http://winkingzhang.VEVb.com) GCDN(http://gcdn.grapecity.com/cs)