jeudi 8 octobre 2015

Invoking unsafe methods via reflection in C#

I'm trying to run tests on a private method in a library. Since it's buried several layers down from the public API, I thought it would be more convenient to use reflection to invoke the method, like so:

var type = Type.GetType("Foo,Bar");
var method = type.GetMethod("Method",
    BindingFlags.Instance | BindingFlags.NonPublic);
object result = method.Invoke(...);

The only problem is, my method takes a char* argument! So when I try to pass it in to Invoke like so (which takes an object[] array),

char* param = ...
method.Invoke(null, new object[] { param });

I get the following error from the compiler:

CS0029: Cannot implicitly convert type 'char*' to 'object'

Even when I try an explicit cast like (object)param, the compiler flat-out says that it can't convert char* to object.

If there's no way to pass it into Invoke, is it possible to invoke the method?





Aucun commentaire:

Enregistrer un commentaire