Last active
August 20, 2020 02:11
-
-
Save mr5z/614df0780f0e0d5f85675313d528d2e3 to your computer and use it in GitHub Desktop.
validation service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidationService : IValidationService | |
{ | |
private BindableBase bindableBase; | |
private object ruleCollection; | |
private MethodInfo methodInfo; | |
public event EventHandler<ValidationResultArgs> PropertyInvalid; | |
public RuleCollection<VM> For<VM>(VM bindableBase) where VM : BindableBase | |
{ | |
this.bindableBase = bindableBase; | |
ruleCollection = new RuleCollection<VM>(); | |
bindableBase.PropertyChanged += BindableBase_PropertyChanged; | |
var type = typeof(RuleCollection<VM>); | |
methodInfo = type.GetMethod(nameof(RuleCollection<BindableBase>.GetRules)); | |
return (RuleCollection<VM>)ruleCollection; | |
} | |
// was ruleCollection.GetRules() | |
private List<IValidationRule> GetRules() | |
{ | |
var returnValue = methodInfo.Invoke(ruleCollection, null); | |
return (List<IValidationRule>)returnValue; | |
} | |
... | |
} | |
public class RuleCollection<VM> where VM : BindableBase | |
{ | |
private readonly List<IValidationRule> validationRuleList = new List<IValidationRule>(); | |
public RuleCollection<VM> AddRule<T>(Expression<Func<VM, object>> expression, params ValidationRule<T>[] rules) | |
{ | |
var propertyName = expression.GetMemberName(); | |
return AddRule(propertyName, rules); | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment