.net4.0
我的测试.py
def Add (a, b):
return a+b
我可以在 C# 4 中使用它,如下所示:
ScriptRuntime runtime = Python.CreateRuntime();
dynamic script = runtime.UseFile("mytest.py");
Console.WriteLine(script.Add(1, 3));
但是,我如何在 F# 中使用动态?
open System
open Microsoft.Scripting.Hosting
open IronPython.Hosting
let call a b=
let runtime = Python.CreateRuntime()
let script = runtime.UseFile("mytest.py")
script.Add(a,b)
最佳答案
有一个模块FSharp.Interop.Dynamic ,在使用 dlr 实现动态运算符的 nuget 上。以便:
open System
open Microsoft.Scripting.Hosting
open IronPython.Hosting
open EkonBenefits.FSharp.Dynamic
let call a b=
let runtime = Python.CreateRuntime()
let script = runtime.UseFile("mytest.py")
script?Add(a,b)
与那里的许多片段相比,它有几个优点。
它是开源的,Apache许可证,你可以看看implementation它包括单元测试example cases .
关于dynamic - 我可以在 F# 中使用动态类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3521489/