gdoron
gdoron

Reputation: 150303

Why PropertyInfo.GetValue() need the instance to retrieve the value?

The PropertyInfo class has the GetValue method which takes two parameters, the first one is the instance of the property.
Well it's just doesn't make sense to me, why the ProperyInfo doesn't hold the value? I get the PropertyInfo from the instance and then I need the Instance again to retrieve the value?!
That's weird.

Can someone please explain me why C# built that way?

Upvotes: 1

Views: 279

Answers (3)

Bas
Bas

Reputation: 27105

The PropertyInfo class is designed on a per-type basis. The PropertyInfo resembles the property on the class, and not on the instance. That's why you need the instance to retrieve the value.

Upvotes: 2

masterchris_99
masterchris_99

Reputation: 2733

the propertyinfo generates from the instance only the construction plan and not the values itself

Upvotes: 0

Ani
Ani

Reputation: 113462

A PropertyInfo is "metadata"; it is associated with a property defined on a type, not an instance.

Upvotes: 6

Related Questions