F# – Make extension methods visible to C#

In F# you can write an extension method like this:

image

Whilst this will work perfectly fine in your F# code, the extension method will not be visible to any C# code using the FileInfo type because F# and C# compiles extension methods differently.

To make C#-compatible extension methods in F#, here’s what you need to do instead:

image

That’s it, just follow these 3 simple steps and you’re done:

  1. wrap the extension methods inside a class decorated with the [<Extension>] attribute
  2. write the extension methods as static members where the first argument is of the type which should be extended (like how you would write an extension method in C#)
  3. mark the extension methods with the [<Extension>] attribute


Yan Cui

I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

You can contact me via Email, Twitter and LinkedIn.

Hire me.