从属性名称获取属性 lambda(其中属性类型可以为空)
您好,我基本上需要一个具有以下签名的函数,
Expression<Func<T, object>> GetPropertyLambda(string propertyName)
我已经做了一些尝试,但是当属性可为空时就会出现问题
它是这样的
ParameterExpression param = Expression.Parameter(typeof(T), "arg");
Expression member = Expression.Property(param, propertyName);
//this next section does conver if the type is wrong however
// when we get to Expression.Lambda it throws
Type typeIfNullable = Nullable.GetUnderlyingType(member.Type);
if (typeIfNullable != null)
{
member = Expression.Convert(member, typeIfNullable);
}
return Expression.Lambda<Func<T, object>>(member, param);
例外是
类型的表达 '系统.十进制' 不能用于返回类型 '系统.对象'
我真的很欣赏一些想法以及为什么它不能按预期工作
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上我不认为这个问题与 Nullable 类型有任何关系,而是与值类型有关。尝试使用
decimal
类型的属性(不是Nullable
)的方法:它也会以同样的方式失败。看一下如何为值和引用类型生成表达式树(例如使用 LinqPad)
Expression>> λ = x => x.AString;
(引用类型)=>主体是一个
MemberExpression
Expression> 。 λ = x => x.ADecimal;
(值类型)=>主体是一个带有
NodeType = Convert
和Type = typeof(object)
的UnaryExpression
,其Operand
是MemberExpression
我稍微修改了你的方法以考虑到这一点,它似乎工作正常:
Actually I don't think the problem has anything to do with Nullable types, but rather with value types. Try your method with a property of type
decimal
(notNullable<decimal>
) : it will fail the same way.Have a look at how expression trees are generated for value and reference types (using LinqPad for instance)
Expression<Func<T, object>> lambda = x => x.AString;
(reference type)=> The body is a
MemberExpression
Expression<Func<T, object>> lambda = x => x.ADecimal;
(value type)=> The body is a
UnaryExpression
withNodeType = Convert
andType = typeof(object)
, and itsOperand
is aMemberExpression
I modified your method slightly to take that into account, and it seems to work fine :