开发者

TypeArgumentException in silverlight lambda expression

开发者 https://www.devze.com 2023-03-23 08:59 出处:网络
I\'ve defined the following method public static Expression<Func<T,dynamic>> CreateExpression(string propName)

I've defined the following method

public static Expression<Func<T,dynamic>> CreateExpression(string propName)
{
ParameterExpression param = Expression.Parameter(typeof(T));

MemberExpression aggregator = Expression.PropertyOrField(parameter,propName);

return Expression.Lambda<Func<T,dynamic>>(aggregator,param);
}

The code compiles OK but in the runtime the last line throws argumentexception stating 'Expression of Type开发者_Python百科 'System.Int32' cannot be used for return type 'System.Object'

Am i missing something?

Thanks in advance


Use Expression.Convert. That will add the boxing conversion you require to turn the Int32 basic type (or any basic type you reference) into an object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号