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.
Based on the reference source code, it should be. The PropertyInfo instance is being obtained from the matching Type.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With