Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save liulixiang1988/526440878340a72ad5d969aaa4a316fa to your computer and use it in GitHub Desktop.
Save liulixiang1988/526440878340a72ad5d969aaa4a316fa to your computer and use it in GitHub Desktop.
C#'s lambda expression tree.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace LambdaExpressionTree
{
class Program
{
static void Main(string[] args)
{
// これはシンプルな式じゃない( {} を含む複数ステートメント構成)ので、Expression<Func<...>> にできない。コンパイラに怒られる。
// Expression<Func<int, int>> makeDouble = (x) => { return x * 2; };
Expression<Func<int, int>> makeDouble = (x) => x * 2;
//function makeDouble: x => (x * 2)
// .Body: (x * 2)
// .TailCall: False
// .ReturnType: System.Int32
// .Type: System.Func`2[System.Int32,System.Int32]
// .CanReduce: False
// .NodeType: Lambda
// .Parameters: System.Runtime.CompilerServices.TrueReadOnlyCollection`1[System.Linq.Expressions.ParameterExpression]
// .Parameters[0]: x
// .Parameters[0].Name: x
// .Parameters[0].IsByRef: False
// .Parameters[0].NodeType: Parameter
// .Parameters[0].Type: System.Int32
DisplayLambdaExpression("function makeDouble", makeDouble);
//function returnNull: () => null
// .Body: null
// .TailCall: False
// .ReturnType: System.Object
// .Type: System.Func`1[System.Object]
// .CanReduce: False
// .NodeType: Lambda
// .Parameters: System.Runtime.CompilerServices.TrueReadOnlyCollection`1[System.Linq.Expressions.ParameterExpression]
DisplayLambdaExpression("function returnNull", (Expression<Func<object>>)(() => null));
//function manyParams: (a, b, c, d) => null
// .Body: null
// .TailCall: False
// .ReturnType: System.Object
// .Type: System.Func`5[System.Int32,System.Single,System.Char,System.String,System.Object]
// .CanReduce: False
// .NodeType: Lambda
// .Parameters: System.Runtime.CompilerServices.TrueReadOnlyCollection`1[System.Linq.Expressions.ParameterExpression]
// .Parameters[0]: a
// .Parameters[0].Name: a
// .Parameters[0].IsByRef: False
// .Parameters[0].NodeType: Parameter
// .Parameters[0].Type: System.Int32
// .Parameters[1]: b
// .Parameters[1].Name: b
// .Parameters[1].IsByRef: False
// .Parameters[1].NodeType: Parameter
// .Parameters[1].Type: System.Single
// .Parameters[2]: c
// .Parameters[2].Name: c
// .Parameters[2].IsByRef: False
// .Parameters[2].NodeType: Parameter
// .Parameters[2].Type: System.Char
// .Parameters[3]: d
// .Parameters[3].Name: d
// .Parameters[3].IsByRef: False
// .Parameters[3].NodeType: Parameter
// .Parameters[3].Type: System.String
DisplayLambdaExpression("function manyParams", (Expression<Func<int, float, char, string, object>>)((a, b, c, d) => null));
//action givenIntType: x => x.GetType()
// .Body: x.GetType()
// .TailCall: False
// .ReturnType: System.Void
// .Type: System.Action`1[System.Int32]
// .CanReduce: False
// .NodeType: Lambda
// .Parameters: System.Runtime.CompilerServices.TrueReadOnlyCollection`1[System.Linq.Expressions.ParameterExpression]
// .Parameters[0]: x
// .Parameters[0].Name: x
// .Parameters[0].IsByRef: False
// .Parameters[0].NodeType: Parameter
// .Parameters[0].Type: System.Int32
DisplayLambdaExpression("action givenIntType", (Expression<Action<int>>)((x) => x.GetType()));
//action stringType: () => "".GetType()
// .Body: "".GetType()
// .TailCall: False
// .ReturnType: System.Void
// .Type: System.Action
// .CanReduce: False
// .NodeType: Lambda
// .Parameters: System.Runtime.CompilerServices.TrueReadOnlyCollection`1[System.Linq.Expressions.ParameterExpression]
DisplayLambdaExpression("action stringType", (Expression<Action>)(() => "".GetType()));
}
private static void DisplayLambdaExpression<T>(string title, Expression<T> makeDouble)
{
Debug.WriteLine("{0}: {1}", title, makeDouble);
Debug.WriteLine(" .Body: {0}", makeDouble.Body);
Debug.WriteLine(" .TailCall: {0}", makeDouble.TailCall);
Debug.WriteLine(" .ReturnType: {0}", makeDouble.ReturnType);
Debug.WriteLine(" .Type: {0}", makeDouble.Type);
Debug.WriteLine(" .CanReduce: {0}", makeDouble.CanReduce);
Debug.WriteLine(" .NodeType: {0}", makeDouble.NodeType);
Debug.WriteLine(" .Parameters: {0}", makeDouble.Parameters);
var pIdx = 0;
foreach (var p in makeDouble.Parameters)
{
Debug.WriteLine(" .Parameters[{0}]: {1}", pIdx,p);
Debug.WriteLine(" .Parameters[{0}].Name: {1}", pIdx, p.Name);
Debug.WriteLine(" .Parameters[{0}].IsByRef: {1}", pIdx,p.IsByRef);
Debug.WriteLine(" .Parameters[{0}].NodeType: {1}", pIdx,p.NodeType);
Debug.WriteLine(" .Parameters[{0}].Type: {1}", pIdx,p.Type);
pIdx++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment