astef
astef

Reputation: 9478

What is System.Reflection.RuntimePropertyInfo and how do I compare it?

I was suprised to see that the actual type of the object I get with x.GetType().GetProperty("Foo") is not System.Reflection.PropertyInfo but System.Reflection.RuntimePropertyInfo.

I don't see this type documentation in msdn or elsewhere.

My actual problem grows from reference-comparing two properties. I receive a property from third-party lib and compare it with a property which I get with .GetProperty("Foo") on the same type. I expected properties to be the same object (and they looks like the same property in "Locals" window while debugging), but they are not (GetHashCode result is different). So, I think it can somehow be related with the actual type of the property object which is System.Reflection.RuntimePropertyInfo.

What is System.Reflection.RuntimePropertyInfo? How to compare it? Does it behaves the same as usual PropertyInfo?

Upvotes: 8

Views: 6310

Answers (2)

Nitin Purohit
Nitin Purohit

Reputation: 18580

PropertyInfo is an abstract class while RuntimePropertyInfo is the concrete implementation of PropertyInfo.

When we call Type.GetProperties() or Type.GetProperty() they actually returns the RuntimePropertyInfo.

The reason you are getting reference not equal could be because of the Type signature difference in the third-party lib.

Upvotes: 3

Rob
Rob

Reputation: 27357

RuntimePropertyInfo is an internal implementation. It is a PropertyInfo, and in fact, GetProperty returns PropertyInfo (even if the underlying type is RuntimePropertyInfo).

The third-party lib is probably getting the property of a different type than you are?

new blah().GetType().GetProperty("Test") == new blah().GetType().GetProperty("Test")

Returns true.

Upvotes: 12

Related Questions