在 C# 中运行 F# 代码 -(就像 C# 中的 C# 使用 Roslyn) [英] Run F# code in C# - (Like C# in C# using Roslyn)

查看:21
本文介绍了在 C# 中运行 F# 代码 -(就像 C# 中的 C# 使用 Roslyn)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 C# 中运行 F# 代码?我用 C# 编写了一个应用程序 - 我想为它提供执行 F#、读取 Record 对象、从 F# 枚举列表的能力.目前的解决方案是什么?我知道将来可能会通过更新 Roslyn 来实现这一点.另外,好奇目前如何在 F# 中运行 F# 代码.有没有办法轻松做到这一点?

Is there a way to run F# code in C#? I have written an app in C# - I'd like to provide it the ability to execute F#, read Record objects, enumerate lists from F#. What is the current solution for this? I know in the future there will be probably be a way to do this via an update to Roslyn. Also, curious how to run F# code in F#, currently. Is there a way to do that easily?

推荐答案

目前,您必须将 F# 变成一个库,然后从 C# 中调用它.由于 F# 只是 .NET,因此使用 F# 库(大部分)就像使用任何其他 C# 库一样.但是,您可能需要查看 FSharpX 以了解这一部分,因为它提供了一些实用程序来简化从 C# 调用 F#,反之亦然.

Currently, you have to make the F# a library, and then call it from C#. Since F# is just .NET, using the F# library is (mostly) just like using any other C# library. You might want to look into FSharpX for this portion, however, as it provides some utilities to ease calling F# from C# and vice versa.

如果您想从 C# 动态编译 F#,您需要使用 F# Power Pack 中的 F# CodeDom 实现.这将让您使用 CodeDom 即时编译 F#,然后从 C# 执行它.

If you want to compile the F# dynamically from C#, you'll need to use the F# CodeDom implementation from the F# Power Pack. This will let you use CodeDom to compile F# on the fly, and then execute it from C#.

请注意,即使 Roslyn 已发布,它也无济于事,因为它不支持 F# 作为代码模型,仅支持 C# 和 VB.Net.有可能使用 Roslyn 在 F# 中操作和编译 C#,但反之则不行.

Note that Roslyn will not help here, even when it's released, as it won't support F# as a code model, only C# and VB.Net. It would potentially be possible to use Roslyn to manipulate and compile C# from within F#, but not the other way around.

这篇关于在 C# 中运行 F# 代码 -(就像 C# 中的 C# 使用 Roslyn)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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