mercredi 30 mai 2018

What is the best way to check if two properties are the same

I have to check if two PropertyInfo variables refer to the same property. See example from below: enter image description here I know, that these are the same, because:

  • The one on the right is returned by type.GetProperties()
  • The one on the left is returned by such an expression: (PropertyInfo)((MemberExpression)e.Body).Member, where e is property access expression
  • I can always be sure that both sides come from the same type.

But equality check returns false, as they are the result of different reflection operations.

I know I could rely only on Name in my actual case, but because I want to have a generic approach, I would probably need to check at least DeclaringType also. Would this be enough?

From performance point of view my best guess is comparing MetadataToken property, but from what I could understand from this, I am not sure if scopes could or could not interfere in such a situation.

Thank you for sharing your ideas.





Aucun commentaire:

Enregistrer un commentaire