简体   繁体   English

检查MethodInfo实例是否是接口通用方法的实现

[英]Check MethodInfo instance is implementation of interface generic method

I has interface 我有界面

public interface ITest
{
    Task<bool> MyMethod1<T>(string key, out T value);
    Task<bool> MyMethod2<T>(string key, out T value);
}

And they implementation 他们实施

public class TestImpl : ITest
{
    public Task<bool> MyMethod1<T>(string key, out T value) // Implements interface
    {
        // Skipped
    }

    public Task<bool> MyMethod1<T>(string key, T value) // Does not implements interface
    {
        // Skipped
    }

    public Task<bool> MyMethod2<T>(string key, out T value) // Implements interface
    {
        // Skipped
    }

    public Task<bool> MyMethod2<T>(string key, T value) // Does not implements interface
    {
        // Skipped
    }
}

I need to check that specified instance of MethodInfo is method implementation of ITest interface. 我需要检查MethodInfo的指定实例是否是ITest接口的方法实现。

For example: 例如:

void DoWork(MethodInfo methodInfo)
{
    if (...) // Check methodInfo is implementation of any method declared in ITest interface
    {
        /* Do something */
    }
}

How to do that? 怎么做?

I have an idea, but I don't know if it fits your situation 我有个主意,但不知道是否适合您的情况
Why you don't get type of the implementation the class and then you get the interface you are interested in by its name nameof(ITest) and then you iterate through all methods of this interface which is implemented by that type like the following: 为什么不获得类的实现the class ,然后又通过其名称nameof(ITest)获得感兴趣的接口,然后遍历由该类型实现的该接口的所有方法,如下所示:

foreach(var methodInfo in typeof(TestImpl).GetInterface(nameof(ITest)).GetMethods())
{

}

This is very easy idea, the fact that you didn't hard-coded the interface name in the foreach loop it is good too. 这是一个非常简单的主意,因为您没有在foreach循环中对接口名称进行硬编码,这也很好。 If it didn't fit your situation, please consider adding more details to your question so I can think about a different idea and edit this answer, or someone can help you. 如果不适合您的情况,请考虑为您的问题添加更多详细信息,以便我考虑其他想法并修改此答案,或者有人可以为您提供帮助。
EDIT 编辑
Or you can get the concrete class type by the property ReflectedType and then you can get a 'reflection-mapping' of the methods of this type based on specific interface type using GetInterfaceMap method, this method will then returns the methods which is implemented for this interface type in this concrete class type, you can then simply use the method Contains to check for specific MethodInfo instance as you asked. 或者,您可以通过属性ReflectedType获取具体的类类型,然后可以使用GetInterfaceMap方法根据特定的接口类型获取此类型的方法的“反射映射”,然后该方法将返回为此实现的方法在此具体类类型的接口类型中,然后您可以根据需要简单地使用Contains方法检查特定的MethodInfo实例。 Hope this helped you now. 希望这对您有所帮助。

static bool IsMethodImplementationOfInterface(Type interfaceType,MethodInfo method)
{
    return method.ReflectedType.GetInterfaceMap(interfaceType).TargetMethods.Contains(method);
}

foreach (var methodInfo in typeof(TestImpl).GetMethods())
{
    if (IsMethodImplementationOfInterface(typeof(ITest), methodInfo))
    {
        //Logic
    }
}

You can use GetInterfaceMap: 您可以使用GetInterfaceMap:

var testImpl = new TestImpl();
        var interfaceTargeMethods = testImpl.GetType().GetInterfaceMap(typeof(ITest)).TargetMethods;

        foreach (var methodInfo in testImpl.GetType().GetMethods())
        {
            if (interfaceTargeMethods.Contains(methodInfo))
            {
                // do something...
            }    
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM