-2
當我使用:本地化數據驗證錯誤
[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;
}
}