如何获取接口方法的MethodInfo,实现类方法的MethodInfo?

发布于 2024-07-27 04:19:21 字数 291 浏览 4 评论 0原文

我有接口方法的MethodInfo和实现接口Type嗯>。 我想找到实现接口方法的类方法的MethodInfo

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

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

I have a MethodInfo of an interface method and Type of a class that implements the interface.
I want to find the MethodInfo of the class method that implements the interface method.

The simple method.GetBaseDefinition() does not work with interface methods.
Lookup by name won't work either, because when implementing interface method explicitly it can have any name (yes, not in C#).

So what is the correct way of doing that that covers all the possibilities?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-08-03 04:19:21

好的,我找到了一种方法,使用 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];

OK, I found a way, using 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];
与往事干杯 2024-08-03 04:19:21

这是一个扩展方法!

public static MethodInfo GetImplementedMethod(this Type targetType, MethodInfo interfaceMethod)
{
    if (targetType is null) throw new ArgumentNullException(nameof(targetType));
    if (interfaceMethod is null) throw new ArgumentNullException(nameof(interfaceMethod));

    var map = targetType.GetInterfaceMap(interfaceMethod.DeclaringType);
    var index = Array.IndexOf(map.InterfaceMethods, interfaceMethod);
    if (index < 0) return null;

    return map.TargetMethods[index];
    
}

Here's an extension method!

public static MethodInfo GetImplementedMethod(this Type targetType, MethodInfo interfaceMethod)
{
    if (targetType is null) throw new ArgumentNullException(nameof(targetType));
    if (interfaceMethod is null) throw new ArgumentNullException(nameof(interfaceMethod));

    var map = targetType.GetInterfaceMap(interfaceMethod.DeclaringType);
    var index = Array.IndexOf(map.InterfaceMethods, interfaceMethod);
    if (index < 0) return null;

    return map.TargetMethods[index];
    
}
谁许谁一生繁华 2024-08-03 04:19:21

我用这个。

var interfacemethodParameterTypes = interfaceMethodInfo.GetParameters().Select(p => p.ParameterType).ToArray();

var map = targetType.GetInterfaceMap(interfaceMethodInfo.DeclaringType);

return map.TargetType.GetMethod(interfaceMethodInfo.Name, interfacemethodParameterTypes);

I use this.

var interfacemethodParameterTypes = interfaceMethodInfo.GetParameters().Select(p => p.ParameterType).ToArray();

var map = targetType.GetInterfaceMap(interfaceMethodInfo.DeclaringType);

return map.TargetType.GetMethod(interfaceMethodInfo.Name, interfacemethodParameterTypes);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文