c# - 如何获取接口(interface)方法的MethodInfo,实现了类方法的MethodInfo?

标签 c# .net reflection methodinfo

我有一个接口(interface)方法的MethodInfo和一个实现Type界面。 我想找到实现接口(interface)方法的类方法的MethodInfo

简单的 method.GetBaseDefinition() 不适用于接口(interface)方法。 按名称查找也不起作用,因为当显式实现接口(interface)方法时,它可以有任何名称(是的,不是在 C# 中)。

那么什么是正确的方法来覆盖所有的可能性?

最佳答案

好的,我找到了一个方法,使用GetInterfaceMap .

var map = targetType.GetInterfaceMap(interfaceMethod.DeclaringType);
var index = Array.IndexOf(map.InterfaceMethods, interfaceMethod);

if (index == -1)
{
    //this should literally be impossible
}

return map.TargetMethods[index];

关于c# - 如何获取接口(interface)方法的MethodInfo,实现了类方法的MethodInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1113635/

相关文章:

c# - 猜4位数字游戏收集正确数字的数量

c# - Uri 构造函数在没有 UriKind 的情况下引发相对路径异常

c# - 方法 'ImageAnnotatorClient.Create' 没有重载需要 1 个参数

java - Nest 聚合搜索请求

c# - 失去焦点后如何保持 NumericUpDown 的无效值?

c# - 将生成 SelectList 的两个方法重构为一个方法

java - Java中如何判断一个字段是否为char[]类型(char数组)?

c# - "FormatException was unhandled by user code"输入字符串的格式不正确

c# - 从 app.config 读取正则表达式和字符串

grails - Groovy 闭包 : How does the definition of Grails url mappings work