gpt4 book ai didi

c# - 如何知道一个类是否重新定义了一个接口(interface)

转载 作者:行者123 更新时间:2023-11-30 22:04:49 25 4
gpt4 key购买 nike

我有以下类(class):

internal class Father : ICloneable
{
public object Clone() { ... }
public void Dispose() { ... }
}

internal class Son : Father { }
internal class Daughter : Father, ICloneable { }

我尝试使用 GetInterfaces 方法从带有反射的 Type 中检索接口(interface),但 Son 和 Daughter 之间没有区别。有没有办法找到仅在当前类级别声明的接口(interface)。

对于 Son,结果应该是空的,对于 Daughter,结果应该是 ICloneable。

PS:我知道在子类中重新定义接口(interface)是没有用的,但这不是来 self 的程序集...

最佳答案

您需要使用 unmanaged .NET Metadata API而不是托管反射 API。 This blog post explains the differences .具体来说,EnumInterfaceImpls方法给你你想要的。我没有亲自使用托管代码中的非托管 API,但快速浏览一下 SO 可以为您提供大量这样做的示例。我希望这回答了你的问题。我不知道你想达到什么目的。如果您需要实现一些代码实践策略检查器,我认为您最好通过其代码模型 API 在 IDE (VS) 中实现它 - 然后您的代码可以推理 C# 源代码,而不是 MSIL 输出。如果您打算将该接口(interface)用作“不可继承”的标记接口(interface),则最好使用属性。

关于c# - 如何知道一个类是否重新定义了一个接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24869651/

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