CriticalFinalizerObject 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
確保所有衍生類別中的最終化程式碼都被標記為關鍵。
public ref class CriticalFinalizerObject abstract
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class CriticalFinalizerObject
public abstract class CriticalFinalizerObject
[<System.Runtime.InteropServices.ComVisible(true)>]
type CriticalFinalizerObject = class
type CriticalFinalizerObject = class
Public MustInherit Class CriticalFinalizerObject
- 繼承
-
CriticalFinalizerObject
- 衍生
- 屬性
範例
以下程式碼範例展示了如何利用類別 SafeFileHandle 為標準輸入與輸出串流提供關鍵的最終化。 從SafeHandle類別衍生出的 SafeFileHandle,會傳入建構子中的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 類別衍生的類別中,通用語言執行時(CLR)保證所有關鍵的最終化程式碼都能執行,只要終結器遵守 CER 的規則,即使 CLR 強制卸載應用程式域或中止執行緒的情況。 若終結者違反 CER 規則,可能無法成功執行。 此外,CLR 在正常終結器與關鍵終結器之間建立了弱排序:對於同時被垃圾回收回收的物件,所有非關鍵終結器都會在任何關鍵終結器之前被呼叫。 例如,像 這樣的類別FileStream,儲存從 衍生出CriticalFinalizerObject的類別中的資料SafeHandle,可以執行標準的終結器來清除現有的緩衝資料。
在大多數情況下,你不需要寫出從該 CriticalFinalizerObject 類別衍生的類別。 .NET Framework 類別函式庫提供兩個類別,SafeHandle 與 CriticalHandle,提供 handle 資源的關鍵最終化功能。 此外,.NET Framework 提供一組由 SafeHandle 類別衍生的預寫類別,該集合位於 Microsoft.Win32.SafeHandles 命名空間中。 這些類別旨在提供支援檔案與作業系統帳號的共通功能。
建構函式
| 名稱 | Description |
|---|---|
| CriticalFinalizerObject() |
初始化 CriticalFinalizerObject 類別的新執行個體。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的 物件是否等於目前的物件。 (繼承來源 Object) |
| Finalize() |
釋放該職業使用的所有 CriticalFinalizerObject 資源。 |
| GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |