mardi 8 septembre 2015

How can I use reflection to retrieve all the fields and/or properties for a specific Type?

I need to use reflection to retrieve the values of a field or property, for a particular type.

I do not know if

  • it's a field
  • it's a property
  • either of the above are private or public.

I can't make assumptions so I was hoping to use reflection to solve this. I'm hoping that the developers will make these private fields ... but I cannot assume that.

How can I find all the fields/properties for a type Foo that has .. say.. int's ?

.NET v 4.0 or 4.5 please. Linq is also acceptable :)

I want something like this pseudo-code:

var property = source.GetType()
                     .GetProperties(BindingFlags.GetField | BindingFlags.NonPublic)
                     .Where(x => x.PropertyType == typeof (int))
                     .ToList();





Aucun commentaire:

Enregistrer un commentaire