Editare

Partajați prin


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
public ref class RuntimeHelpers sealed
public static class RuntimeHelpers
[System.Serializable]
public sealed class RuntimeHelpers
type RuntimeHelpers = class
[<System.Serializable>]
type RuntimeHelpers = class
Public Class RuntimeHelpers
Public NotInheritable Class RuntimeHelpers
Inheritance
RuntimeHelpers
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
Obsolete.
Obsolete.

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

Methods

AllocateTypeAssociatedMemory(Type, Int32)

Allocates memory that's associated with the type and is freed if and when the Type is unloaded.

Box(Byte, RuntimeTypeHandle)

Creates a boxed object of the specified type from the data located at the target reference.

CreateSpan<T>(RuntimeFieldHandle)

Provides a fast way to access constant data stored in a module as a ReadOnlySpan<T>.

EnsureSufficientExecutionStack()

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

Equals(Object, Object)

Determines whether the specified Object instances are considered equal.

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

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

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)

Boxes a value type.

GetSubArray<T>(T[], Range)

Slices the specified array using the specified range.

GetUninitializedObject(Type)

Returns an uninitialized instance of the system-provided type.

InitializeArray(Array, RuntimeFieldHandle)

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

IsReferenceOrContainsReferences<T>()

Returns a value that indicates whether the specified type is a reference type or a value type that contains references or by-refs.

PrepareConstrainedRegions()
Obsolete.

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

PrepareConstrainedRegionsNoOP()
Obsolete.

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

PrepareContractedDelegate(Delegate)
Obsolete.

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

PrepareDelegate(Delegate)

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

PrepareMethod(RuntimeMethodHandle)

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

PrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[])

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

ProbeForSufficientStack()
Obsolete.

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)

Ensures that the type initializer (also known as a static constructor) for the specified type has been run.

RunModuleConstructor(ModuleHandle)

Ensures a specified module constructor method has run by the time this method returns.

SizeOf(RuntimeTypeHandle)

Gets the size of an object of the given type.

TryEnsureSufficientExecutionStack()

Tries to ensure there is sufficient stack to execute the average .NET function.

Applies to