.netwpfvalidationfodyfody-validar

Fody plugin Validar fails. Could not load type 'Validar.ValidationTemplateAttribute' from assembly mscorlib


I'm trying to use Validar to inject validation in my classes. My solution consists of multiple (5 for now, can become more in the future) projects in which I want to inject the validation. So I defined my ValidationTemplate class in one of them and placed the ValidationTemplateAttribute in every assembly like this:

using Validar;

[assembly: ValidationTemplate(typeof(IMS.General.Validation.ValidationTemplate))]

When I build I receive an error which I do not understand, but stops me from going further:

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, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'

I'm using Visual Studio 2013 professional update 4 and the target framework is .Net framework 4.5

If it is of any use for the question my implementation of the ValidationTemplate looks like:

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) => { };
    }
}

What am I doing wrong and how can I solve this issue?

Edit: Come on guys!! Is there really nobody who can help me with this. Should I make a test solution to show the problem? Please advise! I'm really in need of a solution! Fody normally works great and saves me a lot of work while keeping my classes nice and clean!


Solution

  • After direct contact with the programmer of Validar he found that different versions of .Net framework treat importing into modules differently. He fixed the issues on released version 1.4.6. This fixes the problem.

    So my advice: Use Fody and Validar. It works great, has good support and keeps your code clean while having beautiful validation in your WPF application (or any other framework using INotifyDataErrorInfo).