简体   繁体   English

C#中的表达式树

[英]Expression Tree in C#

How can I convert this into expression tree or using linq so I can write function base on these expression hierarchy?.如何将其转换为表达式树或使用 linq 以便我可以根据这些表达式层次结构编写函数?。

I have seen this library but I am not sure I am on right path to https://csharpeval.codeplex.com/wikipage?title=Usage&referringTitle=Documentation我看过这个库,但我不确定我是否在正确的路径上https://csharpeval.codeplex.com/wikipage?title=Usage&referringTitle=Documentation

 max(avg(high1:3),avg(low1:3)) - min(avg(high1:3),avg(low1:3))

Compiling and executing codes at run time is always a bit challenging.在运行时编译和执行代码总是有点挑战性。 The library that you mentioned is just a way.你提到的图书馆只是一种方式。

You can use Roslyn which comes with C# 6.0 and Visual Studio 2015 by Microsoft and the C# team.您可以使用 Microsoft 和 C# 团队随 C# 6.0 和 Visual Studio 2015 一起提供的 Roslyn。 You can't imagine how powerful is it.你无法想象它有多强大。 Here are some samples and walkthroughs:以下是一些示例和演练:

https://github.com/dotnet/roslyn/wiki/Samples-and-Walkthroughs https://github.com/dotnet/roslyn/wiki/Samples-and-Walkthroughs

And some other introductions:以及其他一些介绍:

https://en.wikipedia.org/wiki/.NET_Compiler_Platform https://en.wikipedia.org/wiki/.NET_Compiler_Platform

And here is some sample to create a REPL (something like what you want):这是创建 REPL 的一些示例(类似于您想要的):

http://www.jayway.com/2015/05/09/using-roslyn-to-build-a-simple-c-interactive-script-engine/ http://www.jayway.com/2015/05/09/using-roslyn-to-build-a-simple-c-interactive-script-engine/

Using Roslyn can simple have something like this:使用 Roslyn 可以很简单,如下所示:

var csScript =
    string.Format(@"
        var x = Math.Max(Math.Avg({0},3),Math.Avg(low1:3));
        x;
    ", high1, low1);
    //And this from the REPL
    Console.WriteLine(CSharpScriptEngine.Execute(csScript));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM