如何创建扩展方法(F#)? [英] How do I create an extension method (F#)?

查看:79
本文介绍了如何创建扩展方法(F#)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在F#中创建扩展方法,例如,像这样的C#扩展:

How do I create an extension method in F#, for example, like this C# extension:

    public static string Right(this string host, int index)
    {
        return host.Substring(host.Length - index);
    }

推荐答案

对于可从F#调用的F#扩展名:

For an F# extension that can be called from F#:

type System.String with
  member x.Right(index) = x.Substring(x.Length - index)

请注意,从Beta 1开始,这不会产生与C#兼容的扩展方法.

Note that as of Beta 1, this doesn't result in a C#-compatible extension method.

要生成C#可见的扩展方法(但不能用作F#中的扩展方法),请参见Brian编辑中指向原始帖子的链接.

For generating extension methods visible from C# (but not usable as extension methods in F#), see the link in Brian's edit to the original post.

这篇关于如何创建扩展方法(F#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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