CriticalFinalizerObject 類別

定義

確保衍生類別中所有的最終處理程式碼都標記為關鍵。

public ref class CriticalFinalizerObject abstract
public abstract class CriticalFinalizerObject
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class CriticalFinalizerObject
type CriticalFinalizerObject = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type CriticalFinalizerObject = class
Public MustInherit Class CriticalFinalizerObject
繼承
CriticalFinalizerObject
衍生
屬性

範例

下列程式碼範例示範如何使用 SafeFileHandle 類別來提供標準輸入和輸出資料流程的重要最終處理。 衍生自 類別的 SafeHandleSafeFileHandle 會傳遞至建構函式中的 FileStream 檔案資料流程。

using System;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32.SafeHandles;

namespace CriticalFinalizer
{
    class Program
    {
        const int STD_INPUT_HANDLE   = -10;
        const int STD_OUTPUT_HANDLE = -11;
        const int STD_ERROR_HANDLE  =  -12;
        [DllImport("Kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern IntPtr GetStdHandle(int type);

        static void Main(string[] args)
        {
            FileStream fsIn = null;
            FileStream fsOut = null;
            try
            {
                SafeFileHandle sfhIn = new SafeFileHandle(GetStdHandle(STD_INPUT_HANDLE), false);
                fsIn = new FileStream(sfhIn, FileAccess.Read);
                byte[] input = new byte[] {0};
                fsIn.Read(input,0,1);
                SafeFileHandle sfhOut = new SafeFileHandle(GetStdHandle(STD_OUTPUT_HANDLE), false);
                fsOut = new FileStream(sfhOut, FileAccess.Write);
                fsOut.Write(input,0,1);
                SafeFileHandle sf = fsOut.SafeFileHandle;
            }
            finally
            {
                if (fsIn != null)
                {
                    fsIn.Close();
                    fsIn = null;
                }
                if (fsOut != null)
                {
                    fsOut.Close();
                    fsOut = null;
                }
            }
        }
    }
}
Imports System.Runtime.InteropServices
Imports System.IO
Imports Microsoft.Win32.SafeHandles

Public Module Example
   Const STD_INPUT_HANDLE As Integer  = -10
   Const STD_OUTPUT_HANDLE As Integer = -11
   Const STD_ERROR_HANDLE As Integer  = -12

   Public Declare Auto Function GetStdHandle Lib "Kernel32" (type As Integer) As IntPtr

   Public Sub Main()
      Dim fsIn As FileStream = Nothing
      Dim fsOut As FileStream = Nothing

      Try
         Dim sfhIn As New SafeFileHandle(GetStdHandle(STD_INPUT_HANDLE), False)
         fsIn = new FileStream(sfhIn, FileAccess.Read)
         Dim input() As Byte = { 0 }
         fsIn.Read(input, 0, 1)
         Dim sfhOut As New SafeFileHandle(GetStdHandle(STD_OUTPUT_HANDLE), False)
         fsOut = New FileStream(sfhOut, FileAccess.Write)
         fsOut.Write(input, 0, 1)
         Dim sf As SafeFileHandle = fsOut.SafeFileHandle
      Finally
         If fsIn IsNot Nothing Then
            fsIn.Close()
            fsIn = Nothing
         End If
         If fsOut IsNot Nothing Then 
            fsOut.Close()
            fsOut = Nothing
         End If
      End Try
   End Sub
End Module

備註

衍生自 類別的 CriticalFinalizerObject 類別會隱含視為限制的執列區域, (CER) 。 這需要完成項中的程式碼,才能呼叫具有強可靠性合約的程式碼。 如需 CER 的詳細資訊,請參閱 System.Runtime.ConstrainedExecution 命名空間。

在衍生自 類別的 CriticalFinalizerObject 類別中,Common Language Runtime (CLR) 保證所有重要的最終處理常式代碼都會有機會執行,前提是完成項會遵循 CER 的規則,即使 CLR 強制卸載應用程式域或中止執行緒的情況也一樣。 如果完成項違反 CER 的規則,可能無法成功執行。 此外,CLR 會在一般和重大完成項之間建立弱式排序:針對垃圾收集同時回收的物件,所有非關鍵完成項都會在任一重大完成項之前呼叫。 例如,這類 FileStream 類別會保存衍生自 CriticalFinalizerObjectSafeHandle 類別中的資料,可以執行標準完成項來清除現有的緩衝資料。

在大部分情況下,您不需要撰寫衍生自 類別的 CriticalFinalizerObject 類別。 .NET Framework類別庫提供兩個類別, SafeHandle 以及 CriticalHandle 提供處理資源的重要最終處理功能。 此外,.NET Framework提供一組衍生自 SafeHandle 類別的預先寫入類別,而此集合位於 命名空間中 Microsoft.Win32.SafeHandles 。 這些類別的設計目的是為了提供支援檔案和作業系統控制碼的一般功能。

建構函式

CriticalFinalizerObject()

初始化 CriticalFinalizerObject 類別的新執行個體。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Finalize()

釋放 CriticalFinalizerObject 類別使用的所有資源。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱