我正在尝试将 NHibernate.Validator 与 ASP.NET MVC 客户端验证集成,我发现的唯一问题是我根本无法将非插值消息转换为人类可读的消息。我认为这将是一项简单的任务,但结果证明这是客户端验证中最难的部分。主要问题是因为它不是服务器端的,我实际上只需要正在使用的验证属性,而且我实际上没有实例或其他任何东西。
以下是我已经尝试过的一些摘录:
// Get the the default Message Interpolator from the Engine
IMessageInterpolator interp = _engine.Interpolator;
if (interp == null)
{
// It is null?? Oh, try to create a new one
interp = new NHibernate.Validator.Interpolator.DefaultMessageInterpolator();
}
// We need an instance of the object that needs to be validated, se we have to create one
object instance = Activator.CreateInstance(Metadata.ContainerType);
// we enumerate all attributes of the property. For example we have found a PatternAttribute
var a = attr as PatternAttribute;
// it seems that the default message interpolator doesn't work, unless initialized
if (interp is NHibernate.Validator.Interpolator.DefaultMessageInterpolator)
{
(interp as NHibernate.Validator.Interpolator.DefaultMessageInterpolator).Initialize(a);
}
// but even after it is initialized the following will throw a NullReferenceException, although all of the parameters are specified, and they are not null (except for the properties of the instance, which are all null, but this can't be changed)
var message = interp.Interpolate(new InterpolationInfo(Metadata.ContainerType, instance, PropertyName, a, interp, a.Message));
我知道上面是一个看似简单的问题的相当复杂的代码,但我仍然没有解决方案。有什么方法可以从 NHValidator 中取出插值字符串?