开发者

Implicit expression for property

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜