Created
May 13, 2013 20:26
-
-
Save EdVinyard/5571213 to your computer and use it in GitHub Desktop.
Attempt to validate behavior of IsOverridden implementation suggested by http://stackoverflow.com/a/10020948/150
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Reflection; | |
using NUnit.Framework; | |
public class IsOverrideTests | |
{ | |
abstract class Base | |
{ | |
public virtual void Overridden() { } | |
public virtual void Inherited() { } | |
} | |
class Derived : Base | |
{ | |
public override void Overridden() { } | |
} | |
/// <summary>broken</summary> | |
public static bool IsOverride_broken(MethodInfo m) | |
{ | |
return m.GetBaseDefinition() != m; | |
} | |
[Test] | |
public void IsOverride_broken_overriden() | |
{ | |
Assert.IsTrue(IsOverride_broken( | |
typeof (Derived).GetMethod("Overridden"))); | |
} | |
[Test] | |
public void IsOverride_broken_inherited() | |
{ | |
Assert.IsFalse(IsOverride_broken( | |
typeof(Derived).GetMethod("Inherited"))); | |
} | |
/// <summary>not as broken</summary> | |
public static bool IsOverride_better(MethodInfo m) | |
{ | |
return m.GetBaseDefinition().DeclaringType != m.DeclaringType; | |
} | |
[Test] | |
public void IsOverride_better_overriden() | |
{ | |
Assert.IsTrue(IsOverride_better( | |
typeof(Derived).GetMethod("Overridden"))); | |
} | |
[Test] | |
public void IsOverride_better_inherited() | |
{ | |
Assert.IsFalse(IsOverride_better( | |
typeof(Derived).GetMethod("Inherited"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment