尝试欺骗用于使用反射构建选择表达式
我正在尝试从字符串反射中构建一个选择的表达式。我的开始是好的,我的选择表达式是从此来源正确构建的:
Type entityType = Type.GetType("NameSpacePath.MyEntity");
ParameterExpression xParameter = Expression.Parameter(entityType, "x");
string propertyPath = "ReferenceProperty.SubReference.Property";
Expression body = propertyPath.Split('.').Aggregate((Expression)xParameter, Expression.PropertyOrField);
这项工作如预期:
身体输出:{X.ReferenceProperty.subreference.property}
SelectExpression最终输出:X => new {property = x.ReferenceProperty.subreference.property},
所以现在我以一种简单的方式表达了我的身体表达,但是问题是当我尝试获取列表时。 我希望能够写下这样的书:
string propertyPath = "ReferenceProperty.SubReference.PropertyList.Select(y => y.Id)";
或
string propertyPath = "ReferenceProperty.SubReference.PropertyList.Select(y => new { Id = y.Id })";
这不定如预期:
例外:select(y => y.id)不是Myentity的成员
预期的身体输出:{X.ReferenceProperty.SubReference.Propertylist.Select(....)}
在.Aggregate Tricks中,'select'不是我类型的成员...
有人有聪明的方式来帮助我在逻辑后执行此任务,还是应该修改自己的操作方式来处理列表中的选择的情况?
谢谢你的宝贵时间。
I'm trying to build a select expression in reflection from string. My beginning is good and my select expression is built correctly from this source:
Type entityType = Type.GetType("NameSpacePath.MyEntity");
ParameterExpression xParameter = Expression.Parameter(entityType, "x");
string propertyPath = "ReferenceProperty.SubReference.Property";
Expression body = propertyPath.Split('.').Aggregate((Expression)xParameter, Expression.PropertyOrField);
This work as expected :
Body output : { x.ReferenceProperty.SubReference.Property }
SelectExpression Final output : x => new { Property = x.ReferenceProperty.SubReference.Property }
So now I have my body expression in a simple way but the probleme is when i try to get a List.
I would like to be able to write somethings like:
string propertyPath = "ReferenceProperty.SubReference.PropertyList.Select(y => y.Id)";
or
string propertyPath = "ReferenceProperty.SubReference.PropertyList.Select(y => new { Id = y.Id })";
This doesn't work as expected :
Exception : Select(y => y.Id) is not a member of MyEntity
Body output expected : {x.ReferenceProperty.SubReference.PropertyList.Select(....)}
In the .Aggregate tricks, 'Select' is not a member of my type ...
Does anyone have a clever way to help me to perform this task following my logic or should I revise the way I do things to handle the case of a select on a list ?
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需使用
install-package system.linq.dynamic.core
You can simply use
Install-Package System.Linq.Dynamic.Core
感谢Aron的回答,这帮助我意识到,包装比“经典” /“自制”代码更为足够,可以从字符串或propertyinfo []中创建表达式。
在比较了Roslyn之后, csharpscript.evaluateasync 和 dynamicexpressionparser.parselambda ,我认为 estuneAsync 在我的情况下是最方便的。
管理不如 parselambda ,它需要在字符串上进行大量工作才能获得正确的结果。
两者之间的表现都是相似的,这里没有赢家。
如果可以帮助某人,我在这里公开我的解决方案:
用反射调用:
edit:
经过进一步的性能测试(连续100个呼叫不同的功能), DynamiceXpressionParser.parselambda 似乎比 csharpscript.evaluateasync 构建以构建相同的表达。
Thanks for Aron's answer which helped me to realize that packages are more adequate than "classical" / "home-made" code to create an Expression from a string or a PropertyInfo[].
After comparing Roslyn, CSharpScript.EvaluateAsync and DynamicExpressionParser.ParseLambda, I think EvaluateAsync is the most convenient in my case.
It is less complicated to manage than ParseLambda which requires a lot of work on the string in order to get a correct result.
The performances between both are similar, no winner here.
I expose my solution here if it can help someone :
Called with reflection :
Edit :
After further performance tests (100 consecutive calls to different functions), DynamicExpressionParser.ParseLambda seems to be about 2 times faster than CSharpScript.EvaluateAsync to build the same expression.