Using extension methods defined in C# from F# code

前端 未结 2 863
闹比i
闹比i 2021-01-11 14:24

I have a series of extension methods defined for various classes in a C# library. I\'m currently writing some F# code and instead of rewriting that code I would simply like

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 14:46

    Update:

    In the current version of F#, you can simply consume extension methods by adding

    open TheNamespaceOfExtensionMethod
    

    to your source file (just like you do in C# with a using directive) and simply call the extension method as if it was a normal instance method.

    By the way, you can always call them directly just like a normal static method and pass the object reference as the first parameter without any directives if you want. An extension method is simply a static method decorated with ExtensionAttribute under the hood.

    Original answer (before F# 2010 beta; not true anymore, as Dykam points out):

    I don't think extension methods are supported by F#. You can always call them directly just like a normal static method and pass the object reference as the first parameter.

提交回复
热议问题