1
다음 코드와 같이 사용자 정의 유효성 검사기를 사용하는 모델 클래스가 있습니다.두 개의 다른 모델에서 동일한 유효성 검사기를 사용하는 방법
public class Model1
{
[ApplicationIdentifierValidator()]
public Guid ApplicationIdentifier { get; set; }
public string ApplicationName { get; set; }
public string ApplicationType { get; set; }
}
public class ApplicationIdentifierValidator: ValidationAttribute
{
public ApplicationIdentifierValidator() { }
protected override ValidationResult IsValid(object value, ValidationContext context)
{
Model1 model = (Model1)(context.ObjectInstance);
Guid ApplicationIdentifier = model.ApplicationIdentifier;
// Validate ApplicationIdentifier value
return ValidationResult.Success;
}
}
이제 동일한 유효성 검사기를 사용해야하는 다른 모델 클래스를 추가했습니다. ApplicationIdentifierValidator이 모델 2의 문맥에서 사용되는 경우
public class Model2
{
[ApplicationIdentifierValidator()]
public Guid ApplicationIdentifier { get; set; }
public Guid SystemIdentifier { get; set; }
}
문제 MODEL1
Model1 model = (Model1)(context.ObjectInstance);
같은 유효성 검사기를 다시 사용하기 위해 더 나은 방법이 될 수 있는지 어떤 아이디어로 캐스팅 할 때, 그것은 예외가 발생입니까?