3

So I'm working on a project in C# to get the property name by attribute and its value. I have a collection:

ObservableCollection<Entity> collection = new ObsevableCollection<Entity>();
collection.Add(new Entity { Id = 5, Description = "Pizza" });
collection.Add(new Entity { Id = 2, Description = "Coca cola" });
collection.Add(new Entity { Id = 1, Description = "Broccoli" });

and the entity consists of:

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

    [MyAttribute]
    public string Description { get; set; }

    // other properties
}

My question is: is it possible to get the specific property within the entity that has the attribute MyAttribute and also its value. This would all be from the objects from collection.

user3357962
  • 95
  • 3
  • 11

3 Answers3

6

Use GetCustomAttributes to find the attributes, and LINQ to filter and get an anonymous object which holds the Property and the Attribute.

Use PropertyInfo.GetValue to read the actual value.

But please beware that the reflection calls are pretty expensive:

var propertiesWithAttribute = typeof(Entity).GetProperties()
    // use projection to get properties with their attributes - 
    .Select(pi => new { Property = pi, Attribute = pi.GetCustomAttributes(typeof(MyAttribute), true).FirstOrDefault() as MyAttribute})
    // filter only properties with attributes
    .Where(x => x.Attribute != null)
    .ToList();

foreach (Entity entity in collection)
{
    foreach (var pa in propertiesWithAttribute)
    {
        object value = pa.Property.GetValue(entity, null);
        Console.WriteLine($"PropertyName: {pa.Property.Name}, PropertyValue: {value}, AttributeName: {pa.Attribute.GetType().Name}");
    }
}
LolKiller
  • 28
  • 1
  • 4
Hannes
  • 426
  • 3
  • 12
  • Hey thanks a lot again. Since you already answered my question, i was wondering if you could help me a bit more hehe. Is there a way to find a second property with another attribute in the same entity.? I would add an attribute above the Id property. I have already tryed putting another parameter in Select statement after the Attribute, just changed the name, but I don't know how to call it, since the call is pa.Property.GetValue(entity, null); – user3357962 Mar 25 '14 at 20:53
  • You're welcome! Well, the pi.GetCustomAttributes call takes an filter for the type of the attribute you want to get (that is the first parameter 'typeof(MyAttribute)'. Just omit this param and it will return all Attributes. Later, in the Where function you may apply a filtering on the attributes types to get only the attributes you want to see. The rest could stay the same. – Hannes Mar 26 '14 at 09:12
  • Oh so I change to Attribute = pi.GetCustomAttributes(true).FirstOrDefault(), but how do i filter in the Where function? – user3357962 Mar 26 '14 at 10:42
  • .Where(x => x.Attribute != null && (x.Attribute is MyAttribute || x.Attribute is MyOtherAttribute)) Or You just leave it, and take all attributes You get. – Hannes Mar 26 '14 at 10:46
0

your question is: is it possible to get the specific property within the entity that has the attribute MyAttribute and also its value.

Answer : of course possible

then next question would be how !!

short answer : use reflections

long answer in the following link

reflections-to-get-attribute-value

pay attention to the following excerpt :

// Using reflection.


 System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // Reflection. 
// Displaying output. 
foreach (System.Attribute attr in attrs)
{
    if (attr is Author)
    {
        Author a = (Author)attr;
        System.Console.WriteLine("   {0}, version {1:f}", a.GetName(), a.version);
    }
}
stackunderflow
  • 3,811
  • 5
  • 31
  • 43
0

I have custome DisplayAttribute but, dose not have input parameter and I want to find which property used this attribute. In my entity all properties have this attribute, but by which way I can get property of each call?

I want to return correct disolay text by searching property name in database because of that, I don't want to specify input parameter for attribute. If property name did not exist in database, it should be add to database otherwise previous data will return.

Thnaks

Hadi Mazareei
  • 31
  • 1
  • 7