所以我有一种情况,我需要根据包含的值动态更新对象的属性。在下面的情况下,如果条件为真,我需要用不同的字符串替换当前值的前两个字符来更新值。
PersonDetail.EvaluateConditionalRule("ID",
"((ID.Length > Convert.ToInt32(@0) ) AND ID.Substring(Convert.ToInt32(@1), Convert.ToInt32(@2)) == @3 )",
new[] { "1", "0", "2", "SS" }, " ID = (@0 + ID.Substring(Convert.ToInt32(@1))) " , new[] { "98", "2" });
public static void EvaluateConditionalRule(this PersonDetail Detail, String PropertyToEvaluate,
String ConditionalExpression, String[] parameters, String IfTrueExpression,String[] IfTrueExpreassionparameters )
{
var property = Detail.GetType().GetProperties().Where(x => x.Name == PropertyToEvaluate).FirstOrDefault();
if (property == null)
throw new InvalidDataException(String.Format("Please specify a valid {0} property name for the evaluation.", Detail.GetType()));
//put together the condition like so
if (new[] { Detail }.AsQueryable().Where(ConditionalExpression, parameters).Count() > 0 && IfTrueExpression != null)
{
var result = new[] { Detail }.AsQueryable().Select(IfTrueExpression, IfTrueExpreassionparameters);
//Stuck Here as result does not contain expected value
property.SetValue( Detail,result , null);
}
}
基本上我想要的是,能够执行提供给它的表达式,而且我认为我没有正确评估子字符串替换表达式的格式。我想从上面是这样的
ID = "98"+ ID.Substring(2);
任何帮助将不胜感激。谢谢