How to use field.GetCustomAttribute()

BitSmithy 1,751 Reputation points
2020-05-19T14:20:19.317+00:00

Hello,

I am traing to get custom atribute from a property. I use such code. But I don't want how to use
function: Attribute a = property.GetCustomAttribute(DBFiledAtribute);
Please help. My code is below:

Type type = typeof(Entry);
// get all all public properties.
PropertyInfo[] properties = type.GetProperties();

foreach (var property in properties)
{

Attribute a = property.GetCustomAttribute(DBFiledAtribute);//How to use this, How to get atribute from property
//do something with a
}

public class Entry : INotifyPropertyChanged
{
[DBFiledAtribute(DbFieldAtribute = "Id")]
public int Id {get; set;}

    //rest of code
   }

public class DBFiledAtribute : Attribute
{
public string DbFieldAtribute { get; set; }//Id, TimeStamp
}

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Daniele 1,996 Reputation points
    2020-05-19T14:52:48.09+00:00

    Try with

    Type type = typeof(Entry);
    var properties = type.GetProperties();
    foreach (PropertyInfo property in properties)
    {
        Attribute a = property.GetCustomAttribute(typeof(DBFiledAtribute));
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful