GCHandle.Alloc 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為指定物件分配一個句柄。
多載
| 名稱 | Description |
|---|---|
| Alloc(Object) |
為 Normal 指定物件分配一個句柄。 |
| Alloc(Object, GCHandleType) |
為指定物件分配指定類型的句柄。 |
Alloc(Object)
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
為 Normal 指定物件分配一個句柄。
public:
static System::Runtime::InteropServices::GCHandle Alloc(System::Object ^ value);
[System.Security.SecurityCritical]
public static System.Runtime.InteropServices.GCHandle Alloc(object value);
public static System.Runtime.InteropServices.GCHandle Alloc(object? value);
public static System.Runtime.InteropServices.GCHandle Alloc(object value);
[<System.Security.SecurityCritical>]
static member Alloc : obj -> System.Runtime.InteropServices.GCHandle
static member Alloc : obj -> System.Runtime.InteropServices.GCHandle
Public Shared Function Alloc (value As Object) As GCHandle
參數
傳回
一個 GCHandle 能保護物件免於垃圾回收的新裝置。 GCHandle當不再需要時,必須隨Free()之解除。
- 屬性
範例
以下範例展示了 App 一個類別,該類別使用 該方法建立一個 handle 到 受 GCHandle.Alloc 管理物件,阻止該受管理物件被收集。 呼叫該 EnumWindows 方法會傳遞代理(deleged)和受管理物件(兩者皆宣告為受管理型別,但未顯示),並將 handle 投射為 IntPtr。 未管理函式會將型別作為回調函式的參數傳回呼叫者。
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public delegate bool CallBack(int handle, IntPtr param);
internal static class NativeMethods
{
// passing managed object as LPARAM
// BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
[DllImport("user32.dll")]
internal static extern bool EnumWindows(CallBack cb, IntPtr param);
}
public class App
{
public static void Main()
{
Run();
}
public static void Run()
{
TextWriter tw = Console.Out;
GCHandle gch = GCHandle.Alloc(tw);
CallBack cewp = new CallBack(CaptureEnumWindowsProc);
// platform invoke will prevent delegate to be garbage collected
// before call ends
NativeMethods.EnumWindows(cewp, GCHandle.ToIntPtr(gch));
gch.Free();
}
private static bool CaptureEnumWindowsProc(int handle, IntPtr param)
{
GCHandle gch = GCHandle.FromIntPtr(param);
TextWriter tw = (TextWriter)gch.Target;
tw.WriteLine(handle);
return true;
}
}
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Public Delegate Function CallBack(ByVal handle As Integer, ByVal param As IntPtr) As Boolean
Friend Module NativeMethods
' passing managed object as LPARAM
' BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam);
<DllImport("user32.dll")>
Friend Function EnumWindows(ByVal cb As CallBack, ByVal param As IntPtr) As Boolean
End Function
End Module
Module App
Sub Main()
Run()
End Sub
<SecurityPermission(SecurityAction.Demand, UnmanagedCode:=True)>
Sub Run()
Dim tw As TextWriter = Console.Out
Dim gch As GCHandle = GCHandle.Alloc(tw)
Dim cewp As CallBack
cewp = AddressOf CaptureEnumWindowsProc
' platform invoke will prevent delegate to be garbage collected
' before call ends
NativeMethods.EnumWindows(cewp, GCHandle.ToIntPtr(gch))
gch.Free()
End Sub
Function CaptureEnumWindowsProc(ByVal handle As Integer, ByVal param As IntPtr) As Boolean
Dim gch As GCHandle = GCHandle.FromIntPtr(param)
Dim tw As TextWriter = CType(gch.Target, TextWriter)
tw.WriteLine(handle)
Return True
End Function
End Module
備註
Normal handle 是不透明的,意思是你無法透過handle解析其所包含物件的位址。
另請參閱
適用於
Alloc(Object, GCHandleType)
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
- 來源:
- GCHandle.cs
為指定物件分配指定類型的句柄。
public:
static System::Runtime::InteropServices::GCHandle Alloc(System::Object ^ value, System::Runtime::InteropServices::GCHandleType type);
[System.Security.SecurityCritical]
public static System.Runtime.InteropServices.GCHandle Alloc(object value, System.Runtime.InteropServices.GCHandleType type);
public static System.Runtime.InteropServices.GCHandle Alloc(object? value, System.Runtime.InteropServices.GCHandleType type);
public static System.Runtime.InteropServices.GCHandle Alloc(object value, System.Runtime.InteropServices.GCHandleType type);
[<System.Security.SecurityCritical>]
static member Alloc : obj * System.Runtime.InteropServices.GCHandleType -> System.Runtime.InteropServices.GCHandle
static member Alloc : obj * System.Runtime.InteropServices.GCHandleType -> System.Runtime.InteropServices.GCHandle
Public Shared Function Alloc (value As Object, type As GCHandleType) As GCHandle
參數
- type
- GCHandleType
其中一個 GCHandleType 值,表示要創建的 GCHandle 類型。
傳回
一個指定類型的新機 GCHandle 。 GCHandle當不再需要時,必須隨Free()之解除。
- 屬性
例外狀況
具有非原始(非可位塊)成員的實例無法被釘選。