对象与使用C#Reflection的目标类型不匹配

时间:2010-02-11 13:12:30

标签: c# .net reflection

我正在尝试获取Window的值,如下所示

指的是主窗口(window1)

Type type = this.GetType();
PropertyInfo pi = type.GetProperty("Left");
object obj = pi.GetValue(type, null);

但我得到一个“对象与目标类型不匹配”错误。有什么问题?

2 个答案:

答案 0 :(得分:50)

因为您试图获取Type的'Left'属性,而不是您的实例。

试试这个

object obj = pi.GetValue(this, null);

答案 1 :(得分:0)

使用此代码

object obj = property.GetValue(currentObject, null);