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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2244607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:43:04  来源:igfitidea点击:

Object does not match target type using C# Reflection

c#.netreflection

提问by user101375

I am trying to get a value of a Window as follows

我正在尝试按如下方式获取 Window 的值

thisrefers to the main window (window1)

是指主窗口(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?

但是我收到“对象与使用的目标类型不匹配”错误。怎么了?

采纳答案by Sky Sanders

Because you are trying to get the 'Left' property of a Type, not your instance.

因为您正在尝试获取类型的“左”属性,而不是您的实例。

try this

尝试这个

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

回答by ali

use this code

使用此代码

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

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