简体   繁体   English

如果我有继承类类型的MethodInfo,如何获取接口的MethodInfo?

[英]How to get MethodInfo of interface, if i have MethodInfo of inherited class type?

I have the MethodInfo of a method on a class type that is part of an interface definition that that class implements. 我有类方法的MethodInfo ,它是该类实现的接口定义的一部分。
How can I retrieve the matching MethodInfo object of the method on the interface type that the class implements ? 如何在类实现的接口类型上检索方法的匹配MethodInfo对象?

I think i found the best way to do this : 我想我发现了最好的方法:

var methodParameterTypes = classMethod.GetParameters().Select(p => p.ParameterType).ToArray();
MethodInfo interfaceMethodInfo = interfaceType.GetMethod(classMethod.Name, methodParameterTypes);

Looking up by name and parameters will fail for explicitly implemented interface methods. 对于显式实现的接口方法,按名称和参数查找将失败。 This code should handle that situation as well: 此代码也应该处理这种情况:

private static MethodInfo GetInterfaceMethod(Type implementingClass, Type implementedInterface, MethodInfo classMethod)
{
    var map = implementingClass.GetInterfaceMap(implementedInterface);
    var index = Array.IndexOf(map.TargetMethods, classMethod);
    return map.InterfaceMethods[index];
}

If you want to find the Method from the interface that the class implements, something like this should work 如果你想从类实现的接口中找到Method,那么这样的东西应该可行

MethodInfo interfaceMethod = typeof(MyClass).GetInterfaces()
                .Where(i => i.GetMethod("MethodName") != null)
                .Select(m => m.GetMethod("MethodName")).FirstOrDefault();

Or if you want to get the method from the interface that the class implements from the method info from the class, you can do. 或者,如果您想从类实现的接口获取方法,则可以从类中获取方法信息。

    MethodInfo classMethod = typeof(MyClass).GetMethod("MyMethod");

    MethodInfo interfaceMethod = classMethod.DeclaringType.GetInterfaces()
        .Where(i => i.GetMethod("MyMethod") != null)
        .Select(m => m.GetMethod("MyMethod")).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现了类方法的MethodInfo,如何获取接口方法的MethodInfo? - How to get MethodInfo of interface method, having implementing MethodInfo of class method? 如何在 ThreadContext 类中获取 MethodInfo? - How to get a MethodInfo in the ThreadContext class? 如何获取泛型方法的MethodInfo? - How to get MethodInfo of a generic method? 当类使用泛型和泛型类型参数时,如何获取正确的MethodInfo对象 - How to get the correct MethodInfo object when a class uses generics and generic type parameters 如何使用方法名称获取非静态MethodInfo(不在字符串中,不在类类型中搜索) - How to get non-static MethodInfo using method name (not in string, not searching in class type) 如何从MethodInfo MakeGenericMethod的字符串中获取类类型 - How to take class type from string for MethodInfo MakeGenericMethod 如何获取类的函数的MethodInfo,而不进行字符串比较 - How to get the MethodInfo of a function of a class, without string comparison 如何检查MethodInfo是否是特定的接口方法 - How to check if a MethodInfo is a specific interface method 如何获取Queryable.Join的MethodInfo - How can I get the MethodInfo of Queryable.Join 有了 HttpContext,我怎样才能获得将要执行的操作的“MethodInfo”? - Having an HttpContext how can I get the "MethodInfo" of the action that is going to be executed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM