C# 将一个方法作为参数传递给另一个方法

发布于 2024-10-25 06:31:02 字数 477 浏览 2 评论 0原文

我有一个在发生异常时调用的方法:

public void ErrorDBConcurrency(DBConcurrencyException e)
{
    MessageBox.Show("You must refresh the datasource");
}

我想做的是将此函数传递给一个方法,因此如果用户单击“是”,则调用该方法,例如,

public void ErrorDBConcurrency(DBConcurrencyException e, something Method)
{
    if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
        Method();
}

如果是这种情况,该方法可能有参数,也可能没有参数我也想通过它们。

我怎样才能做到这一点?

I have a method that is called when an exception occurs:

public void ErrorDBConcurrency(DBConcurrencyException e)
{
    MessageBox.Show("You must refresh the datasource");
}

What i would like to do is pass this function a method so if the user clicks Yes then the method is called e.g.

public void ErrorDBConcurrency(DBConcurrencyException e, something Method)
{
    if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
        Method();
}

The Method may or may not have parameters, if this is the case i would like to pass them too.

How can i acheive this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

山色无中 2024-11-01 06:31:03

查看 Func 和 Action 类。您可以使用以下方法来实现此目的:使用以下

public void ErrorDBConcurrency(DBConcurrencyException e, Action method)
{
    if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
        method()
}

public void Method()
{
    // do stuff
}
//....

方式调用它:

ErrorDBConcurrency(ex, Method)

查看本文了解一些详细信息。如果你希望你的方法接受一个参数,请使用 Action、Action 等。如果你希望它返回一个值,请使用 Func 等。这些泛型类有很多重载。

Look into the Func and Action classes. You can achieve this using the following:

public void ErrorDBConcurrency(DBConcurrencyException e, Action method)
{
    if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
        method()
}

public void Method()
{
    // do stuff
}
//....

Call it using

ErrorDBConcurrency(ex, Method)

Take a look at this article for some details. If you want your method to take a parameter, use Action, Action, etc. If you want it to return a value, use Func etc. There are many overloads of these generic classes.

﹎☆浅夏丿初晴 2024-11-01 06:31:03
public delegate void MethodHandler(); // The type

public void ErrorDBConcurrency(DBConcurrencyException e, MethodHandler Method) // Your error function

ErrorDBConcurrency (e, new MethodHandler(myMethod)); // Passing the method
public delegate void MethodHandler(); // The type

public void ErrorDBConcurrency(DBConcurrencyException e, MethodHandler Method) // Your error function

ErrorDBConcurrency (e, new MethodHandler(myMethod)); // Passing the method
冷情 2024-11-01 06:31:02

您可以使用操作 委托类型。

public void ErrorDBConcurrency(DBConcurrencyException e, Action method)
{
    if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
        method();
}

然后你可以像这样使用它:

void MyAction()
{

}

ErrorDBConcurrency(e, MyAction); 

如果你确实需要参数,你可以使用 lambda 表达式。

ErrorDBConcurrency(e, () => MyAction(1, 2, "Test")); 

You can use the Action delegate type.

public void ErrorDBConcurrency(DBConcurrencyException e, Action method)
{
    if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
        method();
}

Then you can use it like this:

void MyAction()
{

}

ErrorDBConcurrency(e, MyAction); 

If you do need parameters you can use a lambda expression.

ErrorDBConcurrency(e, () => MyAction(1, 2, "Test")); 
茶色山野 2024-11-01 06:31:02

添加一个 Action 作为参数:

public void ErrorDBConcurrency(DBConcurrencyException e, Action errorAction)
{
   if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
       errorAction()
}

然后你可以像这样调用它

ErrorDBConcurrency(ex, () => { do_something(foo); });

ErrorDBConcurrency(ex, () => { do_something_else(bar, baz); });

Add an Action as parameter:

public void ErrorDBConcurrency(DBConcurrencyException e, Action errorAction)
{
   if (MessageBox.Show("You must refresh the datasource") == DialogResult.OK)
       errorAction()
}

and then you can call it like this

ErrorDBConcurrency(ex, () => { do_something(foo); });

or

ErrorDBConcurrency(ex, () => { do_something_else(bar, baz); });
尹雨沫 2024-11-01 06:31:02

您需要使用委托作为参数类型。

如果 Method 返回 void,则 something操作操作操作等(其中 T1...Tn 是 Method 的参数类型)。

如果 Method 返回 TR 类型的值,则 somethingFunc, Func, Func 等。

You need to use a delegate as the parameter type.

If Method returns void, then something is Action, Action<T1>, Action<T1, T2>, etc (where T1...Tn are the parameter types for Method).

If Method returns a value of type TR, then something is Func<TR>, Func<T1, TR>, Func<T1, T2, TR>, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文