Using reflection to obtain list of DisplayNames from Class properties

603 views Asked by At

I'm trying to obtain a List of DisplayNames out of a class that has most of its properties booleans:

public class AccessoriesModel
{
    public int Id { get; set; }

    [Display(Name = "Acc 1")]
    public bool Accessory1 { get; set; }

    [Display(Name = "Acc 2")]
    public bool Accessory2 { get; set; }

    [Display(Name = "Acc 3")]
    public bool Accessory3 { get; set; }

    [Display(Name = "Acc 4")]
    public bool Accessory4 { get; set; }
}

by iterating over the PropertyInfos of the class and seeing which values are true, as below:

    List<string> list = new List<string>();
    foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
        {
            if (propertyInfo.PropertyType == typeof(bool))
            {
                bool value = (bool)propertyInfo.GetValue(data, null);

                if (value)
                {
                   //add the DisplayName of the item who's value is true to the list named "list"

                   //the following line works fine, but I cannot iterate over the list of items to get dinamicaly build the list
                   string displayName = GetPropertyDisplayName<AccessoriesModel>(i => i.AirConditioning);

                   list.add(displayName)

                }
            }
        }

where GetPropertyDisplayName is a solution suggested by a fellow member in his answer for another question for retrieving the DisplayName of a property: https://stackoverflow.com/a/10048758

The end result that I'm looking for is a list of strings (display names) that will be formed only by the properties that are true.

Thank you, in advance, for your help on this.

1

There are 1 answers

2
Mikaal Anwar On BEST ANSWER

I think you are using the wrong attribute. I just took a snippet from https://stackoverflow.com/a/5015878/6866739 and replaced "DisplayNameAttribute" with "DisplayAttribute" and I got working results.

The sample code you referred to has its properties like:

public class Class1
{
    [DisplayName("Something To Name")]
    public virtual string Name { get; set; }

Yours are like:

public class AccessoriesModel
{
    public int Id { get; set; }

    [Display(Name = "Acc 1")]
    public bool Accessory1 { get; set; }

So that difference in the usage of attributes, might've been the reason why it wasn't working for you. Rest, you can find the working code below:

foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
{
    if (propertyInfo.PropertyType == typeof(bool))
    {
        bool value = (bool)propertyInfo.GetValue(data, null);
        if (value)
        {
            var attribute = propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true)
                                .Cast<DisplayAttribute>().Single();
            string displayName = attribute.Name;
            list.Add(displayName);
        }
    }
}

I reused the extension methods from this answer https://stackoverflow.com/a/5015911/6866739