簡體   English   中英

C#中的表達式樹

[英]Expression Tree in C#

如何將其轉換為表達式樹或使用 linq 以便我可以根據這些表達式層次結構編寫函數?。

我看過這個庫,但我不確定我是否在正確的路徑上https://csharpeval.codeplex.com/wikipage?title=Usage&referringTitle=Documentation

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

在運行時編譯和執行代碼總是有點挑戰性。 你提到的圖書館只是一種方式。

您可以使用 Microsoft 和 C# 團隊隨 C# 6.0 和 Visual Studio 2015 一起提供的 Roslyn。 你無法想象它有多強大。 以下是一些示例和演練:

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

以及其他一些介紹:

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

這是創建 REPL 的一些示例(類似於您想要的):

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

使用 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