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

使用 IDisposable 和“使用"是否濫用?作為獲得“

Is it abusive to use IDisposable and quot;usingquot; as a means for getting quot;scoped behaviorquot; for exception safety?(使用 IDisposable 和“使用是否濫用?作為獲得“范圍行為的一種手段為了異常安全?) - IT屋-程序
本文介紹了使用 IDisposable 和“使用"是否濫用?作為獲得“范圍行為"的一種手段為了異常安全?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我經常在 C++ 中使用的東西是讓一個類 A 通過 A 處理另一個類 B 的狀態進入和退出條件> 構造函數和析構函數,以確保如果該范圍內的某些內容引發異常,則 B 在退出范圍時將具有已知狀態.就首字母縮略詞而言,這并不是純粹的 RAII,但它仍然是一種既定模式.

Something I often used back in C++ was letting a class A handle a state entry and exit condition for another class B, via the A constructor and destructor, to make sure that if something in that scope threw an exception, then B would have a known state when the scope was exited. This isn't pure RAII as far as the acronym goes, but it's an established pattern nevertheless.

在C#中,我經常想做的事

In C#, I often want to do

class FrobbleManager
{
    ...

    private void FiddleTheFrobble()
    {
        this.Frobble.Unlock();
        Foo();                  // Can throw
        this.Frobble.Fiddle();  // Can throw
        Bar();                  // Can throw
        this.Frobble.Lock();
    }
}

需要這樣做

private void FiddleTheFrobble()
{
    this.Frobble.Unlock();

    try
    {            
        Foo();                  // Can throw
        this.Frobble.Fiddle();  // Can throw
        Bar();                  // Can throw
    }
    finally
    {
        this.Frobble.Lock();
    }
}

如果我想保證 FiddleTheFrobble 返回時的 Frobble 狀態.代碼會更好

if I want to guarantee the Frobble state when FiddleTheFrobble returns. The code would be nicer with

private void FiddleTheFrobble()
{
    using (var janitor = new FrobbleJanitor(this.Frobble))
    {            
        Foo();                  // Can throw
        this.Frobble.Fiddle();  // Can throw
        Bar();                  // Can throw
    }
}

FrobbleJanitor看起來大致

class FrobbleJanitor : IDisposable
{
    private Frobble frobble;

    public FrobbleJanitor(Frobble frobble)
    {
        this.frobble = frobble;
        this.frobble.Unlock();
    }

    public void Dispose()
    {
        this.frobble.Lock();
    }
}

這就是我想要做的.現在現實趕上了,因為我 想要 使用 需要 FrobbleJanitor 使用 with using.我可以認為這是一個代碼審查問題,但有些事情困擾著我.

And that's how I want to do it. Now reality catches up, since what I want to use requires that the FrobbleJanitor is used with using. I could consider this a code review issue, but something is nagging me.

問題:以上是否會被視為濫用usingIDisposable?

Question: Would the above be considered as abusive use of using and IDisposable?

推薦答案

我不這么認為,一定.IDisposable 在技術上 is 意味著用于具有非托管資源的事物,但是 using 指令只是實現 try .. finally { dispose }.

I don't think so, necessarily. IDisposable technically is meant to be used for things that have non-managed resources, but then the using directive is just a neat way of implementing a common pattern of try .. finally { dispose }.

純粹主義者會爭辯說是的 - 這是濫用",而在純粹主義者的意義上它是;但我們大多數人不是從純粹主義的角度編碼,而是從半藝術的角度編碼.在我看來,以這種方式使用使用"結構確實很有藝術性.

A purist would argue 'yes - it's abusive', and in the purist sense it is; but most of us do not code from a purist perspective, but from a semi-artistic one. Using the 'using' construct in this way is quite artistic indeed, in my opinion.

您可能應該在 IDisposable 之上粘貼另一個接口以將其推得更遠,向其他開發人員解釋為什么該接口意味著 IDisposable.

You should probably stick another interface on top of IDisposable to push it a bit further away, explaining to other developers why that interface implies IDisposable.

還有很多其他的替代方法可以做到這一點,但最終,我想不出有什么比這更整潔,所以去吧!

There are lots of other alternatives to doing this but, ultimately, I can't think of any that will be as neat as this, so go for it!

這篇關于使用 IDisposable 和“使用"是否濫用?作為獲得“范圍行為"的一種手段為了異常安全?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Is there a C# library that will perform the Excel NORMINV function?(是否有執行 Excel NORMINV 函數的 C# 庫?)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權列表中選擇 x 個隨機元素(無需替換))
Create a summary description of a schedule given a list of shifts(給定輪班列表,創建時間表的摘要描述)
C# Normal Random Number(C# 普通隨機數)
Standard deviation of generic list?(通用列表的標準偏差?)
AsyncCTP: Creating a class that is IAwaitable(AsyncCTP:創建一個 IAwaitable 的類)
主站蜘蛛池模板: 国产精品视频久久久 | 五月综合激情在线 | 国产一级片91 | 亚洲高清视频一区二区 | 国产黄色在线观看 | 欧美日韩一二区 | 99久久婷婷国产综合精品电影 | 久久成人av | 午夜精品久久久久久久久久久久久 | 免费日本视频 | 成年人网站免费视频 | 就操在线 | 中文字幕亚洲精品 | 欧美日韩不卡合集视频 | 欧美日韩电影免费观看 | 国产黄色小视频在线观看 | 狠狠亚洲 | 亚洲欧美一区二区三区国产精品 | 欧美一区二区免费视频 | 一区二区三区在线播放 | 中文字幕一区二区三 | 性生生活大片免费看视频 | 日本一区二区三区精品视频 | 在线看免费 | 在线看片国产精品 | 日本一区二区三区四区 | 99在线免费观看视频 | 天天色天天射天天干 | 九九久久精品 | 国产精品中文字幕在线播放 | 五月婷婷中文 | 日韩日韩日韩日韩日韩日韩日韩 | 91视频正在播放 | 亚洲精品一区二区三区中文字幕 | 精品国产乱码久久久久久丨区2区 | 一级做a爰片性色毛片16 | 国产精品.xx视频.xxtv | 中文字幕在线电影观看 | 中文字幕第90页 | 欧美日韩中文字幕在线 | 欧美在线视频一区二区 |