user101375
user101375

Reputation: 7079

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?

Upvotes: 35

Views: 40236

Answers (2)

ali
ali

Reputation: 195

use this code

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

Upvotes: 1

Sky Sanders
Sky Sanders

Reputation: 37104

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

try this

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

Upvotes: 56

Related Questions