c# 8.0 抢先看-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 14855
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2018-12-19 17:30
文章分类

(8)

  • (1)
  • (1)
  • (2)
  • (1)
  • (1)
  • (1)
  • (1)
  • (0)
文章存档

(8)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: c#/.net

2018-12-23 18:48:01

如果一个方法要回传iasyncenumerable ,而方法内部使用yield return 该怎么写呢?
我们一样就拿readlineasync 来示范,首先建立一个类别实作iasyncenumerator ,当然这也包含了实作iasyncdisposable:

点击(此处)折叠或打开

  1. internal class asyncenumerator : iasyncenumerator<string>
  2.     {
  3.         private readonly streamreader _reader;

  4.         private bool _disposed;

  5.         public string current { get; private set; }

  6.         public asyncenumerator(string path)
  7.         {
  8.             _reader = file.opentext(path);
  9.             _disposed = false;
  10.         }
  11.         async public valuetask<bool> movenextasync()
  12.         {
  13.             var result = await _reader.readlineasync();
  14.             current = result;
  15.             return result != null;
  16.         }
  17.         async public valuetask disposeasync()
  18.         {
  19.             await task.run(() => dispose());
  20.         }

  21.         private void dispose()
  22.         {
  23.             dispose(true);
  24.             gc.suppressfinalize(this);
  25.         }

  26.         private void dispose(bool disposing)
  27.         {
  28.             if (!this._disposed)
  29.             {
  30.                 if (_reader != null)
  31.                 {
  32.                     _reader.dispose();
  33.                 }
  34.                 _disposed = true;
  35.             }
  36.         }
  37.     }

接着建立另外一个类别, 这个类别很简单,只包含一个静态的方法async static public iasyncenumerable readlineasync(string path),实作内容如下:

点击(此处)折叠或打开

  1. async static public iasyncenumerable<string> readlineasync(string path)
  2.         {

  3.             var enumerator = new asyncenumerator(path);
  4.             try
  5.             {
  6.                 while (await enumerator.movenextasync())
  7.                 {
  8.                     await task.delay(100);
  9.                     yield return enumerator.current;
  10.                 }
  11.             }
  12.             finally
  13.             {
  14.                 await enumerator.disposeasync();
  15.             }
  16.         }
  17.     }
程式码没有错,但编译过不了,观察一下错误讯息:

点击(此处)折叠或打开

  1. 错误cs0656:缺少编译器所需成员'system.threading.tasks.manualresetvaluetasksourcelogic`1.getresult'
  2. 错误cs0656:缺少编译器所需成员'system.threading.tasks.manualresetvaluetasksourcelogic`1.getstatus'
  3. 错误cs0656:缺少编译器所需的成员'系统。 threading.tasks.manualresetvaluetasksourcelogic`1.get_version'
  4. 错误cs0656:缺少编译器所需成员'system.threading.tasks.manualresetvaluetasksourcelogic`1.oncompleted'
  5. 错误cs0656:缺少编译器所需成员'system.threading.tasks.manualresetvaluetasksourcelogic`1.reset'
  6. 错误cs0656:缺少编译器所需的成员'system.threading.tasks.manualresetvaluetasksourcelogic`1.setexception'
  7. 错误cs0656:缺少编译器所需的成员'system.threading.tasks.manualresetvaluetasklogic.cn
  8. .setresult ' 错误cs0656:缺少编译器所需的成员'system.runtime.compilerservices.istrongbox`1.get_value'
  9. 错误cs0656:缺少编译器所需的成员

很明显,编译器需要两个型别(1) system.threading.tasks.manualresetvaluetasksourcelogic (2) system.runtime.compilerservices.istrongbox才能完成编译。感谢open source与git hub,在微软的dotnet/corclr的专案中找到了这么一段讨论~~ ,有位 (应该是微软员工而且是这个专案的成员)提到『it's not missing exactly, but like  said things are just out-of-sync between the compiler and library in preview 1. the compiler is looking for the old design (manualresetvaluetasksourcelogic and istrongbox) , while the libraries include the approved api surface area (manualresetvaluetasksourcecore), and we didn't have time to get the compiler updated.』,简单说就是编译器和框架目前的更新进度不一致,导致少了点什么。既然如此,我们就遵照本草纲目的指示,补上这两个型别,请注意,这两个型别的命名空间必须正确:

点击(此处)折叠或打开

  1. using system;
  2. using system.runtime.compilerservices;
  3. using system.threading.tasks.sources;


  4. namespace system.threading.tasks{
  5.    
  6.     internal struct manualresetvaluetasksourcelogic<tresult>
  7.     {
  8.         private manualresetvaluetasksourcecore<tresult> _core;
  9.         public manualresetvaluetasksourcelogic(istrongbox<manualresetvaluetasksourcelogic<tresult>> parent) : this() { }
  10.         public short version => _core.version;
  11.         public tresult getresult(short token) => _core.getresult(token);
  12.         public valuetasksourcestatus getstatus(short token) => _core.getstatus(token);
  13.         public void oncompleted(action<object> continuation, object state, short token, valuetasksourceoncompletedflags flags) => _core.oncompleted(continuation, state, token, flags);
  14.         public void reset() => _core.reset();
  15.         public void setresult(tresult result) => _core.setresult(result);
  16.         public void setexception(exception error) => _core.setexception(error);
  17.     }
  18. }

  19. namespace system.runtime.compilerservices
  20. {
  21.     internal interface istrongbox<t> { ref t value { get; } }
  22. }

补上去后就大功告成,可以快乐地非同步yielld return。
阅读(864) | 评论(0) | 转发(0) |
0

上一篇:

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图