RuntimeHelpers Class

 

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

Namespace:   System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)

System.Object
  System.Runtime.CompilerServices.RuntimeHelpers

public static class RuntimeHelpers

NameDescription
System_CAPS_pubpropertySystem_CAPS_staticOffsetToStringData

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

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticEnsureSufficientExecutionStack()

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

System_CAPS_pubmethodSystem_CAPS_staticEquals(Object, Object)

Determines whether the specified Object instances are considered equal.

System_CAPS_pubmethodSystem_CAPS_staticExecuteCodeWithGuaranteedCleanup(RuntimeHelpers.TryCode, RuntimeHelpers.CleanupCode, Object)

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

System_CAPS_pubmethodSystem_CAPS_staticGetHashCode(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.

System_CAPS_pubmethodSystem_CAPS_staticGetObjectValue(Object)

Boxes a value type.

System_CAPS_pubmethodSystem_CAPS_staticInitializeArray(Array, RuntimeFieldHandle)

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

System_CAPS_pubmethodSystem_CAPS_staticPrepareConstrainedRegions()

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

System_CAPS_pubmethodSystem_CAPS_staticPrepareConstrainedRegionsNoOP()

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

System_CAPS_pubmethodSystem_CAPS_staticPrepareContractedDelegate(Delegate)

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

System_CAPS_pubmethodSystem_CAPS_staticPrepareDelegate(Delegate)

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

System_CAPS_pubmethodSystem_CAPS_staticPrepareMethod(RuntimeMethodHandle)

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

System_CAPS_pubmethodSystem_CAPS_staticPrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[])

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

System_CAPS_pubmethodSystem_CAPS_staticProbeForSufficientStack()

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.

System_CAPS_pubmethodSystem_CAPS_staticRunClassConstructor(RuntimeTypeHandle)

Runs a specified class constructor method.

System_CAPS_pubmethodSystem_CAPS_staticRunModuleConstructor(ModuleHandle)

Runs a specified module constructor method.

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;
    }

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: