大厦表达式树 [英] Building Expression Trees
本文介绍了大厦表达式树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我和如何建立更多的lambda表达式树的想法挣扎,如一个下面,更不用说一些可能有多个语句。例如:
I'm struggling with the idea of how to build an expression tree for more lambdas such as the one below, let alone something that might have multiple statements. For example:
Func<double?, byte[]> GetBytes
= x => x.HasValue ? BitConverter.GetBytes(x.Value) : new byte[1] { 0xFF };
我将不胜感激的任何想法。
I would appreciate any thoughts.
推荐答案
我建议通过方法阅读列表在表达类,所有的选项都列出,并 。表达式树编程指南
I would suggest reading through the list of methods on the Expression class, all of your options are listed there, and the Expression Trees Programming Guide.
对于此特定实例:
/* build our parameters */
var pX = Expression.Parameter(typeof(double?));
/* build the body */
var body = Expression.Condition(
/* condition */
Expression.Property(pX, "HasValue"),
/* if-true */
Expression.Call(typeof(BitConverter),
"GetBytes",
null, /* no generic type arguments */
Expression.Member(pX, "Value")),
/* if-false */
Expression.Constant(new byte[] { 0xFF })
);
/* build the method */
var lambda = Expression.Lambda<Func<double?,byte[]>>(body, pX);
Func<double?,byte[]> compiled = lambda.Compile();
这篇关于大厦表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文