在 C# 中传递具有 N 个参数作为参数的方法
我有几种方法都返回带有不同签名(参数)和不同名称的 void 。我需要将这些方法作为参数传递到稍后调用它的通用方法中。此外,还需要尽可能透明。
按照我到目前为止所得到的:
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);
}
这就是我需要的:
private void button1_Click(object sender, EventArgs e)
{
InvokeMyMethod(Test);
InvokeMyMethod(TestN);
}
public void InvokeMyMethod(XXX_Type Method)
{
object[] args = new object[X];
Method.DynamicInvoke(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);
}
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.DynamicInvoke(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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并没有直接回答你的问题,但这是我解决类似问题的方法:
这个想法是,如果你有一个需要参数的
Action
,你可以说:拥有 代码生成器。
同样,您可能希望有一种方法来柯里化为其他委托类型(例如
Action
)。我的方法和你的方法之间的区别在于,我的方法不是后期绑定,但你似乎并没有使用后期绑定,因此早期绑定为你提供了一些编译时类型安全性。我不确定你能做我认为你要求的事情。您使用一些
object[]
调用method.DynamicInvoke
,但是如何获取这些参数的值呢?为了在任何人提出问题之前回答这个问题,我创建了 Lambda.Pin 函数,因为我厌倦了犯这个错误:
This doesn't answer your question directly, but here is how I solve a similar problem:
The idea is that if you have an
Action
which requires arguments, you can say: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 someobject[]
, 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:但你需要像这样调用它
But you need to invoke it like