I'm trying to get the most specific interface that both types implement. Getting the list of interfaces is fairly easy:
var interfaces = leftType.GetInterfaces().Intersect(rightType.GetInterfaces());
Then, I traverse the list and remove interfaces that are "included" into other interfaces:
var remaining = interfaces.ToDictionary(i => i, i => true);
foreach(var iface in interfaces)
foreach(var subIface in iface.GetInterfaces())
remaining.Remove(subIface);
However, when trying to get the most common interface for int[]
and List<int>
, I get a list of three interfaces:
IList
(non-generic)IList<int>
IReadOnlyList<int>
First of all, why doesn't IList<T>
implement non-generic IList
, while IEnumerable<T>
does actually implement IEnumerable
?
Then, the IReadOnlyList
causes a certain confusion. The name implies that the container that implements the interface prohibits modification, but the usage, on the contrary, implies that the container allows read access and doesn't care about modifications. But if so, why doesn't IList<T>
implement IReadOnlyList<T>
?
Maybe the whole approach is incorrect, and there's a more clever way to detect the most specific interface?