当我使用:
[Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "MessageIISPathRequired")]
[CustomValidation(typeof (IISVM), "WebsiteRootExists", ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "MessageIISPathInvalid")]
public string WebsiteRoot
{
get { return _data.WebsiteRoot; }
set { SetProperty("WebsiteRoot", () => _data.WebsiteRoot == value, () => _data.WebsiteRoot = value); }
}
我收到一条空消息,并且在我的资源文件中该消息在那里并且是公开的。这不是 MVC 应用程序,它是 WPF 桌面应用程序,在我将其转换为使用资源之前,我的所有验证都有效。
我认为这与我如何抓住错误有关:
private static ValidationAttribute[] GetValidations(PropertyInfo property)
{
return property.GetCustomAttributes(typeof (ValidationAttribute), true) as ValidationAttribute[];
}
private static Func<IISVM, object> GetValueGetter(PropertyInfo property)
{
LinqExpress.ParameterExpression instance = LinqExpress.Expression.Parameter(typeof (IISVM), "i");
LinqExpress.UnaryExpression cast =
LinqExpress.Expression.TypeAs(LinqExpress.Expression.Property(instance, property),
typeof (object));
return (Func<IISVM, object>) LinqExpress.Expression.Lambda(cast, instance).Compile();
}
而我的IDataErrorInfo
实现
public string this[string columnName]
{
get
{
if (PropertyGetters.ContainsKey(columnName))
{
object value = PropertyGetters[columnName](this);
string[] errors =
Validators[columnName].Where(v => !v.IsValid(value)).Select(v => v.ErrorMessage).ToArray();
return string.Join(Environment.NewLine, errors);
}
return string.Empty;
}
}