开发者

Pass a method with N params as parameter in C#

I have several methods all returning void with different signature (parameters) and different names. I need to pass those methods as a parameter in a generic method that will invoke it latter. Also that need to be as transparent as possible.

Following what I got so far:

    private void button1_Click(object sender, EventArgs e)
    {
        Action<string,int> TestHandler = Test;
        InvokeMyMethod(TestHandler);

        Action<string, int,object > TestHandlerN = TestN;
        InvokeMyMethod(TestHandlerN);
    }        
public void InvokeMyMethod(Delegate Method)
    {
          object[] args = new object[X];            
          Method.DynamicInvoke();
    }

    public void Test(string t1, int t2)
    {
        MessageBox.Show(t1 + t2);
    }

    public void TestN(string t1, int t2, object t3)
    {
        MessageBox.Show(t1 + t2);
    }

That is what I need:

    private void button1_Click(object sender, EventArgs e)
    {
        InvokeMyMethod(Test);
        InvokeMyMethod(TestN);
    }

    public void InvokeMyMethod(XXX_Type Method)
    {
         object[] args = new object[X];
         Method.D开发者_如何学编程ynamicInvoke(args);
    }

    public void Test(string t1, int t2)
    {
        MessageBox.Show(t1 + t2);
    }

    public void TestN(string t1, int t2, object t3)
    {
        MessageBox.Show(t1 + t2);
    }


This doesn't answer your question directly, but here is how I solve a similar problem:

public static partial class Lambda
{
    public static Action Pin<T0>
        (
            this Action<T0> action,
            T0 arg0
        )
    {
        return () => action(arg0);
    }

    public static Func<TResult> Pin<T0, TResult>
        (
            this Func<T0, TResult> func,
            T0 arg0
        )
    {
        return () => func(arg0);
    }

    public static Action Pin<T0, T1>
        (
            this Action<T0, T1> action,
            T0 arg0,
            T1 arg1
        )
    {
        return () => action(arg0, arg1);
    }

    public static Func<TResult> Pin<T0, T1, TResult>
        (
            this Func<T0, T1, TResult> func,
            T0 arg0,
            T1 arg1
        )
    {
        return () => func(arg0, arg1);
    }

    // More signatures omitted for brevity...
    // I would love it if C# supported variadic template parameters :-)
}

The idea is that if you have an Action which requires arguments, you can say:

Action<int, string> foo;
Action a = foo.Pin(5, "bleh");

Have the code generator.

Likewise, you might want to have a way to curry to some other delegate type (like Action<string, int>). The difference between my method and yours is that mine is not late-bound, but you do not appear to be using late-binding anyway, so early-binding gives you some compile-time type safety.

I'm not sure you can do what I think you are asking. You call method.DynamicInvoke with some object[], but how did you get values for those parameters?


And to answer the question before anybody asks it, I created the Lambda.Pin functions because I was tired of making this mistake:

Action<int> foo;
foreach (int i in someList)
    AddAction(() => foo(i));


public void InvokeMyMethod(Delegate method) {
    Method.DynamicInvoke(new object[] {"Test", 1});
}

But you need to invoke it like

InvokeMyMethod((Action<string, int>)Test);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜