1

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?

MJK
  • 3,434
  • 3
  • 32
  • 55

2 Answers2

1

After finding this thread, I have found the way to get the display attribute.

 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;
    }

Hope, it may help someone.

Community
  • 1
  • 1
MJK
  • 3,434
  • 3
  • 32
  • 55
0

You have decorated your properties with instances of class DisplayAttribute.

[Display(Name = "First Name")]
public override string christian { get; set; }

But then you try to get attributes of type DisplayNameAttribute:

GetCustomAttribute(propItem, typeof(DisplayNameAttribute))
M4N
  • 94,805
  • 45
  • 217
  • 260
  • Sorry, that was typo. I have modified the actual questions. But, I am not able to get any attributes of other properties. DisplayAttribute is an example. What am I missing? – MJK Apr 27 '15 at 11:49