Reflection is a power tool of managed language like Java and .NET (C#, VB.NET,C++/CLI and others). In the last days I have discovered that MethodInfo class don’t provide a method to check if is overridden or not.This activity is vary simple but on internet you can found a dozen of example about Reflection without one about this case.
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Reflection;
5:
6: namespace ReflectionMethod
7: {
8: internal class Program
9: {
10: static void Main(string[] args)
11: {
12: BaseClass derived = new DerivedClass();
13:
14: Type type = derived.GetType();
15:
16: MethodInfo method1 = type.GetMethod("Method2");
17:
18: Console.WriteLine(String.Format("This method is overridden? {0}",
19: IsOverridden(method1)));
20:
21: Console.ReadKey();
22: }
23:
24: public static Boolean IsOverridden(MethodInfo info)
25: {
26: MethodInfo baseDef = info.GetBaseDefinition();
27:
28: return !(info.DeclaringType.FullName == baseDef.DeclaringType.FullName);
29: }
30: }
31: }