开发者

Can Attributes on a Property be Hidden?

I am attempting to use reflection to determine which Properties of a Type have a certain Attribute. This seems to work fine when I create a custom attribute myself, but is currently not working for an attribute in a 3rd party assembly.

The assembly in question is SolrNet, and the Attribute is of Type SolrField.

Example class with usage:

public class PublicDocument : SearchItem {

    [SolrField("case")]
    public string CaseNumber { get; set; }

    [SolrField("case_name")]
    public string CaseName { get; set; }
}

Here is my code for getting at these Attributes. The curious thing is that the property.Attributes is empty! EDIT: After looking again, this property will be empty even with other custom typ开发者_开发技巧es and seems to be reserved for framework Attributes.

        PublicDocument item = new PublicDocument();
        foreach (PropertyInfo property in item.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {

            foreach (object attribute in property.GetCustomAttributes(typeof(SolrField), true)) {
                //do some stuff here
            }
        }

SolrField is defined here: https://github.com/mausch/SolrNet/blob/master/SolrNet/Attributes/SolrFieldAttribute.cs

As I said, this same code works fine with an attribute defined in one of my own assemblies and used in the same pattern. So my question here is, can Attributes be marked to not show up through reflection like this, or is there another issue here?


Hard to tell for sure. My first guess would be that you might use typeof(SolrFieldAttribute) instead of typeof(SolrField).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜