Methodinfo.Invoke with Out Parameter

MethodInfo.Invoke with out Parameter

public static bool TryParse( string text, out int number ) { .. }

MethodInfo method = GetTryParseMethodInfo();
object[] parameters = new object[]{ "12345", null }
object result = method.Invoke( null, parameters );
bool blResult = (bool)result;
if ( blResult ) {
int parsedNumber = (int)parameters[1];
}

How can I invoke a method with an out parameter?

public class MyWebClient : WebClient
{
delegate byte[] DownloadDataInternal(Uri address, out WebRequest request);

DownloadDataInternal downloadDataInternal;

public MyWebClient()
{
downloadDataInternal = (DownloadDataInternal)Delegate.CreateDelegate(
typeof(DownloadDataInternal),
this,
typeof(WebClient).GetMethod(
"DownloadDataInternal",
BindingFlags.NonPublic | BindingFlags.Instance));
}

public byte[] DownloadDataInternal(Uri address, out WebRequest request)
{
return downloadDataInternal(address, out request);
}
}

Reflection: How to Invoke Method with parameters

Change "methodInfo" to "classInstance", just like in the call with the null parameter array.

  result = methodInfo.Invoke(classInstance, parametersArray);

How to pass a parameter as a reference with MethodInfo.Invoke

You need to create the argument array first, and keep a reference to it. The out parameter value will then be stored in the array. So you can use:

object[] arguments = new object[] { "test", null };
MethodInfo method = ...;
bool b = (bool) method.Invoke(null, arguments);
byte[] rawAsm = (byte[]) arguments[1];

Note how you don't need to provide the value for the second argument, because it's an out parameter - the value will be set by the method. If it were a ref parameter (instead of out) then the initial value would be used - but the value in the array could still be replaced by the method.

Short but complete sample:

using System;
using System.Reflection;

class Test
{
static void Main()
{
object[] arguments = new object[1];
MethodInfo method = typeof(Test).GetMethod("SampleMethod");
method.Invoke(null, arguments);
Console.WriteLine(arguments[0]); // Prints Hello
}

public static void SampleMethod(out string text)
{
text = "Hello";
}
}

How can I invoke a method that has params string[] as its only parameter without send values

Pass an empty array, because that is what the C# compiler will pass when indicating no arguments for a variadic method:

var dbResult = methodInfo.Invoke(repository, new object[] { new string[0] });

Note that you will have to wrap the string array into an object array, because that object array represents the list of arguments. The first passed argument is then the empty string array.

How to invoke methods with ref/out params using reflection

You can do something like this:

static void Main(string[] args)
{
var method = typeof (Cow).GetMethod("TryParse");
var cow = new Cow();
var inputParams = new object[] {"cow string", cow};
method.Invoke(null, inputParams);
// out parameter value is then set in inputParams[1]
Console.WriteLine( inputParams[1] == null ); // outputs True
}

class Cow
{
public static bool TryParse(string s, out Cow cow)
{
cow = null;
Console.WriteLine("TryParse is called!");
return false;
}
}

How to extract MethodInfo.Invoke parameters from an object maybe implementing IEnumerableT

FirstOrDefault is an Extension method, you need to pass this as an arg

return methodInfo.Invoke(null, new object[] {objectThatIsExpectedToImplementIEnumerableT });


Related Topics



Leave a reply



Submit