Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a MehtodBase of a method and I need to know if that method is an implementation of a specific interface. So if I have the following class:

class MyClass : IMyInterface
{
    public void SomeMethod();
}

Implementing the interface:

interface IMyInterface
{
    void SomeMethod();
}

I want to be able to discover at runtime (using reflection) if a certain method implements IMyInterface.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
336 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use GetInterfaceMap for this.

InterfaceMapping map = typeof(MyClass).GetInterfaceMap(typeof(IMyInterface));

foreach (var method in map.TargetMethods)
{
    Console.WriteLine(method.Name + " implements IMyInterface");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...