Вопрос

I have a simple POCO class, e.g.

class C {
  [MyAtrib]
  public int i {get; set;}
  [MyAtrib]  
  public int i2;
}

When I call:

GetType().GetFields(
  BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

on that class (instance) I can't get the FieldInfo for those members that have automatically generated getters/setters (i.e. int i above).

Actually, I'm trying to read those custom attributes (MyAtrib) and can't do it for those properties that have {get; set;}.

Why is that? I'd expect to get both i and it's (private) backing field, since i is public.

Can I get to i's MyAtrib somehow through reflection?

Это было полезно?

Решение

You get fields right now, but public int i {get; set;} is a property. You need to get properties:

// note: properties -> generally public
GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top