Quantcast
Channel: Extension Methods – theburningmonk.com
Viewing all articles
Browse latest Browse all 10

F# – Make extension methods visible to C#

$
0
0

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

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.

Hi, my name is Yan Cui. I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

You can contact me via Email, Twitter and LinkedIn.

Hire me.

The post F# – Make extension methods visible to C# appeared first on theburningmonk.com.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images