Enterprise Library の Validation Application Block が気に入っています :-)
asp.net Dynamic Data も使用しているため、Winforms で DataAnnotations を使用したいと思います。そのため、全社共通の技術を持っています。
また、データ注釈も使いやすくなります。
Stephen Walter が asp.net MVC 内で行ったように、Winforms で同様のことを行うにはどうすればよいですか?
Enterprise Library の Validation Application Block が気に入っています :-)
asp.net Dynamic Data も使用しているため、Winforms で DataAnnotations を使用したいと思います。そのため、全社共通の技術を持っています。
また、データ注釈も使いやすくなります。
Stephen Walter が asp.net MVC 内で行ったように、Winforms で同様のことを行うにはどうすればよいですか?
http://blog.codeville.net/category/validation/page/2/にあるソリューションを採用しました
public class DataValidator
{
public class ErrorInfo
{
public ErrorInfo(string property, string message)
{
this.Property = property;
this.Message = message;
}
public string Message;
public string Property;
}
public static IEnumerable<ErrorInfo> Validate(object instance)
{
return from prop in instance.GetType().GetProperties()
from attribute in prop.GetCustomAttributes(typeof(ValidationAttribute), true).OfType<ValidationAttribute>()
where !attribute.IsValid(prop.GetValue(instance, null))
select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty));
}
}
これにより、次のコードを使用して、次の構文を使用して任意のオブジェクトを検証できます。
var errors = DataValidator.Validate(obj);
if (errors.Any()) throw new ValidationException();