c#接口实现异步等待

时间:2015-04-01 19:41:48

标签: c# interface async-await

确定!这已经让我感到压力了几个小时。

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

public interface ICustomInterface
{
      CustomType DoSomething(string name);
}

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

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;
   }
}

到目前为止一直很好但是在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;
   }
}

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

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

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

1 个答案:

答案 0 :(得分:4)

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