从使用F#代码在C#中定义的扩展方法 [英] Using extension methods defined in C# from F# code

查看:76
本文介绍了从使用F#代码在C#中定义的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的在C#库各阶层
定义的扩展方法。我目前正在写一些F#代码,而不是改写
的代码,我只是想用我现有的扩展方法在我的F#代码。

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 to use my existing extension methods in my F# code.

我有添加到库中的引用,并使用了
打开语句导入命名空间,
,但扩展方法不会出现在F#

I have added a reference to the library and used the open statement to import the namespace, but the extension methods to not appear in F#

推荐答案

在F#的当前版本,可以通过添加

Update:

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

open TheNamespaceOfExtensionMethod

到您的源文件(就像你在C#中做一个使用指令),并简单地调用扩展方法,就好像它是一个正常的实例方法。

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.

顺便说一句,你总是可以直接调用它们就像一个正常的静态方法和传递对象引用作为第一个参数没有任何指令,如果你想。扩展方法只是一个静态方法引擎盖下用 ExtensionAttribute 装饰。

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.

我不认为扩展方法是由F#的支持。你总是可以直接调用它们就像一个正常的静态方法和传递对象引用作为第一个参数。

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.

这篇关于从使用F#代码在C#中定义的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆