【发布时间】:2013-02-12 21:26:42
【问题描述】:
我想创建一个动态的Expression<Func<T,Y>>。这是适用于字符串但不适用于 DateTime 的代码。不起作用我的意思是,我得到了这个例外:
“'System.Nullable`1[System.DateTime]' 类型的表达式不能 用于返回类型'System.Object'"
有人可以analyze这个错误吗?
Type type = typeof(DSVPNProjection);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
PropertyInfo propertyInfo = type.GetProperty(sidx);
expr = Expression.Property(expr, propertyInfo);
var expression =
Expression.Lambda<Func<DSVPNProjection, object>>(expr, arg);
我需要将object 更改为其他类型吗?如果是,那么是哪个?如您所见,我正在尝试动态获取 PropertyInfo 并将其用作 Func 中的第二个参数。
【问题讨论】:
-
它到底是怎么不起作用的?它会抛出异常吗?消息是什么?