当我使用Validator.TryValidateObject验证不起作用
-
19-09-2019 - |
题
DataAnnotations不与好友类工作。下面的代码始终验证正确的。为什么呢?
变种的isValid = Validator.TryValidateObject(新顾客(),上下文,结果,TRUE);
和这里是哥们类。
public partial class Customer
{
public string Name { get; set; }
public int Age { get; set; }
}
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
public class CustomerMetaData
{
[Required(ErrorMessage = "You must supply a name for a customer.")]
public string Name { get; set; }
}
}
下面是另一个线程具有同样的问题,但没有答案。 链接文本
解决方案
我找到了答案在这里: http://forums.silverlight.net/论坛/ p / 149264 / 377212.aspx
MVC识别MetaDataType属性,但其他项目没有。验证之前,需要手工注册元数据类:
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer), typeof(CustomerMetadata)), typeof(Customer));
var isValid = Validator.TryValidateObject(new Customer(), context, results, true);
其他提示
经过一番研究,我找不到任何理由,如果我使用MetadataType(哥们类)为什么TryValidateObject始终返回true。但它与下面的代码(XVAL)。
public static IEnumerable<ErrorInfo> GetErrors(object instance, string name)
{
var metadataAttrib = instance.GetType()
.GetCustomAttributes(typeof(MetadataTypeAttribute), true)
.OfType<MetadataTypeAttribute>().FirstOrDefault();
var buddyClassOrModelClass = metadataAttrib != null
? metadataAttrib.MetadataClassType
: instance.GetType();
var buddyClassProperties = TypeDescriptor.GetProperties(buddyClassOrModelClass)
.Cast<PropertyDescriptor>();
var modelClassProperties = TypeDescriptor.GetProperties(instance.GetType())
.Cast<PropertyDescriptor>();
var list = from buddyProp in buddyClassProperties
join modelProp in modelClassProperties on
buddyProp.Name equals modelProp.Name
from attribute in buddyProp.Attributes.OfType<ValidationAttribute>()
where !attribute.IsValid(modelProp.GetValue(instance))
select new ErrorInfo(
buddyProp.Name,
attribute.FormatErrorMessage(modelProp.Name),
instance);
if (name != null)
list = list.Where(x => x.PropertyName == name);
return list;
}
虽然我没有测试在.NET 4.0的代码,在.NET 3.5 / Silverlight 3中,元数据类应该是这样的:
[MetadataType(typeof(Customer.CustomerMetaData))]
public partial class Customer
{
internal sealed class CustomerMetaData
{
private CustomerMetaData()
{
}
[Required(ErrorMessage = "You must supply a name for a customer.")]
public string Name;
}
}
有是其中MetadataType属性没有被对象上下文识别的问题。虽然可以手动验证之前添加类型描述:
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer), typeof(CustomerMetaData)), typeof(Customer));
更简洁的方式来处理这将是更新实体模型.TT文件,添加以下各DTO:
Type currentType = MethodBase.GetCurrentMethod().DeclaringType;
object[] attributes = currentType.GetCustomAttributes(typeof(MetadataTypeAttribute),false);
if(attributes.Length > 0)
{
//MetadataType attribute found!
MetadataTypeAttribute metaDataAttribute = (MetadataTypeAttribute)attributes[0];
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(
currentType, metaDataAttribute.MetadataClassType),currentType);
}
这将允许您的属性添加到部分类:
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}
public partial class CustomerMetaData
{
[Required]
public string CustomerName { get; set; }
}
不隶属于 StackOverflow