M.R.
M.R.

Reputation: 4837

Cannot get attribute from PropertyInfo c#

I have a class and interface set up like this:

public partial interface INav_Item 
{
        [FieldID("df918977-369c-4a06-ac38-adb8741b5f75")]
        string Title  {get; set;}
}


public partial class Nav_Item  : INav_Item 
{
        [FieldID("df918977-369c-4a06-ac38-adb8741b5f75")]
        public virtual string Title  {get; set;}
}

And then I have this class inherited:

public class MenuItem : Nav_Item
{
        public virtual IEnumerable<MenuItem> Children { get; set; }

        //some other properties
}

I'm now trying to instantiate an object of type MenuItem, and trying to get the attributes from the Inherited class (I can't instantiate MenuItem directly, because the type is being passed in from other classes)

object obj = Activator.CreateInstance(type);

foreach (PropertyInfo propInfo in type.GetProperties())
{
            FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType.GetCustomAttribute(typeof(FieldAttribute));

}

But this is giving me sfi to be null. I've also debugged to try getting all the attributes:

propInfo.PropertyType.GetCustomAttributes()

.. but this is only giving me the system attributes (type and something else), but my own attributes are just not there? Is this is because the class is inherited? How can I get the attribute value?

EDIT:

The attribute class is defined as this:

public class FieldIDAttribute : Attribute
{
    private string _id;

    public FieldAttribute(string Id)
    {
        _id = Id;
    }

    public ID TheFieldID
    {
        get
        {
            return new ID(_id);
        }
    }
}

Upvotes: 0

Views: 1481

Answers (2)

Ron Beyer
Ron Beyer

Reputation: 11273

In addition to what Alex said, you should specify binding flags, this worked for me:

foreach (PropertyInfo propInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
            FieldIDAttribute sfi = propInfo.PropertyType.GetCustomAttribute(typeof(FieldIDAttribute)) as FieldIDAttribute;

            //Check if null here, if not, it has the attribute.

}

Edit: The "type" has to be Nav_Item, you don't have the FieldIDAttribute applied to anything in the Menu_item class. In order to get the attributes of the IEnumerable, you would have to enumerate through it and read the properties on each type of element in the list.

Upvotes: 0

Alex
Alex

Reputation: 13234

No, it is not an inheritance problem. You need to change this:

FieldAttribute sfi =(FieldAttribute)propInfo.PropertyType
    .GetCustomAttribute(typeof(FieldAttribute));

to this:

var sfi = propInfo.GetCustomAttribute(typeof(FieldIDAttribute)) as FieldIDAttribute;

This is because PropertyType returns the type of the property, i.e. string in your case. And the type string does not have your custom attribute.

Also your edit of the FieldIDAttribute class is not correct. It's constructor does not match the class name and it features an undeclared & incorrect ID type.

Upvotes: 1

Related Questions