F#等同于析构函数 [英] F# Equivalent of Destructor

查看:98
本文介绍了F#等同于析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在翻译将非托管库包装为F#的C#类。我遇到了重写以下析构函数的看似简单的问题。

I am translating a C# class that wraps an unmanaged library to F#. I have run into the seemingly simple problem of rewriting the destructor that follows.

class Wrapper {

    // P/Invoke ellided

    private SomeType x;

    public Wrapper() {
        x = new SomeType();
        Begin();
    }

    public ~Wrapper() {
        End();
    }

我现在拥有的简化F#代码如下:

The simplified F# code I have at this point is as follows:

type Wrapper() =
  [<Literal>]
  static let wrappedDll = "Library.dll"

  [<DllImport(wrappedDll , EntryPoint = "Begin")>]
  static extern void Begin()

  [<DllImport(wrappedDll , EntryPoint = "End")>]
  static extern void End()

  let x = new SomeType()

  do
    Begin()

如何修改此F#代码以具有相同的行为?我对F#析构函数的搜索未在我拥有或在网络上的书中找到任何结果。

How can I modify this F# code to have the same behaviour? My search for F# destructor turned up no results in the books I have or on the web.

谢谢。

推荐答案

是否尝试过查找F#终结器?

Have you tried looking for F# finalizer?

override x.Finalize() = ...

这篇关于F#等同于析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆