F# Class Library Template
So, in my last post I said that the template would be the last for a while, but here is one more.
This template creates a Windows Library with a simple class definition. The template can be found on the Visual Studio Gallery:
https://visualstudiogallery.msdn.microsoft.com/a3f39bd9-9fd2-4465-a931-2163758a1447
When the template is installed you get the following template added to your F# folder when creating a new F# project:
This template also defines several Item Templates for creating the following project items:
- F# Class – The definition of a simple F# class
- F# Interface – The definition of a simple F# interface
- F# Disposable Class – The definition of a F# class that implements IDisposable
The simple F# class generated code is as follows:
namespace FSharp.Library
open System;
open System.Xml;
type MyClass() as self =
// https://msdn.microsoft.com/en-us/library/dd233205.aspx
// TODO define your variables
// TODO perform initialization code
do
()
The F# interface definition is as follows:
namespace FSharp.Library
open System;
type MyInterface =
// https://msdn.microsoft.com/en-us/library/dd233207.aspx
// TODO define your abstract members
abstract member MyMember: unit -> unit
The more interesting Item Template is the F# Class that implements the IDisposable Interface. This template provides the core code for dealing with both managed and unmanaged resources:
namespace FSharp.Library
open System;
open System.Xml;
type MyClass() as self =
let mutable disposed = false;
// https://msdn.microsoft.com/en-us/library/dd233205.aspx
// TODO define your variables including disposable objects
// TODO perform initialization code
do
()
// 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)
Using a class defined in this manner allows one to clean-up the class resources with a use binding.
Once again, hopefully you will find this template useful.
Written by Carl Nolan