如何修复这个以func为参数的.NET方法?

时间:2016-11-12 13:14:00

标签: c# .net async-await func

我有以下方法

public async Task<dynamic> FooAsync(dynamic parameters, 
                                    CancellationToken cancellationToken)
{..}

但我似乎无法找到使用方法正确调用它的方法:

public async Task<dynamic> TryAsync(Func<dynamic, CancellationToken, Task<dynamic>> func)
{
    try
    {
        await func(); // Doesn't compile. Says it missing the 2 arguments.
    }
    catch(Exception exception)
    {
        ..
    }
}

e.g。将FooAsync传递给TryAsync ..

的代码
public async Task PewPewAsync(..)
{
    ...
    // TODO: Get this to work/compile :(
    var result = await TryAsync(FooAsync(parameters, cancellationToken);
    ...
}

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:3)

你有一个带有两个参数(dynamicCancellationToken)的委托,你试图用零参数调用它。

根据您的目的,您可以让TryAsync接受这两个参数,然后在调用它时传递它们:

public async Task<dynamic> TryAsync(
    Func<dynamic, CancellationToken, Task<dynamic>> func,
    dynamic parameters, CancellationToken cancellationToken)
{
    try
    {
        return await func(parameters, cancellationToken);
    }
    catch(Exception exception)
    {
        ..
    }
}

TryAsync(FooAsync, parameters, cancellationToken);

或者您可以使用没有任何参数的委托并在调用时使用lambda:

public async Task<dynamic> TryAsync(Func<Task<dynamic>> func)
{
    try
    {
        return await func();
    }
    catch(Exception exception)
    {
        ..
    }
}

TryAsync(() => FooAsync(parameters, cancellationToken));

答案 1 :(得分:0)

试试这个

public async Task<dynamic> TryAsync(Func<dynamic, CancellationToken, Task<dynamic>> func, dynamic parameters, CancellationToken cancellationToken)
{
    try
    {
        await func(parameters, cancellationToken);
    }
    catch(Exception exception)
    {
        ..
    }
}

并像这样称呼它

public async Task PewPewAsync(..)
{
    ...
    var result = await TryAsync(FooAsync, parameters, cancellationToken);
    ...
}

原因是您需要将参数传递给要单独调用/包装的函数,以便将它们传递给实际的调用。在原始代码中,您调用函数并将返回值作为参数传递给TryAsync,而不是传递对函数本身的引用。

答案 2 :(得分:0)

这取决于您尝试使用TryAsync实现的目标。一种方法在另一个答案中显示。另一种方式是

public async Task<dynamic> TryAsync(Func<Task<dynamic>> func) {
    try {
        var result = await func();
        return result;
    }
    catch (Exception exception) {
        // do something
        return null;
    }
}

像这样打电话

var result = await TryAsync(() => FooAsync(parameters, cancellationToken));

答案 3 :(得分:0)

这取决于 - 你想什么时候执行FooAsync

TryAsync或之前?如果要执行TryAsync中的所有内容,可以使用:

    public async Task<dynamic> FooAsync(dynamic parameters, CancellationToken cancellationToken) {
        return await Task.FromResult("x");
    }

    public async Task<dynamic> TryAsync(Func<Task<dynamic>> func) {
        try {
            return await func(); // Doesn't compile. Says it missing the 2 arguments.
        }
        catch (Exception exception) {
            return Task.FromResult(false);
        }
    }

    public async Task PewPewAsync() {
        var parameters = "x";
        var cancellationToken = CancellationToken.None;
        var result = await TryAsync(() => FooAsync(parameters, cancellationToken));
    }