反射:将PropertyInfo转换为List <obj>

ebb*_*ebb 1 c# reflection

正如标题所说,然后我试图将其PropertyInfo转换为"原始"类型,这List<obj>在我的情况下.

我没试过下面的代码:

(List<obj>)pInfo.GetValue(pInfo, null)

(List<obj>)pInfo.GetValue(typeof<obj>, null)

它简单地抛出了一个例外:

TargetException未处理:Object与目标类型不匹配.

我确信我忽略了一些非常简单的东西,但我无法弄清楚是什么.

Mar*_*ell 9

第一个参数是目标对象:

var list = (List<object>)prop.GetValue(obj,null);
Run Code Online (Sandbox Code Playgroud)

但就个人而言,我可能想在这里使用非通用API; 泛型和反射很少混合:

var list = (IList)prop.GetValue(obj,null);
Run Code Online (Sandbox Code Playgroud)