c# Expression类的简介

// Add the following directive to your file:
// using System.Linq.Expressions;  

// The block expression allows for executing several expressions sequentually.
// When the block expression is executed,
// it returns the value of the last expression in the sequence.
BlockExpression blockExpr = Expression.Block(
    Expression.Call(
        null,
        typeof(Console).GetMethod("Write", new Type[] { typeof(String) }),
        Expression.Constant("Hello ")
       ),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("World!")
        ),
    Expression.Constant(42)
);

Console.WriteLine("The result of executing the expression tree:");
// The following statement first creates an expression tree,
// then compiles it, and then executes it.           
var result = Expression.Lambda<Func<int>>(blockExpr).Compile()();

// Print out the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:");
foreach (var expr in blockExpr.Expressions)
    Console.WriteLine(expr.ToString());

// Print out the result of the tree execution.
Console.WriteLine("The return value of the block expression:");
Console.WriteLine(result);

// This code example produces the following output:
//
// The result of executing the expression tree:
// Hello World!

// The expressions from the block expression:
// Write("Hello ")
// WriteLine("World!")
// 42

// The return value of the block expression:
// 42


取得属性名:

/// <summary>
/// /Get property name
/// </summary>
/// <typeparam name="T">Type T</typeparam>
/// <param name="express">Expression of property</param>
/// <returns>property name</returns>
private static string GetPropertyName<T>(Expression<Func<T>> express)
{
     var memberExpress = express.Body as MemberExpression;
     if (memberExpress != null)
     {
         return memberExpress.Member.Name;
     }
     else
     {
         return string.Empty;
     }
}

 

转自

https://msdn.microsoft.com/zh-cn/library/system.linq.expressions.expression(v=vs.110).aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# Expression是一个用于表示代码中的表达式的。它可以用于构建和操作表达式树,这些表达式树可以在运行时进行解析和执行。通过使用Expression,可以将代码中的表达式表示为对象,并对其进行操作,例如将表达式转换为字符串、编译为委托等。\[1\] 在C#中,可以使用Expression来创建各种型的表达式,例如Lambda表达式、常量表达式、二元运算表达式等。可以通过将这些表达式组合在一起来构建复杂的表达式树。\[1\]\[3\] 一旦创建了表达式树,可以使用Compile方法将其编译为委托,从而可以在运行时执行该表达式。编译后的委托可以像普通的方法一样调用,并返回表达式的结果。\[2\] 通过Expression,还可以获取表达式的信息,例如表达式的字符串表示、返回值型、参数列表等。可以使用ExpressionType属性来判断表达式的型,例如Lambda表达式、常量表达式等。\[1\] 总之,C# Expression提供了一种在代码中表示和操作表达式的方式,可以用于构建和执行动态生成的表达式树。它在编写一些需要在运行时生成和执行代码的场景中非常有用。 #### 引用[.reference_title] - *1* *2* *3* [C# 表达式树Expression](https://blog.csdn.net/sinat_23050697/article/details/123520591)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值