久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

避免處理底層流

Avoiding dispose of underlying stream(避免處理底層流)
本文介紹了避免處理底層流的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試模擬一些文件操作.在我擁有的真實"對象中:

I'm attempting to mock some file operations. In the "real" object I have:

StreamWriter createFile( string name )
{
  return new StreamWriter( Path.Combine( _outFolder, name ), false, Encoding.UTF8 ) );
}

在我希望喜歡的模擬對象中:

In the mock object I'd like to have:

StreamWriter createFile( string name )
{
  var ms = new MemoryStream();
  _files.Add( Path.Combine( _outFolder, name ), ms );
  return new StreamWriter( ms, Encoding.UTF8 ) );
}

其中 _files 是一個字典,用于存儲創建的文件以供以后檢查.

where _files is a dictionary to store created files for later inspection.

但是,當消費者關閉 StreamWriter 時,它釋放 MeamoryStream... :-(

However, when the consumer closes the StreamWriter, it also disposes the MeamoryStream... :-(

對如何追求這個有什么想法嗎?

Any thoughts on how to pursue this?

推薦答案

如果將 MemoryStream 子類化,這將起作用,但您必須調用 ManualDispose 方法來關閉底層流.
我不確定,但我認為這個對象超出范圍時會被垃圾回收.

If you subclass the MemoryStream, this will work but you have to call ManualDispose method to close the underlying stream.
I′m not sure but I think this object will be garbage-collected when it goes out of scope.

public sealed class ManualMemoryStream : MemoryStream
{
    protected override void Dispose(bool disposing)
    {
    }
    public void ManualDispose()
    {
        base.Dispose(true);
    }
}


如果您希望 MemoryStream 被刷新并準備好從頂部讀取,這是一種替代方法.


This is an alternative if you want the MemoryStream to be flushed and ready to be read from top.

public sealed class ManualMemoryStream : MemoryStream
{
    protected override void Dispose(bool disposing)
    {
        Flush();
        Seek(0, SeekOrigin.Begin);
    }
    public void ManualDispose()
    {
        base.Dispose(true);
    }
}

這篇關于避免處理底層流的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 欧美2区 | 91精品国产综合久久久久蜜臀 | 艹逼网 | 黄网站色大毛片 | 综合久久一区 | 在线观看成年人视频 | 一级黄色日本片 | 中文字幕国产 | 97伦理电影网 | 亚洲一级在线 | 日韩成人一区 | 欧美亚洲激情 | 国产精品亚洲精品 | 国产在线观看一区二区三区 | 亚洲国产成人精品久久久国产成人一区 | 日韩快播电影网 | 中文字幕久久精品 | 久久香蕉精品视频 | 欧美一区二区免费 | 91精品国产日韩91久久久久久 | 亚洲精品一区二区三区蜜桃久 | 欧美成视频 | 久久久123 | 黄片毛片免费看 | 精品久久久久久久久久久久 | 亚洲欧美另类在线观看 | 99热激情| 亚洲午夜av久久乱码 | 日韩视频 中文字幕 | 日韩影音 | 久久久高清 | 在线视频国产一区 | 国产乱码精品一品二品 | 欧美精品一区二区在线观看 | 97人人干| 国产精品久久久久久久久久久久久久 | 天堂av中文在线 | 成人黄色电影免费 | 99色综合 | 一久久久| 国产伦精品一区二区三区四区视频 |