讓我們來看看。據我所知,沒有BCL的方式只有獲得實際上實現的特定類型的接口,但排除任何類型繼承的接口。因此,我們將不得不推出自己的:
public static Dictionary<Type, IEnumerable<Type>> GetInterfaceHierarchyMap(this Type type)
{
List<Type> typeAncestry = new List<Type>();
Type ancestor = type;
while(ancestor != null)
{
typeAncestry.Add(ancestor);
ancestor = ancestor.BaseType;
}
Dictionary<Type, IEnumerable<Type>> interfaceMaps = new Dictionary<Type, IEnumerable<Type>>();
foreach(Type childType in typeAncestry.Reverse<Type>())
{
var mappedInterfaces = interfaceMaps.SelectMany(kvp => kvp.Value);
var allInterfacesToPoint = childType.GetInterfaces();
interfaceMaps.Add(childType, allInterfacesToPoint.Except(mappedInterfaces));
}
return interfaceMaps;
}
一步一個時間:
- 我們從當前的類型開始,走了BaseType,直到我們達到根類型。
- 我們顛倒了這個列表,所以當我們迭代它時,我們首先從根類型開始。
- 對於鏈中的每種類型,我們獲得應用於該類型的所有接口並從祖先類型繼承,然後我們使用Except來消除之前迭代中已發現的所有接口。
這會將重複的接口要求視爲多餘 - 即,如果其中一個祖先類型實現了IDisposable,並且您的類型也如此,則只會計算最早的實現。
這種方法的一個假想的結果將是一本字典,看起來像:
[object] - []
[BaseBaseClass] - [ISomeInterface]
[BaseClass] - [IAnotherInterface, IOneMore]
[ConcreteClass] - [IYetAnother]
你是什麼意思?你能舉一個具體的例子嗎? – 2010-11-05 01:06:30
@Sam:看到下面的答案 – 2010-11-05 01:26:21