C# 使用委托调用async以将方法调用传递给方法
我有下面的代码,我正试图编写一个委托,允许服务调用被传递到方法中,但我真的很挣扎。 有人能建议这是怎么可能的吗 非常感谢, 这是一个简单的示例,但是请注意这两个方法是如何相同的,除了它们各自进行的内部服务调用。我想把他们调用的方法传递给DoIt方法,即 示例C# 使用委托调用async以将方法调用传递给方法,c#,delegates,C#,Delegates,我有下面的代码,我正试图编写一个委托,允许服务调用被传递到方法中,但我真的很挣扎。 有人能建议这是怎么可能的吗 非常感谢, 这是一个简单的示例,但是请注意这两个方法是如何相同的,除了它们各自进行的内部服务调用。我想把他们调用的方法传递给DoIt方法,即 示例 var result1 = DoIt(await _service.GetMethodA(..)) var result2 = DoIt(await _service.GetMethodB(..)) async Task<ICus
var result1 = DoIt(await _service.GetMethodA(..))
var result2 = DoIt(await _service.GetMethodB(..))
async Task<ICustomer> ICustomerSomething.GetIt(Guid pupilId)
{
var t = await _service.GetMethodA(..)
}
async Task<ICustomer> ICustomerSomething.GetSomethingElse(Guid pupilId)
{
var t = await _service.GetMethodB(..)
}
public async Task<IHttpActionResult> DoIt(Func<Task<IHttpActionResult>> func)
{
return await func();
}
public async Task<IHttpActionResult> Get(...)
{
DoIt(() => _myService.CallinternalService(...));
}
要更改的代码
var result1 = DoIt(await _service.GetMethodA(..))
var result2 = DoIt(await _service.GetMethodB(..))
async Task<ICustomer> ICustomerSomething.GetIt(Guid pupilId)
{
var t = await _service.GetMethodA(..)
}
async Task<ICustomer> ICustomerSomething.GetSomethingElse(Guid pupilId)
{
var t = await _service.GetMethodB(..)
}
public async Task<IHttpActionResult> DoIt(Func<Task<IHttpActionResult>> func)
{
return await func();
}
public async Task<IHttpActionResult> Get(...)
{
DoIt(() => _myService.CallinternalService(...));
}
异步任务ICCustomerSomething.GetIt(Guid pupilId)
{
var t=wait_service.GetMethodA(..)
}
异步任务ICCustomerSomethingElse.GetSomethingElse(Guid pupilId)
{
var t=wait_service.GetMethodB(..)
}
签名
async Task<ICustomer> ICustomerService.Get(Guid pupilId)
async Task<IEnumerable<ICar>> ICustomerService.GetSomethingElse(Guid pupilId)
异步任务ICCustomerService.Get(Guid pupilId)
异步任务ICCustomerService.GetSomethingElse(Guid pupilId)
更新
var result1 = DoIt(await _service.GetMethodA(..))
var result2 = DoIt(await _service.GetMethodB(..))
async Task<ICustomer> ICustomerSomething.GetIt(Guid pupilId)
{
var t = await _service.GetMethodA(..)
}
async Task<ICustomer> ICustomerSomething.GetSomethingElse(Guid pupilId)
{
var t = await _service.GetMethodB(..)
}
public async Task<IHttpActionResult> DoIt(Func<Task<IHttpActionResult>> func)
{
return await func();
}
public async Task<IHttpActionResult> Get(...)
{
DoIt(() => _myService.CallinternalService(...));
}
我已经做了建议的更改,尽管它没有编译,如下所示
错误消息
var result1 = DoIt(await _service.GetMethodA(..))
var result2 = DoIt(await _service.GetMethodB(..))
async Task<ICustomer> ICustomerSomething.GetIt(Guid pupilId)
{
var t = await _service.GetMethodA(..)
}
async Task<ICustomer> ICustomerSomething.GetSomethingElse(Guid pupilId)
{
var t = await _service.GetMethodB(..)
}
public async Task<IHttpActionResult> DoIt(Func<Task<IHttpActionResult>> func)
{
return await func();
}
public async Task<IHttpActionResult> Get(...)
{
DoIt(() => _myService.CallinternalService(...));
}
“System.Threading.Tasks.Task>”到“System.Threading.Tasks.Task”MyService
更改后的代码
var result1 = DoIt(await _service.GetMethodA(..))
var result2 = DoIt(await _service.GetMethodB(..))
async Task<ICustomer> ICustomerSomething.GetIt(Guid pupilId)
{
var t = await _service.GetMethodA(..)
}
async Task<ICustomer> ICustomerSomething.GetSomethingElse(Guid pupilId)
{
var t = await _service.GetMethodB(..)
}
public async Task<IHttpActionResult> DoIt(Func<Task<IHttpActionResult>> func)
{
return await func();
}
public async Task<IHttpActionResult> Get(...)
{
DoIt(() => _myService.CallinternalService(...));
}
公共异步任务DoIt(Func-Func)
{
return wait func();
}
公共异步任务Get(…)
{
DoIt(()=>_myService.CallinternalService(…);
}
,异步方法与同步方法类似,只是它们返回任务
/任务
,而不是无效
/T
。因此,异步方法的委托是Func
s,返回Task
/Task
在这种情况下:
Func<.., Task<TResult>>
或者,您可以在lambda表达式中传递参数。
,这将委托类型简化为仅Func
,如下所示:
public async Task<TResult> DoIt(Func<Task<TResult>> func)
{
return await func();
}
DoIt(() => _service.GetMethodA(..));
公共异步任务DoIt(Func-Func)
{
return wait func();
}
DoIt(()=>_service.GetMethodA(..);
System.Threading.Tasks.Task DoIt(Func-Func)
{
返回func();
}
async System.Threading.Tasks.Task Main()
{
等待DoIt(()=>System.Threading.Tasks.Task.FromResult(1));
等待DoIt(()=>System.Threading.Tasks.Task.FromResult(“”);
}
你能发布方法GetMethodA
和GetMethodB
的签名吗?至少参数
和返回类型