检测 MemberExpression 是否有值
如何检测 MemberExpression 是否具有需要编译/计算的值?
我有两个单独的成员表达式输出,第一个有值,第二个没有。区分两者的最佳方法是什么?
exp
**{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6).handle}**
[System.Linq.Expressions.MemberExpression]: **{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6).handle}**
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
与
exp
{x.CreatedBy}
[System.Linq.Expressions.MemberExpression]: {x.CreatedBy}
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
How do I detect if a MemberExpression has a value that needs to be compiled/evaluated?
I have two separate member expression outputs, the first which has a value, and the second which doesn't. What is the best way to differentiate between the two?
exp
**{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6).handle}**
[System.Linq.Expressions.MemberExpression]: **{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6).handle}**
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
vs
exp
{x.CreatedBy}
[System.Linq.Expressions.MemberExpression]: {x.CreatedBy}
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此有两个不同的答案。
您是否尝试使用表达式创建条件,然后对结果执行某些操作?如果是这种情况,您将创建一个 Equals 类型的 BinaryExpression(调用 Expression 类上的静态 Equals 方法),传入左侧的 MemberExpression,然后传递另一个表示非值的表达式(如果是引用类型,则为 null,或者如果是值类型,则为结构的新实例)。
如果您实际上正在尝试计算此时的表达式,那么我将创建一个实际返回该表达式的 lambda,对其进行编译,然后检查代码中的非值。
I see two different answers to this.
Are you trying to create a conditional with expressions and then do something with the result? If that is the case, you will create a BinaryExpression of type Equals (call the static Equals method on the Expression class) passing in the MemberExpression as left, and then passing another expression representing a non-value (null if a reference type, or a new instance of the struct if a value type).
If you are actually trying to evaluate the expression at that point, then I would create a lambda which actually returns the expression, compile it, and then check against a non-value in your code.