开发者

Implicit expression for property

开发者 https://www.devze.com 2023-03-21 13:23 出处:网络
I know I can write the following to generate lambda expression: Expression<Func<st开发者_开发百科ring, bool>> lambda = s => s.Length == 5;

I know I can write the following to generate lambda expression:

Expression<Func<st开发者_开发百科ring, bool>> lambda = s => s.Length == 5;

But is there any way to automatically generate expression for property? In other words is there strongly-typed analogue of this:

var property = Expression.Property("Name") 


This will give you a lambda which returns the Length property:

Expression<Func<string, int>> lambda = s => s.Length;

If you don't want the full lambda, but only the MemberExpression which accesses the property, you can do that:

var propertyExpression = (MemberExpression)lambda.Body;


Expressions<Func<ClassWithProperty, PropertyReturnType>> lambda = C => C.Name;
0

精彩评论

暂无评论...
验证码 换一张
取 消