使用C#反射对象不匹配目标类型 [英] Object does not match target type using C# Reflection
本文介绍了使用C#反射对象不匹配目标类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想如下得到窗口值
这指的是主窗口(窗口1)
this refers to the main window (window1)
Type type = this.GetType();
PropertyInfo pi = type.GetProperty("Left");
object obj = pi.GetValue(type, null);
不过,我得到一个错误使用对象不匹配目标类型。什么是错的?
But I get an "Object does not match target type using" error. What is wrong?
推荐答案
由于你正在试图获得一个类型的'左'的财产,不是你的实例。
Because you are trying to get the 'Left' property of a Type, not your instance.
试试这个
object obj = pi.GetValue(this, null);
这篇关于使用C#反射对象不匹配目标类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文