gpt4 book ai didi

c# - 将回调方法作为参数传递

转载 作者:行者123 更新时间:2023-12-02 01:08:58 26 4
gpt4 key购买 nike

我想将回调方法作为参数传递给通用方法,但不知道该怎么做。我试过 Func<IAsyncResult>但它似乎不兼容。

public void webRequest(string apiName, string requestMethod, string requestData, Func<IAsyncResult> callback)
{
...
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}

回调的签名是:

void GetRequestStreamCallback(IAsyncResult asyncResult)

最佳答案

将参数声明为Action<T>而不是 Func<T> .

public void webRequest(string apiName, string requestMethod, string requestData, Action<IAsyncResult> callback)

Func<IAsyncResult>需要一个不带参数并返回 IAsyncResult 的函数实例:

Func<TResult> Delegate

Encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter.

Action<T>不返回任何东西,只接受参数:

Action<T> Delegate

Encapsulates a method that has a single parameter and does not return a value.

关于c# - 将回调方法作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19290151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com