简体   繁体   English

无法从PropertyInfo C#获取属性

[英]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) 我现在正在尝试实例化一个MenuItem类型的对象,并尝试从Inherited类中获取属性(我无法直接实例化MenuItem ,因为该类型是从其他类传入的)

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 . 但这使我的sfinull 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);
        }
    }
}

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, ie string in your case. 这是因为PropertyType返回PropertyType的类型,即您的情况下为string And the type string does not have your custom attribute. 而且类型string没有您的自定义属性。

Also your edit of the FieldIDAttribute class is not correct. 另外,您对FieldIDAttribute类的编辑不正确。 It's constructor does not match the class name and it features an undeclared & incorrect ID type. 它的构造函数与类名不匹配,并且具有未声明和错误的ID类型。

In addition to what Alex said, you should specify binding flags, this worked for me: 除了Alex所说的以外,您还应该指定绑定标志,这对我有用:

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. 编辑:“类型”必须为Nav_Item,您没有将FieldIDAttribute应用于Menu_item类中的任何内容。 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. 为了获得IEnumerable的属性,您将必须对其进行枚举并读取列表中每种元素的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM