2 回答

TA貢獻1998條經驗 獲得超6個贊
您似乎想要對可以從GetInterfaceMap獲得的信息進行反向查找。構建它非常簡單,
public static Dictionary<MethodInfo, List<(Type, MethodInfo)>> GetReverseInterfaceMap(Type t)
{
var reverseMap = new Dictionary<MethodInfo, List<(Type, MethodInfo)>>();
var maps = t.GetInterfaces().ToDictionary(i => i, i => t.GetInterfaceMap(i));
foreach (var m in t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
var list = new List<(Type, MethodInfo)>();
foreach (var (i, map) in maps)
{
for (int index = 0; index < map.TargetMethods.Length; index++)
{
var targetMethod = map.TargetMethods[index];
if (targetMethod == m)
{
list.Add((map.InterfaceType, map.InterfaceMethods[index]));
break;
}
}
}
reverseMap[m] = list;
}
return reverseMap;
}
關于界面的工作方式,有幾點要說明,因為畫面并不像“派生”->“基礎”->“界面”那樣簡單。有一個映射描述了每個接口方法調用的具體方法。該信息很容易獲得,因為運行時需要它來實現接口調度。然而,相反的過程并不那么簡單。一種具體方法可能是多個接口的映射。接口方法也可能被派生類重新映射。這一切都需要考慮在內。
- 2 回答
- 0 關注
- 199 瀏覽
添加回答
舉報