Exposing extension methods in F#

April 13th, 2011

Thanks for Quora + Reflector + MSDN, I’ve figured out how to expose extension methods from F#

  1. Create a public module within a namespace
  2. Add the extension method as a function
  3. Decorate both the module and the function(s) with the ExtensionMethodAttribute

 C# |  copy code |? 
01
02
namespace Castle.MonoRail
03
 
04
[<System.Runtime.CompilerServices.ExtensionAttribute>]
05
module public ExtensionMethods = 
06
 
07
    open Castle.MonoRail.Routing
08
    open Castle.MonoRail.Hosting.Mvc
09
 
10
    [<System.Runtime.CompilerServices.ExtensionAttribute>]
11
    let Match(router:Router, path:string) = 
12
        router.Match(path, MonoRailHandlerMediator())
13

Categories: .NET, F# | Top Of Page | 2 Comments » |

2 Responses to “Exposing extension methods in F#”

Mauricio Scheffer Says:

Excellent, I just had to write some extension methods and used this as reference. The only downside to this is that it doesn’t support overloads. But if you use a type instead of a module and static members instead of let bindings, you can have overloaded extensions.

hammett Says:

I’ve used [CompiledName] to support overloads..

Leave a Reply