Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will PropertyInfo from Expression be equal to PropertyInfo from GetProperties()

I know that typeof(T) == typeof(T) will always be true because Type objects are basically static and only one Type instance exists per class type (if this is wrong, please correct me...I have several programs that function on this assumption).

I am having a hard time finding in the documentation whether or not the PropertyInfo objects exhibit the same property.

My application is this:

I have a Reflector<T> class which takes the result typeof(T).GetProperties(...) and stores the resulting PropertyInfo objects as keys in a dictionary.

Separately, I have an expression parser which attempts to parse an expression describing the access of a certain property of type Func<T, TResult> (e.g. t => t.SomeProperty with T being t.GetType() and TResult being the type of SomeProperty). After some computation, I eventually get a PropertyInfo object coming from a MemberExpression.Member from the passed Expression.

The resulting PropertyInfo is then used as the key in a Dictionary.TryGetValue call to get the additional data about the property stored in the Reflector<T>'s dictionary.

Question

Can I be assured that the PropertyInfo that comes from the MemberExpression.Member will be equal to (==) the PropertyInfo for the same property returned from Type.GetProperties or could there be two different PropertyInfo instances that in actuality refer to the same property on the same type?

For the purpose of discussion, it may be assumed that the property is non-virtual or not overridden if it is virtual.

like image 316
Los Frijoles Avatar asked Nov 01 '22 14:11

Los Frijoles


2 Answers

Based on the reference source code, it should be. The PropertyInfo instance is being obtained from the matching Type.

like image 65
galenus Avatar answered Nov 10 '22 00:11

galenus


Regardless of whether or not the property is virtual or overridden, the PropertyInfo on a derived type is always different than the one on the base type where it's defined.

When working with the MemberExpression.Member from a lambda of the form () => someInstance.SomeProperty, you may retrieve a PropertyInfo from a derived type. As I uncovered in my answer over here, it's different instance than the PropertyInfo you get from the base type itself.

like image 34
HappyNomad Avatar answered Nov 10 '22 00:11

HappyNomad