【问题标题】:c# interface implementation async awaitc#接口实现异步等待
【发布时间】:2015-04-01 19:41:47
【问题描述】:

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

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

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.... 没有实现。

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

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

【问题讨论】:

标签: c# interface async-await


【解决方案1】:

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

【讨论】:

  • 然后我将如何在其中使用我的异步操作?
  • @DavidDury 要么同步执行操作,而不是异步执行操作,要么不实现接口(可能改用不同的接口)。这当然是因为你不能修改有问题的界面。
  • 我期待这个答案,在过去的 3 个小时里尝试了一切......但感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 2013-01-05
  • 2017-04-17
相关资源
最近更新 更多