gpt4 book ai didi

c# - 如何查找方法是否正在实现特定接口(interface)

转载 作者:可可西里 更新时间:2023-11-01 07:45:30 25 4
gpt4 key购买 nike

我有一个方法的 MehtodBase,我需要知道该方法是否是特定接口(interface)的实现。所以如果我有以下类(class):

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

实现接口(interface):

interface IMyInterface
{
void SomeMethod();
}

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

最佳答案

您可以使用 GetInterfaceMap为此。

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

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

关于c# - 如何查找方法是否正在实现特定接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7379276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com