RuntimeHelpers RuntimeHelpers RuntimeHelpers RuntimeHelpers Class

Definition

Provides a set of static methods and properties that provide support for compilers. This class cannot be inherited.

public ref class RuntimeHelpers abstract sealed
[System.Serializable]
public static class RuntimeHelpers
type RuntimeHelpers = class
Public Class RuntimeHelpers
Inheritance
RuntimeHelpersRuntimeHelpersRuntimeHelpersRuntimeHelpers
Attributes

Examples

The following example shows how to reliably set handles by using the PrepareConstrainedRegions method. To reliably set a handle to a specified pre-existing handle, you must ensure that the allocation of the native handle and the subsequent recording of that handle within a SafeHandle object is atomic. Any failure between these operations (such as a thread abort or out-of-memory exception) will result in the native handle being leaked. You can use the PrepareConstrainedRegions method to make sure that the handle is not leaked.

[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
    public IntPtr m_outputHandle;
}

sealed class MySafeHandle : SafeHandle
{
    // Called by P/Invoke when returning SafeHandles
    public MySafeHandle()
        : base(IntPtr.Zero, true)
    {
    }

    public MySafeHandle AllocateHandle()
    {
        // Allocate SafeHandle first to avoid failure later.
        MySafeHandle sh = new MySafeHandle();

        RuntimeHelpers.PrepareConstrainedRegions();
        try { }
        finally
        {
            MyStruct myStruct = new MyStruct();
            NativeAllocateHandle(ref myStruct);
            sh.SetHandle(myStruct.m_outputHandle);
        }

        return sh;
    }

<StructLayout(LayoutKind.Sequential)> _
Structure MyStruct
    Public m_outputHandle As IntPtr
End Structure 'MyStruct


NotInheritable Class MySafeHandle
    Inherits SafeHandle

    ' Called by P/Invoke when returning SafeHandles
    Public Sub New()
        MyBase.New(IntPtr.Zero, True)

    End Sub


    Public Function AllocateHandle() As MySafeHandle
        ' Allocate SafeHandle first to avoid failure later.
        Dim sh As New MySafeHandle()

        RuntimeHelpers.PrepareConstrainedRegions()
        Try
        Finally
            Dim myStruct As New MyStruct()
            NativeAllocateHandle(myStruct)
            sh.SetHandle(myStruct.m_outputHandle)
        End Try

        Return sh

    End Function


Properties

OffsetToStringData OffsetToStringData OffsetToStringData OffsetToStringData

Gets the offset, in bytes, to the data in the given string.

Methods

EnsureSufficientExecutionStack() EnsureSufficientExecutionStack() EnsureSufficientExecutionStack() EnsureSufficientExecutionStack()

Ensures that the remaining stack space is large enough to execute the average .NET Framework function.

Equals(Object, Object) Equals(Object, Object) Equals(Object, Object) Equals(Object, Object)

Determines whether the specified Object instances are considered equal.

ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers+TryCode, RuntimeHelpers+CleanupCode, Object) ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers+TryCode, RuntimeHelpers+CleanupCode, Object) ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers+TryCode, RuntimeHelpers+CleanupCode, Object) ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers+TryCode, RuntimeHelpers+CleanupCode, Object)

Executes code using a Delegate while using another Delegate to execute additional code in case of an exception.

GetHashCode(Object) GetHashCode(Object) GetHashCode(Object) GetHashCode(Object)

Serves as a hash function for a particular object, and is suitable for use in algorithms and data structures that use hash codes, such as a hash table.

GetObjectValue(Object) GetObjectValue(Object) GetObjectValue(Object) GetObjectValue(Object)

Boxes a value type.

GetUninitializedObject(Type) GetUninitializedObject(Type) GetUninitializedObject(Type) GetUninitializedObject(Type)
InitializeArray(Array, RuntimeFieldHandle) InitializeArray(Array, RuntimeFieldHandle) InitializeArray(Array, RuntimeFieldHandle) InitializeArray(Array, RuntimeFieldHandle)

Provides a fast way to initialize an array from data that is stored in a module.

IsReferenceOrContainsReferences<T>() IsReferenceOrContainsReferences<T>() IsReferenceOrContainsReferences<T>() IsReferenceOrContainsReferences<T>()
PrepareConstrainedRegions() PrepareConstrainedRegions() PrepareConstrainedRegions() PrepareConstrainedRegions()

Designates a body of code as a constrained execution region (CER).

PrepareConstrainedRegionsNoOP() PrepareConstrainedRegionsNoOP() PrepareConstrainedRegionsNoOP() PrepareConstrainedRegionsNoOP()

Designates a body of code as a constrained execution region (CER) without performing any probing.

PrepareContractedDelegate(Delegate) PrepareContractedDelegate(Delegate) PrepareContractedDelegate(Delegate) PrepareContractedDelegate(Delegate)

Provides a way for applications to dynamically prepare AppDomain event delegates.

PrepareDelegate(Delegate) PrepareDelegate(Delegate) PrepareDelegate(Delegate) PrepareDelegate(Delegate)

Indicates that the specified delegate should be prepared for inclusion in a constrained execution region (CER).

PrepareMethod(RuntimeMethodHandle) PrepareMethod(RuntimeMethodHandle) PrepareMethod(RuntimeMethodHandle) PrepareMethod(RuntimeMethodHandle)

Prepares a method for inclusion in a constrained execution region (CER).

PrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[]) PrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[]) PrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[]) PrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[])

Prepares a method for inclusion in a constrained execution region (CER) with the specified instantiation.

ProbeForSufficientStack() ProbeForSufficientStack() ProbeForSufficientStack() ProbeForSufficientStack()

Probes for a certain amount of stack space to ensure that a stack overflow cannot happen within a subsequent block of code (assuming that your code uses only a finite and moderate amount of stack space). We recommend that you use a constrained execution region (CER) instead of this method.

RunClassConstructor(RuntimeTypeHandle) RunClassConstructor(RuntimeTypeHandle) RunClassConstructor(RuntimeTypeHandle) RunClassConstructor(RuntimeTypeHandle)

Runs a specified class constructor method.

RunModuleConstructor(ModuleHandle) RunModuleConstructor(ModuleHandle) RunModuleConstructor(ModuleHandle) RunModuleConstructor(ModuleHandle)

Runs a specified module constructor method.

TryEnsureSufficientExecutionStack() TryEnsureSufficientExecutionStack() TryEnsureSufficientExecutionStack() TryEnsureSufficientExecutionStack()

Applies to