mercredi 31 mai 2017

How to Invoke Method

please bare with me as this is my first post on SO I hope the code snipped illustrates my issue.. I need to Invoke the CallEvent method like it is in the outcommented line.. i have no access to the ThirdParty or AnotherThirdParty class This is as far as i come

public class ThirdParty
{
    private struct MsgType
    {
    }

    private static void AnotherFunc(MsgType msg)
    {
    }
}

public class AnotherThirdParty
{
    public static void CallEvent<T>(Func<int, Action<T>> action, T arg)
    {
    }
}


public class MyClass
{
    public static void Main()
    {


        Type MsgType = typeof(ThirdParty).GetNestedType("MsgType", BindingFlags.Instance | BindingFlags.NonPublic);
        object msg = Activator.CreateInstance(MsgType);

        MethodInfo CallEvent = typeof(AnotherThirdParty).GetMethod("CallEvent");
        CallEvent = CallEvent.MakeGenericMethod(MsgType);

        MethodInfo AnotherFunc = typeof(ThirdParty).GetMethod("AnotherFunc", BindingFlags.Static | BindingFlags.NonPublic);

        CallEvent.Invoke(null, new object[] {???, msg});
        //CallEvent<MsgType>((int x) => new Action<MsgType>(AnotherFunc), msg);  // i cant get my head around how to solve this (Action<msgtype>)
    }
}

I also tried: CallEvent.Invoke(null, new object[] { new Func> ( (int x) => new Action((object y) => AnotherFunc.Invoke(null, new object[] { y })) ), msg });

but this thorws: System.ArgumentException: Object of type 'System.Func2[System.Int32,System.Action1[System.Object]]' cannot be converted to type 'System.Func2[System.Int32,System.Action1[ThirdParty+MsgType]].

Im out of ideas :(





Aucun commentaire:

Enregistrer un commentaire