私はエクスプレッションツリーを理解しようとしています。、を作成し、追加して、文字列を出力する単純なhelloWorld関数を作成することから始めようと思いました。これは私がこれまでに持っているものです:StringBuilder"Helloworld"
var stringBuilderParam = Expression.Variable
    typeof(StringBuilder), "sb");
var helloWorldBlock =
    Expression.Block( new Expression[]
        {
            Expression.Assign(
                stringBuilderParam, 
                Expression.New(typeof(StringBuilder))),
            Expression.Call(
                stringBuilderParam,
                typeof(StringBuilder).GetMethod(
                    "Append",
                    new[] { typeof(string) }),
                new Expression[]
                    {
                        Expression.Constant(
                            "Helloworld", typeof(string))
                    }),
            Expression.Call(
                stringBuilderParam,
                "ToString",
                new Type[0],
                new Expression[0])
        });
var helloWorld = Expression.Lamda<Func<string>>(helloWorldBlock).Compile();
Console.WriteLine(helloWorld);
Console.WriteLine(helloWorld());
Console.ReadKey();
Compile()スロー_InvalidOperationException
スコープ''から参照されるタイプ'System.Text.StringBuilder'の変数'sb'ですが、定義されていません
明らかに、私はこれについて正しい方法で行っていません。誰かが私を正しい方向に向けることができますか?
明らかに、私はそれを行うのConsole.WriteLine("HelloWorld");がいくらか簡単になることを理解しています。