简体   繁体   中英

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

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?

public class ThirdParty
{
    private struct MsgType { }
    private static void AnotherFunc(MsgType msg)
    {
        // Inserted to demonstrate getting here
        Console.WriteLine($"HEY: {msg}");
    }
}

public class AnotherThirdParty
{
    public static void CallEvent<T>(Func<int, Action<T>> action, T arg)
    {
        // Inserted to demonstrate calling the func and then
        // the action
        action(12)(arg);
    }
}

public static void Main()
{
    var msgTypeType = 
        typeof(ThirdParty).GetNestedType("MsgType", BindingFlags.NonPublic);

    // This is the message type we're passing (presumably you'll do more with it)
    var ourMsgTypeArg = Activator.CreateInstance(msgTypeType);

    // Get the reference to the CallEvent method
    var callEventMethod =
        typeof(AnotherThirdParty).GetMethod("CallEvent", BindingFlags.Public | BindingFlags.Static)
        .MakeGenericMethod(msgTypeType);

    // Get the reference to the AnotherFunc method
    var anotherFunc =
        typeof(ThirdParty).GetMethod("AnotherFunc", BindingFlags.NonPublic | BindingFlags.Static);

    // Build the func to pass along to CallEvent
    var func = CreateFunc(msgTypeType, anotherFunc);

    // Call the CallEvent<MsgType> method.
    callEventMethod.Invoke(null, new object[] {
        func,
        ourMsgTypeArg
    });
}

private static Delegate CreateFunc(Type msgType, MethodInfo anotherFunc)
{
    // The func takes an int
    var intArg = Expression.Parameter(typeof(int));

    // The action takes a msgType
    var msgTypeArg = Expression.Parameter(msgType);

    // Represent the call out to "AnotherFunc"
    var call = Expression.Call(null, anotherFunc, msgTypeArg);

    // Build the action to just make the call to "AnotherFunc"
    var action = Expression.Lambda(call, msgTypeArg);

    // Build the func to just return the action
    var func = Expression.Lambda(action, intArg);

    // Compile the chain and send it out
    return func.Compile();
}

This code functions as you've requested and prints the following:

HEY: UserQuery+ThirdParty+MsgType

This seems to run:

    MethodInfo miCreateDelegate = typeof(MethodInfo).GetMethod("CreateDelegate", new[] { typeof(Type), typeof(Object) });
    var ActionType = typeof(Action<>).MakeGenericType(MsgType);
    var lambdabody = Expression.Convert(Expression.Call(Expression.Constant(AnotherFunc), miCreateDelegate, new[] { Expression.Constant(ActionType), Expression.Constant(null) }), ActionType);
    var intparm = Expression.Parameter(typeof(int));
    var lambda = Expression.Lambda(lambdabody, intparm);

    CallEvent.Invoke(null, new object[] {
        lambda.Compile(),
        msg
    });

A more complete answer is how did I generate this? I used LINQPad to compile a simpler, similar expression substituting string for MsgType into an Expression :

public static void afunc(string x) { }

Expression<Func<int, Action<string>>> lambda = (int x) => new Action<string>(afunc);

Then I used the LINQPad Dump() function to output the expression tree.

lambda.Dump();

Then some spelunking in MSDN Expression documentation gave me the right static methods to create the pieces. I already knew how to instantiate generic types from an extension method for LINQPad that creates anonymous types on the fly to extend Dump() to exclude fields from anonymous objects, and I knew how to create lambdas from an extension method that extends LINQ with a proper SQL-translatable Left and Right Join operation.

Use Delegate.CreateDelegate method to construct an Action<MsgType> object. Construct Func<int,Action<T>> using Expression.Lambda<> :

var actionType = typeof(Action<>).MakeGenericType(MsgType);
var funcType = typeof(Func<,>).MakeGenericType(typeof(int), actionType);
var p1 = Expression.Parameter(typeof(int));
var p2 = Expression.Parameter(actionType);
var delegate = Expression.Constant(Delegate.CreateDelegate(actionType, AnotherFunc), funcType);
var lambda = Expression.Lambda(delegate, p1, p2);
CallEvent.Invoke(null, new object[] {
    lambda.Compile()
,   msg
});

This will work and will print an A but the function factory is a mistery to me so I just returned the created delegate. And this is compatible with .net standard 1.1

static void Main(string[] args)
        {
             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);

        var actionType = typeof(Action<>).MakeGenericType(MsgType);

        var actionDelegate = AnotherFunc.CreateDelegate(actionType);

        var param = Expression.Parameter(typeof(int));
        var funcDelegate = Expression.Lambda(Expression.Constant(actionDelegate),param).Compile();

        CallEvent.Invoke(null, new []{ funcDelegate, msg });


        Console.ReadLine();

        }

        public class ThirdParty
        {
            private struct MsgType
            { }

            private static void AnotherFunc(MsgType msg)
            {
                Console.WriteLine("A");
            }
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM