C#: Determine if a Method is Overridden in a Derived Class


The other day I was doing some work in which I needed to be able to determine from a base class whether a particular method had been overridden in a derived class, and once again reflection comes to the rescue.

Consider the class declarations below;

	internal class BaseA
	{
		public virtual string GetMappingsForCreate()
		{ return "A"; }

		public bool TestIfMethodOverridden(string methodName)
		{
			var t = this.GetType();
			var mi = t.GetMethod(methodName,
										BindingFlags.Instance | BindingFlags.Public);
			if (mi == null) return false;

			var declaringType = mi.DeclaringType.FullName;

			return declaringType.Equals(t.FullName, StringComparison.OrdinalIgnoreCase);
		}
	}

	internal class BaseB : BaseA
	{
		public override string GetMappingsForCreate()
		{ return "B"; }
	}

	internal class BaseC : BaseA
	{}

We have some simple inheritance here, a base class BaseA and two deriving classes BaseB and BaseC – only BaseB overrides the single virtual method GetMappingsForCreate(). In base class BaseA there is a method to determine if a given method has been overridden in the class definition of the class instance referred to by this.

Note that it isn’t enough to get the MethodInfo for the method in question, since it will always exist. The key thing here is to evaluate which type declares the method in question – if a derived class doesn’t override a virtual method, the declaring type of the method will be next class down the chain of inhertitance which does override the method, right back down to the base class.

Running the test sample demonstrates this;

			var a = new BaseA();
			var b = new BaseB();
			var c = new BaseC();

			Console.WriteLine("BaseA::GetMappingsForCreate = " + a.TestIfMethodOverridden("GetMappingsForCreate"));
			Console.WriteLine("BaseB::GetMappingsForCreate = " + b.TestIfMethodOverridden("GetMappingsForCreate"));
			Console.WriteLine("BaseC::GetMappingsForCreate = " + c.TestIfMethodOverridden("GetMappingsForCreate"));

			Console.WriteLine();

			Console.WriteLine("BaseA::GetMappingsForCreate returned " + a.GetMappingsForCreate());
			Console.WriteLine("BaseB::GetMappingsForCreate returned " + b.GetMappingsForCreate());
			Console.WriteLine("BaseC::GetMappingsForCreate returned " + c.GetMappingsForCreate());

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

5 thoughts on “C#: Determine if a Method is Overridden in a Derived Class

  1. The following will work without needing string comparisons:

    1. Add BindingFlags.DeclaredOnly to your flags.
    2. Just do: return mi != null

  2. Also, delegates are a far quicker way of getting the MethodInfo of a known method:

    private delegate string GetMappingsForCreateDelegate();
    public bool OverridesGetMappingsForCreate()
    {
    GetMappingsForCreateDelegate del = this.GetMappingsForCreate;
    return (del.Method != null && del.Method.DeclaringType == typeof(BaseA));
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s