DataAnnotations不适用于好友类。以下代码始终验证为true。为什么?
var isValid = Validator.TryValidateObject(new Customer(),Context,results,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; }
}
}
这是另一个有相同问题的主题,但没有答案。 link text
答案 0 :(得分:28)
我在这里找到答案:http://forums.silverlight.net/forums/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);
答案 1 :(得分:4)
经过一些研究后,如果我使用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;
}
答案 2 :(得分:1)
虽然我没有在.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;
}
}
答案 3 :(得分:1)
存在一个问题,即对象上下文无法识别MetadataType属性。虽然您可以在验证之前手动添加类型描述符:
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer), typeof(CustomerMetaData)), typeof(Customer));
更简洁的方法是更新Entity Model .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; }
}