S
shapper
Hello,
I have the following classes:
public class AssetEdit : AssetForm { }
public class AssetForm : PageBase, IAsset { }
And the validation factory as follows:
public class ValidatorFactory : IValidatorFactory {
private static Dictionary<Type, IValidator> _validators = new
Dictionary<Type, IValidator>();
static ValidatorFactory() {
_validators.Add(typeof(AssetEdit), new AssetFormValidator());
_validators.Add(typeof(AssetCreate), new AssetFormValidator());
} // ValidatorFactory
public IValidator<T> GetValidator<T>() {
return (IValidator<T>)GetValidator(typeof(T));
} // GetValidator
public IValidator GetValidator(Type type) {
IValidator validator;
if (_validators.TryGetValue(type, out validator))
return validator;
return null;
} // GetValidator
} // ValidatorFactory
AssetFormValidator is as follows:
public class AssetFormValidator : AbstractValidator<AssetForm> { }
And on my code I am doing the following:
IValidator v = _validatorFactory.GetValidator<AssetEdit>();
I get the error:
Unable to cast object of type 'Validators.AssetFormValidator' to type
'FluentValidation.IValidator`1[Models.AssetEdit]'.
On code line:
return (IValidator<T>)GetValidator(typeof(T));
What am I doing wrong?
Basically, I am trying to have both models AssetEdit and AssetCreate
to use the same validator AssetFormValidator.
Of course later I can create different validators for both necessary.
Thanks,
Miguel
I have the following classes:
public class AssetEdit : AssetForm { }
public class AssetForm : PageBase, IAsset { }
And the validation factory as follows:
public class ValidatorFactory : IValidatorFactory {
private static Dictionary<Type, IValidator> _validators = new
Dictionary<Type, IValidator>();
static ValidatorFactory() {
_validators.Add(typeof(AssetEdit), new AssetFormValidator());
_validators.Add(typeof(AssetCreate), new AssetFormValidator());
} // ValidatorFactory
public IValidator<T> GetValidator<T>() {
return (IValidator<T>)GetValidator(typeof(T));
} // GetValidator
public IValidator GetValidator(Type type) {
IValidator validator;
if (_validators.TryGetValue(type, out validator))
return validator;
return null;
} // GetValidator
} // ValidatorFactory
AssetFormValidator is as follows:
public class AssetFormValidator : AbstractValidator<AssetForm> { }
And on my code I am doing the following:
IValidator v = _validatorFactory.GetValidator<AssetEdit>();
I get the error:
Unable to cast object of type 'Validators.AssetFormValidator' to type
'FluentValidation.IValidator`1[Models.AssetEdit]'.
On code line:
return (IValidator<T>)GetValidator(typeof(T));
What am I doing wrong?
Basically, I am trying to have both models AssetEdit and AssetCreate
to use the same validator AssetFormValidator.
Of course later I can create different validators for both necessary.
Thanks,
Miguel