CriticalFinalizerObject 类

定义

确保派生类中的所有终止代码均标记为关键。Ensures that all finalization code in derived classes is marked as critical.

public ref class CriticalFinalizerObject abstract
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class CriticalFinalizerObject
type CriticalFinalizerObject = class
Public MustInherit Class CriticalFinalizerObject
继承
CriticalFinalizerObject
派生
属性

示例

下面的代码示例演示如何使用SafeFileHandle类为标准输入流和输出流提供关键终结。The following code example shows the use of the SafeFileHandle class to provide critical finalization for the standard input and output streams. 派生SafeFileHandle FileStream自类的将传递到构造函数中的文件流。 SafeHandleThe SafeFileHandle, derived from the SafeHandle class, is passed to the file stream in the FileStream constructor.

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)。Classes deriving from the CriticalFinalizerObject class are implicitly treated as a constrained execution region (CER). 这需要终结器中的代码才能调用具有强可靠性协定的代码。This requires code in the finalizer to only call code with a strong reliability contract. 有关 cer 的详细信息, 请参阅System.Runtime.ConstrainedExecution命名空间。For more information about CERs, see the System.Runtime.ConstrainedExecution namespace.

在从CriticalFinalizerObject类派生的类中, 公共语言运行时 (CLR) 保证所有关键的终止代码都可以执行, 前提是终结器遵循 CER 的规则, 即使在 CLR强制卸载应用程序域或中止线程。In classes derived from the CriticalFinalizerObject class, the common language runtime (CLR) guarantees that all critical finalization code will be given the opportunity to execute, provided the finalizer follows the rules for a CER, even in situations where the CLR forcibly unloads an application domain or aborts a thread. 如果终结器违反 CER 的规则, 则可能无法成功执行。If a finalizer violates the rules for a CER, it might not successfully execute. 此外, CLR 还会在正常和关键终结器之间建立弱顺序: 对于同时由垃圾回收器回收的对象, 所有非关键终结器都将在任何关键终结器前调用。In addition, the CLR establishes a weak ordering among normal and critical finalizers: for objects reclaimed by garbage collection at the same time, all the noncritical finalizers are called before any of the critical finalizers. 例如, 类 ( FileStream如) 保存派生自的SafeHandle类中的数据, 可以运行标准CriticalFinalizerObject终结器来刷新现有的缓冲数据。For example, a class such as FileStream, which holds data in the SafeHandle class that is derived from CriticalFinalizerObject, can run a standard finalizer to flush out existing buffered data.

大多数情况下, 不需要编写从CriticalFinalizerObject类派生的类。In most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. .NET Framework 类库提供了两个类, SafeHandleCriticalHandle, 它们为句柄资源提供关键终止功能。The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. 此外, .NET Framework 提供一组派生自SafeHandle类的预编写类, 并且此集位于Microsoft.Win32.SafeHandles命名空间中。Furthermore, the .NET Framework provides a set of prewritten classes derived from the SafeHandle class, and this set is located in the Microsoft.Win32.SafeHandles namespace. 这些类旨在提供支持文件和操作系统句柄的通用功能。These classes are designed to provide common functionality for supporting file and operating system handles.

构造函数

CriticalFinalizerObject()

初始化 CriticalFinalizerObject 类的新实例。Initializes a new instance of the CriticalFinalizerObject class.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

释放由 CriticalFinalizerObject 类使用的所有资源。Releases all the resources used by the CriticalFinalizerObject class.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

安全性

SecurityPermission
用于调用非托管代码的权限。for permission to call unmanaged code. 安全操作: LinkDemandSecurity action: LinkDemand. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode

InheritanceDemand
完全信任继承者。for full trust for inheritors. 此类不能由部分信任的代码使用。This class cannot be used by partially trusted code.

适用于

另请参阅