防止F#中的垃圾收集器移动对象

时间:2011-02-09 14:40:42

标签: pointers f# garbage-collection

在C#中,很容易使用关键字“fixed”将对象固定到当前存储的位置。以下是MSDN的示例:

unsafe static void TestMethod()
{

    // assume class Point { public int x, y; }
    // pt is a managed variable, subject to garbage collection.
    Point pt = new Point();

    // Using fixed allows the address of pt members to be
    // taken, and "pins" pt so it isn't relocated.

    fixed (int* p = &pt.x)
    {
        *p = 1;
    }        

}

如何在F#中完成?

1 个答案:

答案 0 :(得分:10)

您可以使用{em>固定

类型的GCHandle