本文介绍了如何创建一个子对象的Expression.Property的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常我以这种方式创建一个表达式。 ParameterExpression pe = Expression.Parameter(typeof(object1),x );
string Name =property1;
MemberExpression left = Expression.Property(pe,(object1).GetProperty(Name));
它生成 left = x => x.property1
我需要知道如何生成
left = x => x.Object2.property1
如果Name =Object2.property1;对象2是对象1的小孩
提前感谢
解决方案
我不太明白你想要什么是属性链(比如:x.Prop1.Prop2)?
var pe = Expression.Parameter(typeof(object1));
var property1 = typeof(object1).GetProperty(Name1);
var property2 = property1.PropertyType.GetProperty(Name2);
var inner = Expression.Property(pe,property1);
var outer = Expression.Property(inner,property2);
normally I create an expresion in this way.
ParameterExpression pe = Expression.Parameter(typeof(object1), "x");
string Name = "property1";
MemberExpression left = Expression.Property(pe, (object1).GetProperty(Name));
it produces left = x => x.property1
I need to know how can I produce
left = x => x.Object2.property1
if Name = "Object2.property1"; and object2 is a child to object1
Thanks in advance
解决方案
I don't quite understand what you want. Is it a property chain (say: x.Prop1.Prop2)?
var pe = Expression.Parameter(typeof(object1));
var property1 = typeof(object1).GetProperty(Name1);
var property2 = property1.PropertyType.GetProperty(Name2);
var inner = Expression.Property(pe, property1);
var outer = Expression.Property(inner, property2);
这篇关于如何创建一个子对象的Expression.Property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!