如何查找方法是否正在实现特定接口 [英] How to find if a method is implementing specific interface

查看:49
本文介绍了如何查找方法是否正在实现特定接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法的MehtodBase,我需要知道该方法是否是特定接口的实现。
因此,如果我有以下课程:

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();
}

实现界面:

interface IMyInterface
{
    void SomeMethod();
}

我希望能够在运行时(使用反射)发现是否存在方法实现IMyInterface。

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

推荐答案

您可以使用 GetInterfaceMap

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");
}

这篇关于如何查找方法是否正在实现特定接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆