The original post was posted on GCDN. Since GCDN has made integration adjustments and is now focusing on product exchanges and other reasons, the GCDN Blog has been closed, so I had to move some useful ones here.
August 13, 2009 12:29 by winking
Writing this blog, CyljXu asked me a question today: How to call a method with ref or out parameters through reflection? Thinking that others might encounter this problem, I would like to record it for future readers to search for.
This is explained on MSDN, refer to the MethodBase.Invoke method.
public Object Invoke(Object obj, Object[] parameters)
Visual C++
public
: virtual Object^ Invoke( Object^ obj, arrayJ#
public
final Object Invoke(Object obj, Object[] parameters)
JScript
public
final
function
Invoke( obj : Object, parameters : Object[] ) : Object
parameter
obj Type: System.Object
The object on which methods or constructors are called. If the method is static, this parameter is ignored. If the constructor is static, this parameter must be a null reference (Nothing in Visual Basic) or an instance of the class in which the constructor is defined.
parameters type: System.Object[]
The parameter list of the method or constructor to be called. This is an array of objects that have the same number, order, and type as the parameters of the method or constructor being called. If there are no parameters, parameters should be a null reference (Nothing in Visual Basic).
If the method or constructor represented by this instance takes a ref parameter (ByRef in Visual Basic), the parameter does not require any special attributes when this function is used to call the method or constructor. If an object in the array is not explicitly initialized with a value, the object will contain the default value for that object type. For elements of reference type, the value is null Reference (Nothing in Visual Basic). For elements of value type, the value is 0, 0.0, or false, depending on the specific element type.
So how to call and handle the value passed? Please see the following example:
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.cnblogs.com )
GCDN( http://gcdn.grapecity.com/cs )
-