相关疑难解决方法(0)

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

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.

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

c# string reflection properties

20
推荐指数
2
解决办法
7万
查看次数

标签 统计

c# ×1

properties ×1

reflection ×1

string ×1