是否可以将 F# 代码转换为 C# 代码?
我之所以问这个问题是因为我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他人已经在评论中指出的那样 - 如果有某种方法可以做到这一点,那么将会有很多令人讨厌的情况,它可能无法正常工作并且会非常脆弱......
解决问题的一种方法(对你来说)就是用 F# 编写解决方案,然后将其重写为 C#。这听起来可能很愚蠢,但有一些优点:
当然,最好的办法是说服组织者他们应该支持 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:
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.