jeudi 16 février 2017

C# calling method via reflection with parameters passed inline and as local variables

I have following code. It has problem. The call to MyMethod does not return the correct result set based on the input value passed.

foreach (var input in inputCollection)
            {

var result = myInstance.GetType().InvokeMember("MyMethod",
    BindingFlags.InvokeMethod | BindingFlags.Public,
    null,
    myInstance,
    new Object[] { input })
}

Instead, if I do as following, it works as expected and returns different output based on the supplied input value.

foreach (var input in inputCollection)
            {
var args = new Object[] { input };

var result = myInstance.GetType().InvokeMember("MyMethod",
    BindingFlags.InvokeMethod | BindingFlags.Public,
    null,
    myInstance,
    args)
}

What could possibly be the difference here if I pass argument inline with object initializer and via local variable?

Edit: In the not working code, it always returns result for the first item of InputCollection regardless current iterating value of input.

Thanks Jay





Aucun commentaire:

Enregistrer un commentaire