RuntimeHelpers 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一组静态方法和属性,这些方法和属性为编译器提供支持。 无法继承此类。
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
- 继承
-
RuntimeHelpers
- 属性
示例
以下示例演示如何使用 PrepareConstrainedRegions 方法可靠地设置句柄。 若要可靠地将句柄设置为指定的预先存在的句柄,必须确保本机句柄的分配以及 SafeHandle 对象中该句柄的后续记录是原子的。 这些操作(如线程中止或内存不足异常)之间的任何失败都将导致本机句柄泄露。 可以使用 PrepareConstrainedRegions 方法确保句柄未泄露。
[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
属性
OffsetToStringData |
已过时.
已过时.
获取给定字符串中数据的偏移量(以字节为单位)。 |