“System.Int32”类型的表达式不能用于返回类型“System.Object”
我正在尝试制作一个简单的脚本系统,用于打印标签。我过去通过反射完成此操作没有任何问题,但我现在尝试使用 Lambda 函数来完成此操作,以便我可以缓存函数以供重用。
到目前为止我的代码如下...
public static string GetValue<T>(T source, string propertyPath) {
try {
Func<T, Object> func;
Type type = typeof(T);
ParameterExpression parameterExpression = Expression.Parameter(type, @"source");
Expression expression = parameterExpression;
foreach (string property in propertyPath.Split('.')) {
PropertyInfo propertyInfo = type.GetProperty(property);
expression = Expression.Property(expression, propertyInfo);
type = propertyInfo.PropertyType;
}
func = Expression.Lambda<Func<T, Object>>(expression, parameterExpression).Compile();
object value = func.Invoke(source);
if (value == null)
return string.Empty;
return value.ToString();
}
catch {
return propertyPath;
}
}
这似乎在某些情况下有效,但在其他情况下却失败了。问题似乎在于我尝试将值作为对象返回 - 无论实际数据类型如何。我尝试这样做是因为我在编译时不知道数据类型是什么,但从长远来看,我只需要一个字符串。
每当我尝试访问 Int32 类型的属性时,我都会收到此消息标题中显示的异常 - 但我也会收到可为 Null 类型和其他类型的异常。 当我尝试将表达式编译到函数中时抛出异常。
谁能建议我如何以不同的方式解决这个问题,同时维护 Lambda 功能,以便我可以缓存访问器?
I am trying to produce a simple scripting system that will be used to print labels. I have done this in the past with reflection with no problem, but I am now trying to do it with Lambda functions so that I can cache the functions for reuse.
The code I have so far is as follows...
public static string GetValue<T>(T source, string propertyPath) {
try {
Func<T, Object> func;
Type type = typeof(T);
ParameterExpression parameterExpression = Expression.Parameter(type, @"source");
Expression expression = parameterExpression;
foreach (string property in propertyPath.Split('.')) {
PropertyInfo propertyInfo = type.GetProperty(property);
expression = Expression.Property(expression, propertyInfo);
type = propertyInfo.PropertyType;
}
func = Expression.Lambda<Func<T, Object>>(expression, parameterExpression).Compile();
object value = func.Invoke(source);
if (value == null)
return string.Empty;
return value.ToString();
}
catch {
return propertyPath;
}
}
This seems to work in some cases, but in others it fails. The problem seems to be in my trying to return the values as objects - irrespective of the actual data types. I am trying to do this because I do not know at compile time what the data type will be but in the long run, I only need a string.
I am getting the exception shown in the title of this message whenever I try to access a property of type Int32 - but I am also getting it for Nullable types and others.
The exception is thrown when I try to compile the expression into the function.
Can anybody suggest how I might go about this differently whilst maintaining the Lambda functionality so that I can cache the accessors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用Expression.Convert?这将增加拳击/举重/等转换。
Have you tried using Expression.Convert? That will add the boxing/lifting/etc conversion.
我希望这段代码能帮助你
I hope this code will help you