2014-09-10 59 views
0

我對現場名字在我DataContract下面的代碼與以下驗證屬性:本地化StringLengthValidator沒有得到正確的資源串

[DataMember] 
    [Required(ErrorMessageResourceType = typeof(PatientDataContractRes), 
      ErrorMessageResourceName = "LastNameRequired")] 
    [StringLengthValidator(1, 50, ErrorMessageResourceName = "LastNameLength" 
    , ErrorMessageResourceType = typeof(PatientDataContractRes))] 
    public string LastName { get; set; } 

當我改變了文化所需要的屬性是工作的罰款,同時運行該應用程序,但StringLengthValidator似乎總是得到默認的資源字符串。我的應用程序使用英文和法文,StringLengthValidator始終返回英文文本。這兩個屬性都使用相同的資源。那麼爲什麼它只與Required屬性一起工作呢?

+0

查找出在'LastNameLength'錯別字(在你的代碼和資源,而不是你的問題)。我的人生中見過太多「長」字。 – 2014-09-10 21:18:09

回答

0

我解決了這個問題,添加的CultureInfo在PropertyValidatorCacheKey的構造和固定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); 
     } 


    }