如何通过Reflection获取字符串属性的值?

public class Foo { public string Bar {get; set;} } 

如何通过reflection获得字符串属性Bar的值? 如果PropertyInfo类型是System.String,则以下代码将引发exception

 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 } 

看来我的问题是该属性是一个索引器类型,带有System.String。

另外,如何判断该属性是否为索引器?

您可以按名称获取酒店:

 Foo f = new Foo(); f.Bar = "Jon Skeet is god."; var barProperty = f.GetType().GetProperty("Bar"); string s = barProperty.GetValue(f,null) as string; 

关于后续问题:索引器将始终命名为Item并在getter上有参数。 所以

 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)*/}); } 

我无法重现这个问题。 你确定你没有尝试在具有索引器属性的某个对象上执行此操作吗? 在这种情况下,处理Item属性时将抛出您遇到的错误。 此外,你可以这样做:

 public static T GetPropertyValue(object o, string propertyName) { return (T)o.GetType().GetProperty(propertyName).GetValue(o, null); } ...somewhere else in your code... GetPropertyValue(f, "Bar"); 
 Foo f = new Foo(); f.Bar = "x"; string value = (string)f.GetType().GetProperty("Bar").GetValue(f, null); 
 var val = f.GetType().GetProperty("Bar").GetValue(f, null); 
 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 } 

这是为了后续:

 class Test { public class Foo { Dictionary data =new Dictionary(); 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; } } 
 PropertyInfo propInfo = f.GetType().GetProperty("Bar"); object[] obRetVal = new Object[0]; string bar = propInfo.GetValue(f,obRetVal) as string; 

通过使用Extension方法很容易获得任何对象的属性值,如:

 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); } } 

用法是:

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

带有object和null的getvalue对我来说很有用。 谢谢你的post。

上下文:在新员工的MVC模型中循环遍历所有属性并确定其表单发布值:

newHire =>模型,具有许多属性,其发布的表单值我想单独写入一组数据库记录

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