//Computes the value and puts the result to property. public void Assign(string key, string expression) { string name = key.Substring(0, key.LastIndexOf(PROPERTY_CHAR)); string prop = key.Substring(key.LastIndexOf(PROPERTY_CHAR) + 1); IVariable var = base.Variable(name); try { System.Reflection.PropertyInfo propertyInfo = var.GetType().GetProperty(prop); Type type = propertyInfo.PropertyType; object calcValue = this.Compute(expression, var); if (object.ReferenceEquals(type, typeof(float))) { calcValue = Convert.ToSingle(calcValue); } else if (object.ReferenceEquals(type, typeof(int))) { calcValue = Convert.ToInt32(calcValue); } else if (!object.ReferenceEquals(type, typeof(string)) && calcValue is string) { calcValue = Convert.ToString(calcValue); } var.GetType().InvokeMember(prop, System.Reflection.BindingFlags.SetProperty, null, var, new object[] {calcValue}); } catch (Exception ex) { //Collection exceptions. if (ex is ExpressionException) { m_exceptions.AddException((ExpressionException)ex); } else if (ex is MissingMemberException) { //Incompatible datatype between property and expression. ExpressionException expressionException = new ExpressionException("Expression result is not compatible with property", 0, expression.Length); Expression exp = new Expression(); exp.SetExpression(key, expression, this); expressionException.Expression = exp; m_exceptions.AddException(expressionException); } } }
//Computes all values. public void ComputeExpressions(ICollection expressions) { if (expressions == null) { expressions = m_expressions.Values; } IVariable var; object[] param = new object[1]; foreach (Expression expr in expressions) { //It was a property. string name = expr.Key.Substring(0, expr.Key.LastIndexOf(PROPERTY_CHAR)); string prop = expr.Key.Substring(expr.Key.LastIndexOf(PROPERTY_CHAR) + 1); var = base.Variable(name); try { System.Reflection.PropertyInfo propertyInfo = var.GetType().GetProperty(prop); Type type = propertyInfo.PropertyType; // This code need to be synchronized with ExpressionValidationHelper.ConvertValue. object calcValue = ComputeVariable(expr.Key, var); if (object.ReferenceEquals(type, typeof(float))) { calcValue = Convert.ToSingle(calcValue); } else if (object.ReferenceEquals(type, typeof(int))) { calcValue = Convert.ToInt32(calcValue); } else if (object.ReferenceEquals(type, typeof(bool))) { calcValue = Convert.ToBoolean(calcValue); } /* * 01.01.2012 If this code is not required, remove it. else if (!object.ReferenceEquals(type, typeof(string)) && calcValue is string) { calcValue = Convert.ToString(calcValue); } */ else if (object.ReferenceEquals(type, typeof(string))) { calcValue = Convert.ToString(calcValue); } param[0] = calcValue; var.GetType().InvokeMember(prop, System.Reflection.BindingFlags.SetProperty, null, var, param); } catch (Exception ex) { //Collection exceptions. if (ex is ExpressionException) { m_exceptions.AddException((ExpressionException)ex); } else if (ex is MissingMemberException) { //Incompatible datatype between property and expression. ExpressionException expressionException = new ExpressionException("Expression result is not compatible with property", 0, expr.Formula.Length); expressionException.Expression = expr; m_exceptions.AddException(expressionException); } } } }
public void AddException(ExpressionException ex) { m_exceptions.Add(ex); }