CriticalFinalizerObject Class

Definition

Ensures that all finalization code in derived classes is marked as critical.

public abstract class CriticalFinalizerObject
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class CriticalFinalizerObject
Inheritance
CriticalFinalizerObject
Derived
Attributes

Examples

The following code example shows the use of the SafeFileHandle class to provide critical finalization for the standard input and output streams. The 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;
                }
            }
        }
    }
}

Remarks

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. For more information about CERs, see the System.Runtime.ConstrainedExecution namespace.

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. If a finalizer violates the rules for a CER, it might not successfully execute. 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. 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.

In most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. 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.

Constructors

CriticalFinalizerObject()

Initializes a new instance of the CriticalFinalizerObject class.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize()

Releases all the resources used by the CriticalFinalizerObject class.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also