是否可以将 F# 代码转换为 C# 代码?

发布于 2024-10-14 18:05:29 字数 618 浏览 3 评论 0原文

我之所以问这个问题是因为我正在学习 F# 并且想参加 TopCoder 比赛。然而,F# 并不在支持的语言列表中。但 C# 榜上有名(说实话,几乎所有在线编程比赛都是如此,除了 Google Code Jam 和 Facebook Hacker cup)。

我目前能想到的可能的解决方法是 1)找到一个可以将F#源代码直接翻译成C#的翻译器 2) 首先将 F# 代码编译为 .net 可执行文件,然后将其反汇编回 C# 代码

最低要求是生成的 C# 必须能够编译为可运行的 .net 可执行文件,最好尽可能减少外部依赖。

第一种方法似乎不太可能,快速谷歌搜索结果没有任何相关内容。 方法二看起来更有前途,有.net反汇编程序存在。

我尝试了最受欢迎的一款——Red Gate 的 Reflector。虽然它可以完美地反汇编 C# 可执行文件,但它似乎对从 F# 编译的可执行文件存在问题:它很高兴反汇编,但生成的 C# 代码有一些特殊字符,例如在类名中添加前导 $ 符号和其他奇怪的东西,所以它无法编译。我使用的是 Visual Studio 2010 Professional,最新的 Reflector beta 版本(免费)。

我在这里错过了什么吗?是否可以?

更新: 看来这还是不可能的。现在,我将使用 C# 代替。

The reason why I am asking is that I'm learning F# and would like to attend TopCoder competitions. However, F# is not among the list of languages supported there. But C# is on the list (to be honest, this is the case for almost all online coding competitions, except Google Code Jam and Facebook Hacker cup).

The possible workarounds I can think of at this moment are
1) find a translator that can translate F# source code directly into C#
2) compile F# code into .net executable first, then disassemble it back to C# code

The minimum requirement is that the generated C# must be able to compile into a runnable .net executable, preferable as less external dependency as possible.

The first approach seems unlikely, a quick google search turns out nothing relevant.
Approach two looks more promising, there are .net disassemblers exist.

I tried the most popular one --- Reflector from Red Gate. While it can perfectly dissemble C# executables, it appears to have problems with executables compiled from F#: it happily disassembled, but the resulting C# code has some special characters such as adding a leading $ sign to a class name and other weird stuffs, so it cannot be compiled. I was using Visual Studio 2010 Professional, the latest Reflector beta version (which is free).

Am I missing anything here? Is it possible?

Update:
It looks like this is still impossible. For now, I'll use C# instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-10-21 18:05:29

正如其他人已经在评论中指出的那样 - 如果有某种方法可以做到这一点,那么将会有很多令人讨厌的情况,它可能无法正常工作并且会非常脆弱......

解决问题的一种方法(对你来说)就是用 F# 编写解决方案,然后将其重写为 C#。这听起来可能很愚蠢,但有一些优点:

  • 在 F# 中,您可以轻松地对解决方案进行原型设计,这样您就能够更快地找到正确的解决方案。
  • 将代码翻译为 C# 时,您可能会发现自己更频繁地使用 lambda 表达式等功能,因此它甚至可能会提高您的 C# 技能...
  • 如果您依赖 .NET 库,那么这部分代码将很容易翻译。

当然,最好的办法是说服组织者他们应该支持 F#(如果他们已经允许使用 C#,这可能不会太困难),但我知道这可能是一个挑战。

As others already pointed out in the comments - if there is some way to do that, there will be quite a few nasty cases where it probably won't quite work and it will be very fragile...

One way to deal with the problem (for you) is to just write the solution in F# and then rewrite it to C#. This may sound stupid, but there are some advantages:

  • In F#, you can easily prototype the solution, so you'll be able to find the right solution faster.
  • When translating code to C#, you'll probably find yourself using features like lambda expressions more often, so it may even improve your C# skills...
  • If you rely on .NET libraries, then this part of code will be easy to translate.

Of course, the best thing would be to convince the organizers that they should support F# (which probably wouldn't be too difficult if they allow C# already), but I understand that this may be a challange.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文