26
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 throw an exception if the PropertyInfo type is a System.String

Foo f = new Foo(); f.Bar = "Jon Skeet is god."; foreach(var property in f.GetType().GetProperties()) { object o = property.GetValue(f,null); //throws exception TargetParameterCountException for String type } 

It seems that my problem is that the property is an indexer type, with a System.String.

Also, how do I tell if the property is an indexer?

4
  • 2
    works fine here... is something else going on? Commented Jun 12, 2009 at 17:41
  • seems like you're not posting enough contextual code? Commented Jun 12, 2009 at 17:42
  • Yeah. The Debugger says the underlying type is string, but I suspect there is something else going on. Commented Jun 12, 2009 at 17:45
  • possible duplicate of Get property value from string using reflection in C# Commented Apr 27, 2013 at 15:31

9 Answers 9

53

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)*/}); } 
Sign up to request clarification or add additional context in comments.

3 Comments

That's inexact. Item is the default name for indexers, but anyone can use a IndererNameAttribute on the indexer property to change that. You have to look for the DefaultMemberAttribute on the type to get the actual indexer name.
I was completely unaware of the IndexerName attribute! Thank you. You can find more information here: bartdesmet.net/blogs/bart/archive/2006/09/09/4408.aspx Thanks Jb Evain.
If I only have a string with full class name, and property name like MyNameSpace1.X.Y.Z.ClassName, and PropertyName ?
5

I couldn't reproduce the issue. Are you sure you're not trying to do this on some object with indexer properties? In that case the error you're experiencing would be thrown while processing the Item property. Also, you could do this:

 public static T GetPropertyValue<T>(object o, string propertyName) { return (T)o.GetType().GetProperty(propertyName).GetValue(o, null); } ...somewhere else in your code... GetPropertyValue<string>(f, "Bar"); 

Comments

4
Foo f = new Foo(); f.Bar = "x"; string value = (string)f.GetType().GetProperty("Bar").GetValue(f, null); 

Comments

4
var val = f.GetType().GetProperty("Bar").GetValue(f, null); 

Comments

4
Foo f = new Foo(); f.Bar = "Jon Skeet is god."; foreach(var property in f.GetType().GetProperties()) { if(property.Name != "Bar") { continue; } object o = property.GetValue(f,null); //throws exception TargetParameterCountException for String type } 

And here is for the followup:

class Test { public class Foo { Dictionary<string, int> data =new Dictionary<string,int>(); public int this[string index] { get { return data[index]; } set { data[index] = value; } } public Foo() { data["a"] = 1; data["b"] = 2; } } public Test() { var foo = new Foo(); var property = foo.GetType().GetProperty("Item"); var value = (int)property.GetValue(foo, new object[] { "a" }); int i = 0; } } 

1 Comment

Based on the other comments, you will need to change the null on get value to the proper indexer value(s).
2
PropertyInfo propInfo = f.GetType().GetProperty("Bar"); object[] obRetVal = new Object[0]; string bar = propInfo.GetValue(f,obRetVal) as string; 

1 Comment

what exactly is the downvote for? this code works as expected.
2

Its easy to get property value of any object by use Extension method like:

public static class Helper { public static object GetPropertyValue(this object T, string PropName) { return T.GetType().GetProperty(PropName) == null ? null : T.GetType().GetProperty(PropName).GetValue(T, null); } } 

Usage is:

Foo f = new Foo(); f.Bar = "x"; var balbal = f.GetPropertyValue("Bar"); 

1 Comment

Thanks to Elvis this can be amended to: return T.GetType().GetProperty(PropName)?.GetValue(T, null);. blogs.msdn.microsoft.com/jerrynixon/2014/02/26/…
1

the getvalue with object and null worked great for me. Thanks for the posts.

Context: Looping through all properties in an MVC model for New Hires and determining their form posted values:

newHire => the Model, with many properties, whose posted form values I want to write individually to a set of database records

foreach(var propertyValue in newHire.GetProperties()) { string propName = propertyValue.Name; string postedValue = newHire.GetType().GetProperty(propName).GetValue(newHire, null).ToString(); } 

Comments

0

To get the property names of the object by just passing the object you can use this function may this works.

just make object object a class and pass into it.

 public void getObjectNamesAndValue(object obj) { Type type = obj.GetType(); BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; PropertyInfo[] prop = type.GetProperties(flags); foreach (var pro in prop) { System.Windows.Forms.MessageBox.Show("Name :" + pro.Name + " Value : "+ pro.GetValue(obj, null).ToString()); } } 

But this will only work when the object properties are "Public"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.