C# 本地化StringLengthValidator未获得正确的资源字符串
我的C# 本地化StringLengthValidator未获得正确的资源字符串,c#,wpf,data-annotations,C#,Wpf,Data Annotations,我的DataContract中有以下代码,字段LastName具有以下验证属性: [DataMember] [Required(ErrorMessageResourceType = typeof(PatientDataContractRes), ErrorMessageResourceName = "LastNameRequired")] [StringLengthValidator(1, 50, ErrorMessageResourceName
DataContract
中有以下代码,字段LastName具有以下验证属性:
[DataMember]
[Required(ErrorMessageResourceType = typeof(PatientDataContractRes),
ErrorMessageResourceName = "LastNameRequired")]
[StringLengthValidator(1, 50, ErrorMessageResourceName = "LastNameLength"
, ErrorMessageResourceType = typeof(PatientDataContractRes))]
public string LastName { get; set; }
当我在运行应用程序时更改区域性时,Required属性工作正常,但是StringLengthValidator似乎总是获得默认的资源字符串。我的应用程序是英文和法文的,StringLengthValidator总是返回英文文本。两个属性使用相同的资源。那么为什么它只是使用
Required
属性呢?我解决了这个问题,在PropertyValidatorCacheKey的构造函数中添加CultureInfo,并修复PropertyValidationFactory以使用它
private struct PropertyValidatorCacheKey : IEquatable<PropertyValidatorCacheKey>
{
private Type sourceType;
private string propertyName;
private string ruleset;
private CultureInfo cultureInfo;
public PropertyValidatorCacheKey(Type sourceType, string propertyName, string ruleset, CultureInfo cultureInfo)
{
this.sourceType = sourceType;
this.propertyName = propertyName;
this.ruleset = ruleset;
this.cultureInfo = cultureInfo;
}
public override int GetHashCode()
{
return this.sourceType.GetHashCode() ^ this.propertyName.GetHashCode() ^
(this.ruleset != null ? this.ruleset.GetHashCode() : 0) ^ this.cultureInfo.GetHashCode();
}
#region IEquatable<PropertyValidatorCacheKey> Members
bool IEquatable<PropertyValidatorCacheKey>.Equals(PropertyValidatorCacheKey other)
{
return (this.sourceType == other.sourceType) && (this.propertyName.Equals(other.propertyName)) &&
(this.ruleset == null ? other.ruleset == null : this.ruleset.Equals(other.ruleset)) &&
(this.cultureInfo == other.cultureInfo);
}
}
private struct PropertyValidatorCacheKey:IEquatable
{
私有类型sourceType;
私有字符串propertyName;
私有字符串规则集;
私人文化信息文化信息;
公共属性ValidatorCacheKey(类型sourceType、字符串propertyName、字符串规则集、CultureInfo CultureInfo)
{
this.sourceType=sourceType;
this.propertyName=propertyName;
this.ruleset=规则集;
this.cultureInfo=cultureInfo;
}
公共覆盖int GetHashCode()
{
返回此.sourceType.GetHashCode()^this.propertyName.GetHashCode()^
(this.ruleset!=null?this.ruleset.GetHashCode():0)^this.cultureInfo.GetHashCode();
}
#区域可容纳成员
布尔IEquatable.Equals(PropertyValidatorCacheKey其他)
{
返回(this.sourceType==other.sourceType)&&(this.propertyName.Equals(other.propertyName))&&
(this.ruleset==null?other.ruleset==null:this.ruleset.Equals(other.ruleset))&&
(this.cultureInfo==其他.cultureInfo);
}
}
注意LastNameLength
中的打字错误(在您的代码和参考资料中,而不是在您的问题中)。在我的生活中,我见过太多的lengh
。