lundi 12 novembre 2018

calling method via reflection that takes an Action

I have an internal method like this

void Foo(Action<DB> act)

the class DB is in the same assembly and also marked internal. I can work out how to get a methodinfo for Foo but I cannot work out how to set up the callback act in the calling code. This is what I have so far (as an added spice I want to call another internal method from inside the callback)

    var tt = eng.GetType().Assembly;
    var dd = tt.GetType("Internal.DB");
    var kk = typeof(Action<>);
    Type[] targs =  { dd };
    var qq = kk.MakeGenericType(targs);
    MethodInfo dynMethod = eng.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { qq },null);
    dynMethod.Invoke(this, new object[] {
        ((Action<object>)(db=>{
       MethodInfo dynMethod2 = db.GetType().GetMethod("Wiz", BindingFlags.NonPublic | BindingFlags.Instance);
        dynMethod2.Invoke(db);
    })) }); 

my problem is here (I think)

((Action<object>)(db=>{

I really need

((Action<DB>)(db=>{

but I cannot do that (DB is internal), the one with object compiles but fails at run time saying that the object is of the wrong type.





Aucun commentaire:

Enregistrer un commentaire