如何获取谓词主体?
我有一个非常简单的问题,但我无法弄清楚。
方法代码很简单:
protected void Require<TValidator, TParam>(TValidator validator, Expression<Func<TValidator, TParam>> property, Predicate<TParam> predicate)
{
var propertyValue = property.Compile().Invoke(validator);
if(!predicate.Invoke(propertyValue))
throw new ValidatorInitializationException("Error while initializing validator", GetType());
}
问题是我想将更多信息打包到错误消息中。从表达式中获取信息很容易。但是如何获得谓词的“用户友好”字符串表示形式呢?
I Have a quite simple question that I just cant figure out.
The method code is simple:
protected void Require<TValidator, TParam>(TValidator validator, Expression<Func<TValidator, TParam>> property, Predicate<TParam> predicate)
{
var propertyValue = property.Compile().Invoke(validator);
if(!predicate.Invoke(propertyValue))
throw new ValidatorInitializationException("Error while initializing validator", GetType());
}
The problem is that I would like to pack more info into the error message. Getting information out of the expression is easy. But how can I get to a "user friendly" string representation of the predicate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也必须接受它作为表达式树:
You'd have to accept that as an expression tree too: