public WhenThenClause SplitToWhenThenClause(string text) { if (text.StartsWith("when", StringComparison.InvariantCultureIgnoreCase) == false) throw new InvalidOperationException("statement should start with a when"); int thenIndex = text.IndexOf("then", StringComparison.InvariantCultureIgnoreCase); if (thenIndex == -1) throw new InvalidOperationException("statement should have a then"); WhenThenClause clause = new WhenThenClause(); string whenClause = text.Substring(4, thenIndex - 4).Trim(); ParseClause(whenClause, delegate(string[] parts) { ActionExpression item = new ActionExpression(); item.Left = parts[0]; item.Operator = parts[1]; item.Right = parts[2]; clause.When.Add(item); }); string thenClause = text.Substring(thenIndex + 4); ParseClause(thenClause, delegate(string[] parts) { ActionExpression item = new ActionExpression(); item.Left = parts[0]; item.Right= parts[1]; item.Operator = parts[2]; clause.Then.Add(item); }); return clause; }
public object Invoke(ActionExpression expression) { object obj = parameters[expression.Left]; if (obj == null) { throw new InvalidOperationException("Could not find parameter with name: " + expression.Left); } MethodInfo method = obj.GetType().GetMethod(expression.Operator, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (method == null) { throw new InvalidOperationException("Could not find method operator " + expression.Operator + " on " + expression.Left); } ParameterInfo[] methodParams = method.GetParameters(); if (methodParams.Length != 1) { throw new InvalidOperationException(expression.Operator + " should accept a single parameter"); } object converted; Type paramType = methodParams[0].ParameterType; if (paramType.IsEnum) { converted = Enum.Parse(paramType, expression.Right, true); } else { converted = Convert.ChangeType(expression.Right, paramType, System.Globalization.CultureInfo.InvariantCulture); } return(method.Invoke(obj, new object[] { converted })); }
public WhenThenClause SplitToWhenThenClause(string text) { if (text.StartsWith("when", StringComparison.InvariantCultureIgnoreCase) == false) { throw new InvalidOperationException("statement should start with a when"); } int thenIndex = text.IndexOf("then", StringComparison.InvariantCultureIgnoreCase); if (thenIndex == -1) { throw new InvalidOperationException("statement should have a then"); } WhenThenClause clause = new WhenThenClause(); string whenClause = text.Substring(4, thenIndex - 4).Trim(); ParseClause(whenClause, delegate(string[] parts) { ActionExpression item = new ActionExpression(); item.Left = parts[0]; item.Operator = parts[1]; item.Right = parts[2]; clause.When.Add(item); }); string thenClause = text.Substring(thenIndex + 4); ParseClause(thenClause, delegate(string[] parts) { ActionExpression item = new ActionExpression(); item.Left = parts[0]; item.Right = parts[1]; item.Operator = parts[2]; clause.Then.Add(item); }); return(clause); }
public object Invoke(ActionExpression expression) { object obj = parameters[expression.Left]; if (obj == null) throw new InvalidOperationException("Could not find parameter with name: " + expression.Left); MethodInfo method = obj.GetType().GetMethod(expression.Operator, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (method == null) throw new InvalidOperationException("Could not find method operator " + expression.Operator + " on " + expression.Left); ParameterInfo[] methodParams = method.GetParameters(); if(methodParams.Length!=1) throw new InvalidOperationException(expression.Operator + " should accept a single parameter"); object converted; Type paramType = methodParams[0].ParameterType; if(paramType.IsEnum) { converted = Enum.Parse(paramType, expression.Right,true); } else { converted = Convert.ChangeType(expression.Right, paramType, System.Globalization.CultureInfo.InvariantCulture); } return method.Invoke(obj, new object[] {converted}); }