我正在尝试使用Validar在我的课程中注入验证。我的解决方案由多个(目前为 5 个,未来可能会更多)项目组成,我想在其中注入验证。因此,我在其中一个中定义了我的ValidationTemplate
类,并将其放置ValidationTemplateAttribute
在每个程序集中,如下所示:
using Validar;
[assembly: ValidationTemplate(typeof(IMS.General.Validation.ValidationTemplate))]
当我构建时,我收到一个我不理解的错误,但阻止了我更进一步:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(268,9): error MC1000: Unknown build error, 'Could not load type 'Validar.ValidationTemplateAttribute' from assembly 'mscorlib, Version= 4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089'。'
我正在使用 Visual Studio 2013 专业更新 4,目标框架是 .Net 框架 4.5
如果它对这个问题有任何用处,我的实现ValidationTemplate
如下所示:
namespace IMS.General.Validation
{
public class ValidationTemplate : INotifyDataErrorInfo
{
private readonly INotifyPropertyChanged target;
private readonly ValidationContext validationContext;
private readonly List<ValidationResult> validationResults;
public ValidationTemplate(INotifyPropertyChanged target)
{
this.target = target;
this.validationContext = new ValidationContext(target, null, null);
this.validationResults = new List<ValidationResult>();
Validator.TryValidateObject(this.target, this.validationContext,
this.validationResults, true);
target.PropertyChanged += Validate;
}
private void Validate(object sender, PropertyChangedEventArgs e)
{
this.validationResults.Clear();
Validator.TryValidateObject(this.target, this.validationContext,
this.validationResults, true);
var hashSet = new HashSet<string>(
this.validationResults.SelectMany(x => x.MemberNames));
foreach (var error in hashSet)
{
this.ErrorsChanged(this,
new DataErrorsChangedEventArgs(error));
}
}
public IEnumerable GetErrors(string propertyName)
{
return this.validationResults
.Where(x => x.MemberNames.Contains(propertyName))
.Select(x => x.ErrorMessage);
}
public bool HasErrors
{
get { return this.validationResults.Count > 0; }
}
public event EventHandler<DataErrorsChangedEventArgs>
ErrorsChanged = (s, e) => { };
}
}
我做错了什么,我该如何解决这个问题?
编辑: 来吧伙计们!真的没有人可以帮助我解决这个问题。我应该做一个测试解决方案来显示问题吗?请指教!我真的需要一个解决方案!Fody 通常工作得很好,在保持课堂整洁的同时为我节省了大量工作!