vendredi 15 mars 2019

Determine if two different PropertyInfo come from the same interface?

I've been using

public static bool IsSameAsProperty(PropertyInfo first, PropertyInfo second) =>
     first.DeclaringType == second.DeclaringType && first.Name == second.Name;

To determine whether a reflected property info matches some property I grabbed off a base class.

This approach has begun falling apart when I try to reference properties defined in interfaces.

For example, imagine the following multiple-interface inheritance scenario:

interface IAnimal : { bool IsHungry { get; } }
interface IDog : IAnimal { }

abstract class Animal : IAnimal { public bool IsHungry { get; set; } }
class Dog : Animal, IDog { }

If I'm creating property expressions, all of the following are valid:

Expression<Func<object, bool>> propertyExpression;
propertyExpression = (IAnimal animal) => animal.IsHungry
propertyExpression = (Animal animal) => animal.IsHungry
propertyExpression = (IDog dog) => dog.IsHungry
propertyExpression = (Dog dog) => dog.IsHungry

Since each of these types inherit the property IsHungry, all these expressions are valid.

My problem is that I want some way of programatically detecting that all of these properties "come from" the shared interface IAnimal and are compatible. Unfortunately, in the case of IDog.IsHungry, DeclaringType == typeof(IAnimal) whereas Dog.IsHungry has DeclaringType == typeof(Animal).

I can't think of an easy way to compare interface and concrete type property expressions without either resorting to a simple Name comparison (which is prone to false-positives) - but I can't think of anything that doesn't involve enumerating all the interfaces inherited by the two types and looking for anything with that property name that's in both sets.


Q: Can we create a function that returns true when comparing any of the PropertyInfo yielded from the above 4 property expressions. (e.g. Detect that they all represent/implement the same base interface property?)





Aucun commentaire:

Enregistrer un commentaire