Can I hide property attributes?

I am trying to use reflection to determine which type properties have a specific attribute. This seems like normal when I create my own attribute myself, but it currently does not work for the attribute in a third-party assembly.

This assembly is SolrNet, and the attribute is of type SolrField .

An example class using:

 public class PublicDocument : SearchItem { [SolrField("case")] public string CaseNumber { get; set; } [SolrField("case_name")] public string CaseName { get; set; } } 

Here is my code to get these attributes. Curiously, the .Attributes property is empty! EDIT . After viewing again, this property will be empty even with other custom types and, apparently, is reserved for infrastructure 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, the same code works fine with the attribute defined in one of my own assemblies and is used in one template. So, my question here is, can Attributes be marked so that they do not appear as a result of this reflection, or is there another problem here?

+4
source share
1 answer

It’s hard to say for sure. My first assumption was that instead of typeof(SolrField) you can use typeof(SolrFieldAttribute) .

+6
source

Source: https://habr.com/ru/post/1369049/


All Articles