Looking for eval function in any .NET language

前提是你 提交于 2019-12-10 16:02:14

问题


Do any .NET/CLR languages support an eval function and also allow calling into standard .NET code (e.g. calling into C# code)? I know that neither F# nor C# support eval. Are there any other options?

I am ideally looking for a solution compatible with .NET 3.5 (so I think this rules out Clojure-CLR), but I'm interested in any/all options.


回答1:


JScript.NET has an eval function, see this answer




回答2:


An Eval Function for C# using JScript.NET (JavaScript)




回答3:


Not sure if this helps, but as you said you're looking for any solution. What about IronRuby?

public static class RubyEngineCreator
{
    private static ScriptEngine ironRubyEngine = null;
    private static ScriptEngine CreateEngine()
    {
        if (ironRubyEngine == null)
            ironRubyEngine = Ruby.CreateEngine();

        return ironRubyEngine;
    }

    public static dynamic GetRubyObject(string script)
    {
        return CreateEngine().CreateScriptSourceFromString(script).Execute();
    }
}

[TestClass]
public class UnitTest
{
    private T Eval<T>(string s)
    {
        return (T)RubyEngineCreator.GetRubyObject(s);
    }

    [TestMethod]
    public void Should_Return4_4()
    {
        var result = Eval<int>("2 + 2");
        Assert.AreEqual(4, result);
    }
}

Example taken from http://viniciusquaiato.com/blog/eval-em-c-com-ironruby/ (pt-BR)




回答4:


My eventual solution was to adopt IronPython. JScript.NET technically does work, but it's clumbsy to use with Visual Studio 2008 and JScript.NET seems to be getting deprecated by Microsoft. IronPython on the other hand has a fairly active community.



来源:https://stackoverflow.com/questions/5015565/looking-for-eval-function-in-any-net-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!