传递异步委托的方法签名是什么? [英] What's the method signature for passing an async delegate?

查看:100
本文介绍了传递异步委托的方法签名是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从Objective-C领域回到了C#,C#5中的async/await关键字看起来很酷.但我仍在尝试掌握正确的语法.

I've recently moved back to C# from being in Objective-C land, and the async/await keywords in C# 5 look cool. But I'm still trying to get a handle on the proper syntax.

我想声明一个将异步委托作为参数的方法,但是我在使调用方和被调用方语法正确时遇到了麻烦.有人可以提供一个代码示例来显示方法声明,调用和对委托的调用吗?

I want to declare a method that takes an asynchronous delegate as a parameter, but I am having trouble getting both the caller and the callee syntax correct. Can someone provide a code sample showing the method declaration, the call, and a call to the delegate?

我认为该声明将类似于以下内容.请注意,该函数不是异步的.即它的异步性与委托无关.

I'm thinking the declaration would be something like the following. Note that this function isn't asynchronous; i.e. its asynchronicity is independent of the delegate.

void DoSomethingWithCallback(async delegate foo(int)) 
{
    ...
    foo(42);
    ...
}

呼叫类似于:

DoSomethingWithCallback(async (int x) => { this.SomeProperty = await SomeAsync(x); });

当然,这些都不会编译,并且我见过的大多数示例都假定一个字段或属性是委托,而不是我要使用的匿名委托.

Of course none of this compiles and most of the samples I've seen assume that one has a field or property that's the delegate, rather than the anonymous delegate I'd like to use.

推荐答案

将委托作为参数的函数必须使用命名的委托类型.与Objective-C不同,您不能在函数定义中内联声明匿名委托类型.但是,提供了泛型Action<>和Func<>,因此您不必自己声明新类型.在下面的代码中,我假设委托将单个int作为参数.

A function that takes a delegate as a parameter must use a named delegate type; unlike in Objective-C you can't declare an anonymous delegate type inline in the function definition. However, the generics Action<> and Func<> are provided so that you don't have to declare a new type yourself. In the code below I'm assuming the delegate takes a single int as a parameter.

void DoSomethingWithCallback(Func<int,Task> callbackDelegate)
{
    Task t = callbackDelegate(42);
}

如果此函数实际上对返回的Task对象没有任何作用(与上面显示的代码一样),则可以使用Action<int>作为委托类型.如果使用Action,则仍然可以声明委托异步(如下),但是返回的隐式Task对象将被忽略.

If this function doesn't actually do anything with the Task object returned (as with the code shown above), you can instead use Action<int> as the delegate type. If you use Action, you can still declare the delegate async (below) but the implicit Task object returned is ignored.

用于调用上述函数的lambda语法很简单,并且您在问题中使用的语法是正确的.请注意,此处不需要指定参数类型,因为可以推断出来:

The lambda syntax for calling the above function is straightforward and the syntax you used in the question is correct. Note that the parameter type doesn't need to be specified here since it can be inferred:

DoSomethingWithCallback(async (intParam) => { this.myint = await Int2IntAsync(intParam); });

如果愿意,还可以传递方法或委托变量,而不是使用lambda语法:

You can also pass a method or delegate variable, if you wish, instead of using the lambda syntax:

async Task MyInt2Int(int p) { ... }
Func<int,Task> myDelegate;
void OtherMethod()
{
    myDelegate = MyInt2Int;
    DoSomethingWithCallback(myDelegate); // this ...
    DoSomethingWithCallback(MyInt2Int);  // ... or this.
}

这篇关于传递异步委托的方法签名是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆