CustomerValidator: AbstractValidator<Customer>
How might one dynamically instantiate the class above if passed an instance of a Customer?? Similarly if I had:
Cat c = new Cat();
I would want to dynamically invoke the class that implements
AbstractValidator<Cat>
One common approach (if you own both
CustomerandCustomerValidator) is to decorate the class with the class that provides validation, via an attribute:Note that you may find it easier to work outside of generics, perhaps via an interface (note: no methods etc shown here):
Then obtain the correct validator via reflection:
So calling
GetValidatorshould returnnullor a suitableIValidator.You can use generics in the above – but it usually creates more problems than it solves in example like this.