I have created a validation attribute. And the IsValid
method is as follows
[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;
}
}
And the attribute is used as below
[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; }
}
}
There are some display attributes but attr
is always null. How to get other properties custom attributes?