My Technical Notes

Friday, 22 May 2015

F# COM Component code

This is a simple COM component written in F#.

The F# library project template does not come with an `AssemblyInfo.fs`, therefore you have to add one yourself. Stick it at the top of your solution.

namespace TestComComponentFSharp // **************** REPLACE THIS *******************

open System.Reflection
open System.Runtime.CompilerServices
open System.Runtime.InteropServices

[<assembly: AssemblyTitle("*** TestComComponentFSharp **** REPLACE THIS")>]
[<assembly: ComVisible(true)>]
[<assembly: Guid("0B684F15-DC37-40C0-A785-EDF1A63BBAF5")>] // ******* REPLACE THIS ************
[<assembly: AssemblyKeyFile("KeyFile.snk")>] // ******** INPUT CORRECT KEY FILE NAME

// Version information for an assembly consists of the following four values:
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the ‘*’ as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[<assembly: AssemblyVersion("")>]
[<assembly: AssemblyFileVersion("")>]


Google-ing for `AssemblyInfo.fs` gives many examples. There is even an extension, F# Assembly Info File, for it.

You will also need an assembly key file which can be generated using the below command, which should be created in the project root directory.

sn -k <key file name>.snk

Here is a simple example of a COM class, `Class1` with an interface `IClass1`, with a string property `XYZ`:

namespace TestComComponentFSharp

open System.Runtime.InteropServices

type IClass1 =
    abstract XYZ : string with get, set

type Class1() = 
    interface IClass1 with
        member val XYZ = "Tahir Hassan (XYZ)" with get, set

No comments: