调用静态方法时可以省略类名吗? [英] Can I omit the class name when calling a static method?

查看:446
本文介绍了调用静态方法时可以省略类名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在F#中,调用静态方法时可以省略类名吗?

In F#, can I omit the class name when calling a static method?

示例:

在C#中,我可以做类似的事情:

In C#, I can do something like:

using static Bizmonger.Patterns.MessageBus;
...
Publish("SOME_MESSAGE");

代替:

MessageBus.Publish("SOME_MESSAGE");

我可以在F#中做类似的事情吗?

Can I do something like this in F#?

推荐答案

在F#中,可以在名称空间(就像C#中的using)或模块(在调用API时很有用)上使用open是用F#编写的),而不是静态类(调用C#库时需要用到的).

In F#, you can use open on namespaces (just like using in C#) or on modules (which is useful when the API you are calling has been written in F#), but not on static classes (which is what you'd need when calling C# libraries).

尽管可以使代码短一些,但是您可以做的一件事就是定义类型别名:

One thing that you can do though to make the code a bit shorter is to define a type alias:

type M = Bizmonger.Patterns.MessageBus;

// Now you can write just
M.Publish("SOME_MESSAGE")

// Rather than writing the full
MessageBus.Publish("SOME_MESSAGE");

有一个功能请求在F#UserVoice 上允许在静态类上使用open(就像在C#中一样),因此,如果您希望这种情况发生,请在此处进行投票和评论.

There is a feature request on the F# UserVoice to allow using open on static classes (just like in C#) and so if you'd like this to happen, please upvote and comment there.

这篇关于调用静态方法时可以省略类名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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