已知foo的函數原型麽?如果已知的話可以用GetMethod(string, Type[])這個重載。
比如妳提到的有這樣壹個類:
class a
{
public void foo(string value);
public void foo(out string value);
}
如果想獲得上面那壹個方法,用這個語句:
1
type.GetMethod("foo", new Type[] { typeof(string) });
如果想獲得下面那壹個方法,用這個語句:
1
type.GetMethod("foo", new Type[] { typeof(string).MakeByRefType() });
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: main
14 //
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: test
21 }
22 }
23
24 static void TestMethod(ref string arg)
25 {
26 arg = "test"; // #2 variable, wanna bypass to main process.
27 }
28 }