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.

For example:

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

How to do that?

问题评论:

答案:

答案1:

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:

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.
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. 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
    }
}

答案评论:

    
Thanks for reply. In my case this impossible, because i need check MethodInfo passed by 3rd-party library
– maxswitcher
23 hours ago
    
Provided better example
– maxswitcher
23 hours ago
    
I’ve updated my answer, tell me if this helped you now!
    
Now it works, thanks
– maxswitcher
22 hours ago
    
You’re welcome!

答案2:

You can use 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...
            }    
        }

答案评论:

原文地址:

https://stackoverflow.com/questions/47741099/check-methodinfo-instance-is-implementation-of-interface-generic-method

添加评论