I am trying to get the value of the parameters passed into the expression. The system will use the error message factory, passing in values that have come from a parent / calling method. If I hard code a value and pass it into the expression the Method.Arguments array will have the actual value and the method below will extract that value. If it is passed in from a parent method it will end up getting what seems like a representation of the method call signature
.Constant<AutoValidator.Impl.Validator+<>c__DisplayClass7_0>(AutoValidator.Impl.Validator+<>c__DisplayClass7_0).minLength
I am not sure if I am some how passing the values down incorrectly or I am trying to get the actual value of them incorrectly.
//the expression will receive the value 123
_errorMessageFactory.Get<string>((val, exp) => exp.MinLength(text, 123, message), propName);
//we pass xx which has the value of 123, but the expression doesn't show this value
var xx = 123;
_errorMessageFactory.Get<string>((val, exp) => exp.MinLength(text, xx, message), propName);
public Tuple<string, List<object>> Get<TMember>(Expression<Func<TMember, IValidatorExpression, bool>> exp, string propName)
{
var methodCall = exp.Body as MethodCallExpression;
var methodSignature = methodCall.Method.ToString();
GetArgumentValue(methodCall.Arguments[1]);
}
private object GetArgumentValue(Expression methodExpression)
{
if (methodExpression.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = (MemberExpression)methodExpression;
return GetArgumentValue(memberExpression.Expression);
}
else if (methodExpression.NodeType == ExpressionType.Constant)
{
var constExp = methodExpression as ConstantExpression;
return constExp?.Value;
}
throw new ArgumentOutOfRangeException("Unknown expression argument type");
}