Pages

Showing posts with label Extensibility. Show all posts
Showing posts with label Extensibility. Show all posts

Sunday, August 19, 2012

F# Extension Methods

The F# extension method is one of my favorite feature in F#.

F# method and property to be used inside F#

F# extension methods for F# is like 
[ < AutoOpen > ]
module ExtensionMethodsOnlyInFSharp =
    type System.String with
        member this.IsFSharp() = this = "F#"
        member this.``IsF#`` with get() = this = "F#"
And it can be invoked like:
    printfn "equal to F#? %A" ("F#".IsFSharp())
    printfn "equal to F#? %A" ("F#".``IsF#``)
The IsF# is a property. So F# can define extension property! 

F# method to be used in C#

The extension method needs for C# needs an attribute. [ < System.Runtime.CompilerServices.ExtensionAttribute > ]

The extension method defined in the F# can be used in C# is like:

[< System.Runtime.CompilerServices.ExtensionAttribute >]
[< AutoOpen >]
module ExtensionMethodForCSharp = 
    [< System.Runtime.CompilerServices.ExtensionAttribute >]
    let IsFSharp(str:string) = str = "F#"

    // not possible to access from C# IDE
    [< System.Runtime.CompilerServices.ExtensionAttribute >]
    let ``IsF#``(str:string) = IsFSharp str

F# extension methods on Array

F# can define the extension method on array generic (without specifying the concrete type for 'T). It is NOT possible to declare extension method for concrete array ,such as float [].

type 'T ``[]`` with
        member this.MyExtension with get() = "my extension"

I just added code snippet support for the extension method. You can find at the codeplex project.

Friday, March 23, 2012

Create Visual Studio 11 Template

Recently I have a task to write a template. The task seems easy but turns out very consuming. It involves little programming, but manual mouse click steps.

Because I cannot find VS SDK 2011, so I have to use VS 2011 SP1 SDK. Follow the following screenshoot, you can create a VSIX project:

This will give us the VSIX project. The content is actually from Visual Studio 11's export feature:

The export template provides a .zip file. Copy this .zip file to the VSIX project. The UI is pretty straightforward, the only problem is the VSIX is only generated for Visual Studio 2010. The way to make the VSIX work for 2010 is to make sure the Visual studio version set to 11.0 in the source.extension.vsixmanifest file. You might have to use notepad to edit the content of the file.

< SupportedProducts >
      < VisualStudio Version="11.0" >

If you ever decide to modify the zip file, please make sure you go to Visual Studio UI remove the content and re-add the content. I have been bitten this badly. :-(