有没有办法直接使用 C# 方法作为委托?

发布于 2024-09-08 13:45:41 字数 674 浏览 6 评论 0原文

这更多的是一个 C# 语法问题,而不是一个需要解决的实际问题。假设我有一个采用委托作为参数的方法。假设我定义了以下方法:

void TakeSomeDelegates(Action<int> action, Func<float, Foo, Bar, string> func)
{
    // Do something exciting
}

void FirstAction(int arg) { /* something */ }

string SecondFunc(float one, Foo two, Bar three){ /* etc */ }

现在,如果我想以 FirstActionSecondFunc 作为参数调用 TakeSomeDelegates,据我所知,我需要做这样的事情:

TakeSomeDelegates(x => FirstAction(x), (x,y,z) => SecondFunc(x,y,z));

但是有没有更方便的方法来使用适合所需委托签名的方法而无需编写 lambda?理想情况下类似于 TakeSomeDelegates(FirstAction, SecondFunc) ,尽管显然这不能编译。

This is more of a C# syntax question rather than an actual problem that needs solving. Say I have a method that takes a delegate as parameter. Let's say I have the following methods defined:

void TakeSomeDelegates(Action<int> action, Func<float, Foo, Bar, string> func)
{
    // Do something exciting
}

void FirstAction(int arg) { /* something */ }

string SecondFunc(float one, Foo two, Bar three){ /* etc */ }

Now if I want to call TakeSomeDelegates with FirstAction and SecondFunc as arguments, As far as I can tell, I need to do something like this:

TakeSomeDelegates(x => FirstAction(x), (x,y,z) => SecondFunc(x,y,z));

But is there a more convenient way to use a method that fits the required delegate signature without writing a lambda? Ideally something like TakeSomeDelegates(FirstAction, SecondFunc), although obviously that doesn't compile.

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

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

发布评论

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

评论(4

木落 2024-09-15 13:45:41

您正在寻找的是一种名为“方法组”的东西。有了这些,你可以替换一行 lamdas,例如:

was:

TakeSomeDelegates(x => firstAction(x), (x, y, z) => secondFunc(x, y, z));

替换为方法组后:

TakeSomeDelegates(firstAction, secondFunc);

What you're looking for is something called 'method groups'. With these, you can replace one line lamdas, such as:

was:

TakeSomeDelegates(x => firstAction(x), (x, y, z) => secondFunc(x, y, z));

after replacing with method groups:

TakeSomeDelegates(firstAction, secondFunc);
中二柚 2024-09-15 13:45:41

只需跳过函数名称上的括号即可。

        TakeSomeDelegates(FirstAction, SecondFunc);

编辑:

仅供参考,因为括号在 VB 中是可选的,所以他们必须写这个......

 TakeSomeDelegates(AddressOf FirstAction, AddressOf SecondFunc)

Just skip the parens on the function names.

        TakeSomeDelegates(FirstAction, SecondFunc);

EDIT:

FYI Since parens are optional in VB, they have to write this...

 TakeSomeDelegates(AddressOf FirstAction, AddressOf SecondFunc)
等风也等你 2024-09-15 13:45:41

编译器会接受需要委托的方法组的名称,只要它能够确定选择哪个重载,就不需要构建 lambda。您看到的确切编译器错误消息是什么?

The compiler will accept names of method groups where a delegate is needed, as long as it can figure out which overload to choose, you don't need to build a lambda. What is the exact compiler error message you're seeing?

谜兔 2024-09-15 13:45:41

是的,它被称为方法组,更准确的例子是......

static void FirstAction(int arg) { /* something */ }

static string SecondFunc(float one, Foo two, Bar three) { return ""; }


Action<int> act1 = FirstAction;
Func<float, Foo, Bar, string> act2 = SecondFunc;


TakeSomeDelegates(firstAction, secondFunc);

通过这种方式,您可以使用方法组。

Yes it is called Method Group, and more precise example of that is...

static void FirstAction(int arg) { /* something */ }

static string SecondFunc(float one, Foo two, Bar three) { return ""; }


Action<int> act1 = FirstAction;
Func<float, Foo, Bar, string> act2 = SecondFunc;


TakeSomeDelegates(firstAction, secondFunc);

In this way you can use Method Group.

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