Является ли GetBaseDefinition
тем, что вы ищете?
В основном
if (method.GetBaseDefinition() == method)
{
// Method was declared in this class
}
Вот пример, показывающий интересующие вас случаи:
using System;
using System.Reflection;
class Base
{
public virtual void Overridden() {}
public virtual void NotOverridden() {}
}
class Derived : Base
{
public override void Overridden() {}
public virtual void NewlyDeclared() {}
}
public class Test
{
static void Main()
{
foreach (MethodInfo method in typeof(Derived).GetMethods())
{
Console.WriteLine("{0}: {1} {2} {3}",
method.Name,
method == method.GetBaseDefinition(),
method.DeclaringType,
method.GetBaseDefinition().DeclaringType);
}
}
}
Выход:
Overridden: False Derived Base
NewlyDeclared: True Derived Derived
NotOverridden: False Base Base
ToString: False System.Object System.Object
Equals: False System.Object System.Object
GetHashCode: False System.Object System.Object
GetType: True System.Object System.Object