Value of a property through reflection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, i need help please.. any help would be greatly appreciated. I need to
know how i can get the value of a property throught reflection? please
please.. thanks in advance...
 
object value = obj.GetType().GetProperty("PropertyName").GetValue(obj,
null);

(where obj is your original object, and "PropertyName" is the name of the
public instance property).

Marc
 
The null passed at the end are binding flags which could enable you to read
private/internal properties too.

Ciaran O'Donnell
 
The null passed at the end are binding flags which could enable you to read
private/internal properties too.

Not quite. The null in the call to GetValue above is the "index" value
in case you're dealing with what in C# terms is an indexer instead of a
property (they're both properties in .NET terminology). The overload
which takes a BindingFlags is a different one (and far less commonly
used, IME) - you'd normally specify the BindingFlags in the call to
GetProperty to get at a private/internal property.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top