Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 775
astef Avatar asked Apr 22 '16 16:04

astef


2 Answers

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.

like image 107
Rob Avatar answered Sep 17 '22 21:09

Rob


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.

like image 22
Nitin Avatar answered Sep 20 '22 21:09

Nitin