TypeArgumentException in silverlight lambda expression
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.
精彩评论