#等同于评估和演示˚F [英] F# equivalent to Eval

查看:137
本文介绍了#等同于评估和演示˚F的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个F#相当于EVAL?我的目的是让我的应用程序从一个文件中加载一个小code样品和基本

Is there an F# equivalent to eval? My intent is to have my app load a small code sample from a file and essentially

let file = "c:\mysample"
let sample = loadFromFile file
let results = eval(sample)

我是新来的F#,并试图找出一些限制之前,我申请到一个项目中。

I am new to F# and trying to figure out some of the limitations before I apply it to a project.

感谢您

推荐答案

没有的功能,让您直接做到这一点。但是,当你想通过程序编译F#源文件,你可以调用F#编译器应用程序。要做到这一点,最简单的方法是使用F#codeDOM提供商,它可作为F#PowerPack中的一部分(在 FSharp.Compiler。codeDom.dll 组装)。但是,请注意,你需要有F#编译器安装在用户的计算机上。

There is no function that would allow you to do this directly. However, when you want to compile an F# source file programatically, you can invoke the F# compiler from your application. The easiest way to do this is to use F# CodeDOM provider, which is available as part of the F# PowerPack (in the FSharp.Compiler.CodeDom.dll assembly). However, note that you need to have the F# compiler installed on the user's machine.

在code编译运行看起来大致是这样的:

The code to run the compiler would look roughly like this:

open Microsoft.FSharp.Compiler.CodeDom

let provider = new FSharpCodeProvider()
let compiler = provider.CreateCompiler()

let parameters = new CompilerParameters
  (GenerateExecutable = true, OutputAssembly = "file.exe")
let results = icc.CompileAssemblyFromFile(parameters, "filename.fs")

在编译源文件为一个程序集,你需要使用.NET反射来加载程序集,并从组件运行一些特定的功能或类(例如,你可以找一个功能与一些特殊的名字或标记一些属性)。

Once you compile the source file into an assembly, you'll need to load the assembly using .NET Reflection and run some specific function or class from the assembly (you can for example look for a function with some special name or marked with some attribute).

(作为一个侧面说明,通过报价或编写自己的跨preTER中提到的<一个href="http://stackoverflow.com/questions/372043/how-can-evaluate-an-ex$p$pssion-stored-as-a-string-in-f">related SO质疑可能是一个选项,如果你的源$ C ​​$ c是比较简单的,但如果你想支持F#的全功率)可能不是

(As a side-note, using quotations or writing your own interpreter as mentioned in the related SO question may be an option if your source code is relatively simple, but probably not if you want to support the full power of F#)

这篇关于#等同于评估和演示˚F的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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