F# Equivalent of Destructor

前端 未结 2 1361
清酒与你
清酒与你 2021-02-19 06:07

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 W         


        
2条回答
  •  庸人自扰
    2021-02-19 07:04

    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# Class Library Template

    http://blogs.msdn.com/b/mcsuksoldev/archive/2011/06/05/f-class-library-template.aspx

提交回复
热议问题