如何解决F#等同于析构函数
| 我正在翻译将非托管库包装为F#的C#类。我遇到了重写以下析构函数的看似简单的问题。class Wrapper {
// P/Invoke ellided
private SomeType x;
public Wrapper() {
x = new SomeType();
Begin();
}
public ~Wrapper() {
End();
}
我现在拥有的简化F#代码如下:
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#析构函数的搜索未在我所拥有的书或网络上的书中找到任何结果。
谢谢。
解决方法
您是否尝试过寻找F#终结器?
override x.Finalize() = ...
,namespace FSharp.Library
type MyClass() as self =
let mutable disposed = false;
// TODO define your variables including disposable objects
do // TODO perform initialization code
()
// internal method to cleanup resources
let cleanup(disposing:bool) =
if not disposed then
disposed <- true
if disposing then
// TODO dispose of managed resources
()
// TODO cleanup unmanaged resources
()
// implementation of IDisposable
interface IDisposable with
member self.Dispose() =
cleanup(true)
GC.SuppressFinalize(self)
// override of finalizer
override self.Finalize() =
cleanup(false)
F#类库模板
http://blogs.msdn.com/b/mcsuksoldev/archive/2011/06/05/f-class-library-template.aspx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。