Tiago
Tiago

Reputation: 2167

Get Custom Attributes Generic

I am creating a custom attribute. And I will use it in multiple classes:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public sealed class Display : System.Attribute
{
    public string Name { get; set; }

    public string Internal { get; set; }

}

public class Class1
{
    [Display(Name = "ID")]
    public int ID { get; set; }

    [Display(Name = "Name")]
    public string Title { get; set; }
}

public class Class2
{
    [Display(Name = "ID")]
    public int ID { get; set; }

    [Display(Name = "Name")]
    public string Title { get; set; }
}

Here is working correctly but I want to make it as generic as possible, like the MVC example:

Class1 class1 = new Class1();
class1.Title.DisplayName () / / returns the value "Name"

The only thing I could do was generate a loop of my property, but I need to inform my typeof Class1

foreach (var prop in typeof(Class1).GetProperties())
{
    var attrs = (Display[])prop.GetCustomAttributes(typeof(Display), false);
    foreach (var attr in attrs)
    {
        Console.WriteLine("{0}: {1}", prop.Name, attr.Name);
    }
}

Is there any way to do it the way I want?

Upvotes: 2

Views: 6802

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40818

You can't do what you've shown exactly because class1.Title is an expression that evaluates to a string. You are looking for metadata about the Title property. You can use Expression Trees to let you write something close. Here's a short helper class that pulls the attribute value out of a property in an expression tree:

public static class PropertyHelper
{
    public static string GetDisplayName<T>(Expression<Func<T, object>> propertyExpression)
    {
        Expression expression;
        if (propertyExpression.Body.NodeType == ExpressionType.Convert)
        {
            expression = ((UnaryExpression)propertyExpression.Body).Operand;
        }
        else
        {
            expression = propertyExpression.Body;
        }

        if (expression.NodeType != ExpressionType.MemberAccess)
        {
            throw new ArgumentException("Must be a property expression.", "propertyExpression");
        }

        var me = (MemberExpression)expression;
        var member = me.Member;
        var att = member.GetCustomAttributes(typeof(DisplayAttribute), false).OfType<DisplayAttribute>().FirstOrDefault();
        if (att != null)
        {
            return att.Name;
        }
        else
        {
            // No attribute found, just use the actual name.
            return member.Name;
        }
    }

    public static string GetDisplayName<T>(this T target, Expression<Func<T, object>> propertyExpression)
    {
        return GetDisplayName<T>(propertyExpression);
    }
}

And here's some sample usage. Note that you really don't even need an instance to get this metadata, but I have included an extension method that may be convenient.

public static void Main(string[] args)
{
    Class1 class1 = new Class1();
    Console.WriteLine(class1.GetDisplayName(c => c.Title));
    Console.WriteLine(PropertyHelper.GetDisplayName<Class1>(c => c.Title));
}

Upvotes: 5

Related Questions