验证自定义属性时获取其他 属性 属性
Get other property attributes while validating a custom attribute
我已经创建了一个验证属性。而IsValid
方法如下
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public sealed class BlaBlaAttribute : ValidationAttribute
{
private readonly object _typeId = new object();
//...........
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherProperties = validationContext.ObjectType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).ToList();
foreach (void propItem in otherProperties) {
// propItem.CustomAttributes.Count is zero for each and every property
DisplayAttribute attr = (DisplayAttribute)Attribute.GetCustomAttribute(propItem, typeof(DisplayAttribute));
if (attr == null) {
//............
}
}
return ValidationResult.Success;
}
}
属性使用如下
[MetadataType(typeof(SiteContact.SiteContactMD))]
public class SiteContact
{
public class SiteContactMD
{
[BlaBla()]
[Display(Name = "First Name")]
public override string christian { get; set; }
[Display(Name = "Last Name")]
public virtual string surname { get; set; }
[BlaBla()]
[Display(Name = "Email Id")]
public override string EMail { get; set; }
}
}
有一些显示属性,但attr
始终为空。如何获取其他属性的自定义属性?
您已使用 class DisplayAttribute
.
实例装饰您的属性
[Display(Name = "First Name")]
public override string christian { get; set; }
但随后您尝试获取 DisplayNameAttribute
:
类型的属性
GetCustomAttribute(propItem, typeof(DisplayNameAttribute))
找到this thread后,我找到了获取显示属性的方法
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherProperties = validationContext.ObjectType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).ToList();
//To get the assigned MetaDataAttribute for the class
var metaData = validationContext.ObjectType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().ToArray().FirstOrDefault();
foreach (void propItem in otherProperties) {
if (metaData != null)
{
// Get display attributes for the property
var attrs = metaData.MetadataClassType.GetProperty(propItem ).GetCustomAttributes(typeof(DisplayAttribute), true).OfType<DisplayAttribute>.ToArray();
if (attrs.Count > 0)
{
this.OtherPropertyDisplayName = ((DisplayAttribute)attrs.FirstOrDefault()).Name;
//.............
}
}
}
return ValidationResult.Success;
}
希望,它可能对某人有所帮助。
我已经创建了一个验证属性。而IsValid
方法如下
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public sealed class BlaBlaAttribute : ValidationAttribute
{
private readonly object _typeId = new object();
//...........
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherProperties = validationContext.ObjectType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).ToList();
foreach (void propItem in otherProperties) {
// propItem.CustomAttributes.Count is zero for each and every property
DisplayAttribute attr = (DisplayAttribute)Attribute.GetCustomAttribute(propItem, typeof(DisplayAttribute));
if (attr == null) {
//............
}
}
return ValidationResult.Success;
}
}
属性使用如下
[MetadataType(typeof(SiteContact.SiteContactMD))]
public class SiteContact
{
public class SiteContactMD
{
[BlaBla()]
[Display(Name = "First Name")]
public override string christian { get; set; }
[Display(Name = "Last Name")]
public virtual string surname { get; set; }
[BlaBla()]
[Display(Name = "Email Id")]
public override string EMail { get; set; }
}
}
有一些显示属性,但attr
始终为空。如何获取其他属性的自定义属性?
您已使用 class DisplayAttribute
.
[Display(Name = "First Name")]
public override string christian { get; set; }
但随后您尝试获取 DisplayNameAttribute
:
GetCustomAttribute(propItem, typeof(DisplayNameAttribute))
找到this thread后,我找到了获取显示属性的方法
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherProperties = validationContext.ObjectType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).ToList();
//To get the assigned MetaDataAttribute for the class
var metaData = validationContext.ObjectType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().ToArray().FirstOrDefault();
foreach (void propItem in otherProperties) {
if (metaData != null)
{
// Get display attributes for the property
var attrs = metaData.MetadataClassType.GetProperty(propItem ).GetCustomAttributes(typeof(DisplayAttribute), true).OfType<DisplayAttribute>.ToArray();
if (attrs.Count > 0)
{
this.OtherPropertyDisplayName = ((DisplayAttribute)attrs.FirstOrDefault()).Name;
//.............
}
}
}
return ValidationResult.Success;
}
希望,它可能对某人有所帮助。