public class Foo
{
public string Bar {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
如何通过反射获得字符串属性Bar的值?如果PropertyInfo类型是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
}
Run Code Online (Sandbox Code Playgroud)
看来我的问题是该属性是一个索引器类型,带有System.String.
另外,如何判断该属性是否为索引器?