//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); } } }
//Add a new expression. public void AddExpression(string key, string expression) { Expression expr = new Expression(); expr.SetExpression(key, expression, this); this.m_expressions.Add(key, expr); }