c#接口实现async await

Dav*_*ury 0 c# interface async-await

好的!这已经让我紧张了几个小时。

我正在使用提供的程序集,该程序集具有我需要实现的接口

public interface ICustomInterface
{
      CustomType DoSomething(string name);
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我喜欢这样:

public class MyClass: ICustomInterface
{
   public MyClass()
   {
   }

   // now I should implement the interface like this

   public CustomType DoSomething(string name)
   {
          CustomType nType = new CustomType();

          // do some work

          return nType;
   }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利,但在 MyClass 中的接口实现中,我需要使用,async await因此实现应该是这样的:

    public class MyClass: ICustomInterface
{
   public MyClass()
   {
   }

   // now I should implement the interface like this

   public async Task<CustomType> DoSomething(string name)
   {
          CustomType nType = new CustomType();

          await CallSomeMethodAsync();

          // do some extra work

          return nType;
   }
}
Run Code Online (Sandbox Code Playgroud)

当然这不起作用,因为它抱怨接口ICustomerInterface.DoSomething....没有实现。

有没有办法覆盖接受异步等待的接口实现?

我无法修改提供的程序集。

Ser*_*rvy 7

这不可能。如果接口要求操作同步计算,则操作需要同步执行。