我有下面的代码,我需要一些帮助修复请。我想要的是向此方法传递一个String Expression和一个String []参数,并将Expression编译并应用于列表中的各个项目。动态ForEach循环更新对象属性值
public static IEnumerable<T> ForEach<T>(this IList<T> source, string expression, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (expression == null) throw new ArgumentNullException("expression");
var enumerableList = source.AsEnumerable<T>();
return (from T item in source
select (T) DynamicLambdaExpression.ParseLambda(item.GetType(), typeof(T), expression, values).Compile().DynamicInvoke(item));
}
AS是,表达很可能被应用,但来自每个操作返回的类型不是我期望的类型。不知道,因为我运行它时遇到异常。
<IfTrue Expression="it.UID = @0 + it.index.ToString(@1)" Parameters="000000 D6"/>
该错误消息我不断收到说
System.Linq.Dynamic.ParseException发生 的HResult = -2146233088 消息=类型的表达式IProductDetail“预计 来源=动态 位置= 0 堆栈跟踪:在System.Linq.Dynamic.ExpressionParser.Parse(类型与resultType)
所以,我希望得到任何帮助在固定的代码,并解释请问我做错了什么。提前致谢。
你有什么错误? – SLaks
不要重新编译列表中每个项目的表达式。 – SLaks
如果您有返回类型,这更像是一个'Select'操作(映射),而不是'ForEach'。你的回报类型将基于你在表达式中传递的信息... –