How to Invoke Method<T>(Func<Action<T>> action) with private type

Viewed 2337

I hope the code snipped illustrates my issue. I need to Invoke the CallEvent method like it is in the out-commented 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 can't get my head around how to solve this (Action<msgtype>)
    }
}

I also tried:

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

I get the following Exception:

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]].

How should I proceed?

4 Answers
Related