How to determine if the MethodInfo is an override of the base method

走远了吗. 提交于 2019-11-29 13:15:55

Check its DeclaringType property.

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

Instead of using reflection a much faster way is to use delegates! Especially in the new version of the framework the operation is really fast.

    public delegate string ToStringDelegate();

    public static bool OverridesToString(object instance)
    {
        if (instance != null)
        {
            ToStringDelegate func = instance.ToString;
            return (func.Method.DeclaringType == instance.GetType());
        }
        return false;
    }

You'll want to look at the DeclaringType property. If the ToString method comes from Foo, then the DeclaringType will be of type Foo.

You have to check if DeclaringType property of MemberInfo object (DeclaringType actually gets the class that declares this member) is equal to ReflectedType property (which gets the class object that was used to obtain this instance of MemberInfo).

Besides that, you have also to check the property IsAbstract. If it is true, then the inspected method is definitely not overridden, because "being abstract" means that this member is a new declaration that cannot have it's implementation (body) within current class (but only in derived classes instead).

Here is an example of usage of the extension method provided below:

Student student = new Student
{
    FirstName = "Petter",
    LastName = "Parker"
};

bool isOverridden = student.GetType()
    .GetMethod(
        name: nameof(ToString),
        bindingAttr: BindingFlags.Instance | BindingFlags.Public,
        binder: null,
        types: Type.EmptyTypes,
        modifiers: null
    ).IsOverridden(); // ExtMethod

if (isOverridden)
{
    Console.Out.WriteLine(student);
}

Extension Method:

using System.Reflection;

public static class MethodInfoHelper
{
    /// <summary>
    ///     Detects whether the given method is overridden.
    /// </summary>
    /// <param name="methodInfo">The method to inspect.</param>
    /// <returns><see langword="true" /> if method is overridden, otherwise <see langword="false" /></returns>
    public static bool IsOverridden(this MethodInfo methodInfo)
    {
        return methodInfo.DeclaringType == methodInfo.ReflectedType
               && !methodInfo.IsAbstract;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!