无法访问ValidationAttribute

时间:2013-03-18 21:40:15

标签: c# .net data-annotations

我正在尝试设置一些使用System.ComponentModel.DataAnnotations的自定义验证方法。我已经编写了一些用于验证的通用方法。这是问题发生的地方:

private void SetValidationAttributeErrors<TEntity>(TEntity item, List<string> errors)
    {
        var result = from prop in TypeDescriptor.GetProperties(item).Cast<PropertyDescriptor>()
                     from attribute in prop.Attributes.OfType<ValidationAttribute>()
                     where !attribute.IsValid(prop.GetValue(item))
                     select attribute.FormatErrorMessage(string.Empty);
    }

我在'ValidationAttribute'类型上有一个错误,说它无法找到。我有DataAnnotations的using语句。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

没关系!我想到了。显然使用声明是不够的。我还必须将项目引用添加到包中。