public class Foo
{
public string Bar {get; set;}
}
How do I get the value of Bar, a string property, via reflection? The following code will thr
You can just get the property by name:
Foo f = new Foo();
f.Bar = "Jon Skeet is god.";
var barProperty = f.GetType().GetProperty("Bar");
string s = barProperty.GetValue(f,null) as string;
Regarding the follow up question: Indexers will always be named Item and have arguments on the getter. So
Foo f = new Foo();
f.Bar = "Jon Skeet is god.";
var barProperty = f.GetType().GetProperty("Item");
if (barProperty.GetGetMethod().GetParameters().Length>0)
{
object value = barProperty.GetValue(f,new []{1/* indexer value(s)*/});
}