[英]How can i get property names from an Expression tree?
My method is 我的方法是
public Task<Product> GetProduct(int productId, params Expression<Func<Product, object>>[] properties)
{
var member = properties[0].Body as MemberExpression;
var v = member.Member.Name;
}
i can get a single property name by using the appropriate index 我可以通过使用适当的索引来获得单个属性名称
var member = properties[0].Body as MemberExpression;
var v = member.Member.Name;
But this is not what i want. 但这不是我想要的。 I would love to get all property names and string.join them with linq.
我很想获得所有属性名称和string.linq加入它们。
How can i do that? 我怎样才能做到这一点?
Use as
operator and then filter the ones which were not properties. as
运算符,然后过滤不是属性的那些。 If you use casting, it will throw exception but as
will just return null. 如果你使用的铸造,它会抛出异常,但
as
只会返回null。
var all =
string.Join(", ", properties
.Select(x =>
x.Body as MemberExpression))
.Where(x => x != null)
.Select(x =>
x.Member.Name));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.