Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object does not match target type using C# Reflection

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

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?

like image 269
user101375 Avatar asked Feb 11 '10 13:02

user101375


2 Answers

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

try this

object obj = pi.GetValue(this, null);
like image 179
Sky Sanders Avatar answered Nov 04 '22 21:11

Sky Sanders


use this code

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

like image 32
ali Avatar answered Nov 04 '22 21:11

ali