【发布时间】:2013-09-06 21:53:05
【问题描述】:
所以我有一种情况,我需要根据包含的值动态更新对象的属性。在下面的情况下,如果条件为真,我需要用不同的字符串替换当前值的前两个字符来更新值。
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);
任何帮助将不胜感激。谢谢
【问题讨论】:
-
也许这可以帮助你:C# 中的谓词代表 stackoverflow.com/questions/556425/…
-
是否必须在字符串而不是 Lambda 表达式中有条件?
-
是的,这是必要的。就像我说的那样,我正在努力使它尽可能动态。