美工模板网站,天津建设工程信息网中标,怎么做子网站,太原哪家网站建设公司好从Console.ReadList/Read 的源码中#xff0c;可学习到.NET CORE 是怎么样来读取输入流。也可以学习到是如何使用P/Invoke来调用系统APIConsole.ReadList 的源码为[MethodImplAttribute(MethodImplOptions.NoInlining)]public static string ReadLine(){return In.ReadLine();… 从Console.ReadList/Read 的源码中可学习到.NET CORE 是怎么样来读取输入流。也可以学习到是如何使用P/Invoke来调用系统APIConsole.ReadList 的源码为 [MethodImplAttribute(MethodImplOptions.NoInlining)]public static string ReadLine(){return In.ReadLine();}其中In为。internal static T EnsureInitializedlt;Tgt;(ref T field, Funclt;Tgt; initializer) where T : class gt;LazyInitializer.EnsureInitialized(ref field, ref InternalSyncObject, initializer);public static TextReader In gt; EnsureInitialized(ref s_in, () gt; ConsolePal.GetOrCreateReader());可以看到他是个TextReadinternal static TextReader GetOrCreateReader(){Stream inputStream OpenStandardInput();return SyncTextReader.GetSynchronizedTextReader(inputStream Stream.Null ?StreamReader.Null :new StreamReader(stream: inputStream,encoding: new ConsoleEncoding(Console.InputEncoding),detectEncodingFromByteOrderMarks: false,bufferSize: Console.ReadBufferSize,leaveOpen: true));}继续跳转查看方法OpenStandardInputpublic static Stream OpenStandardInput(){return GetStandardFile(Interop.Kernel32.HandleTypes.STD_INPUT_HANDLE, FileAccess.Read);}继续看方法private static Stream GetStandardFile(int handleType, FileAccess access){IntPtr handle Interop.Kernel32.GetStdHandle(handleType);// 此处源码一坨注释被我删掉了。^_^if (handle IntPtr.Zero || handle InvalidHandleValue ||(access ! FileAccess.Read amp;amp; !ConsoleHandleIsWritable(handle))){return Stream.Null;}return new WindowsConsoleStream(handle, access, GetUseFileAPIs(handleType));}哈哈终于要看到了Interop.Kernel32.GetStdHandle 这个方法就是调用系统API接口函数的方法。lt;!-- Windows --gt;lt;ItemGroup Condition$(TargetsWindows) truegt;lt;Compile Include$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.GetStdHandle.csgt;lt;Linkgt;Common\CoreLib\Interop\Windows\Interop.GetStdHandle.cslt;/Linkgt;lt;/Compilegt;
lt;/ItemGroupgt;
lt;!-- Unix --gt;
lt;ItemGroup Condition $(TargetsUnix) truegt;
lt;/ItemGroupgt;回到GetStandardFile 中看到返回一个WindowsConsoleStreamuseFileAPIs 参数决定是使用操作系统 ReadFile还是 ReadConsole API。对于.NET CORE 源码中有很多 XXXX.Unix.cs,XXXX.Windows.cs