gpt4 book ai didi

c# - 如何确定 MethodInfo 是否是基方法的重写

转载 作者:可可西里 更新时间:2023-11-01 08:18:39 24 4
gpt4 key购买 nike

我正在尝试确定我从类型实例上的 GetMethod 调用中获得的 MethodInfo 对象是由该类型还是由其基础实现的。

例如:

Foo foo = new Foo();
MethodInfo methodInfo = foo.GetType().GetMethod("ToString",BindingFlags|Instance);

ToString 方法可以在 Foo 类中实现,也可以不实现。我想知道我是否获得了 foo 实现?

Related question

Is it possible to tell if a .NET virtual method has been overriden in a derived class?

最佳答案

检查其 DeclaringType 属性。

if (methodInfo.DeclaringType == typeof(Foo)) {
// ...
}

关于c# - 如何确定 MethodInfo 是否是基方法的重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/982347/

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