Skip to content

Instantly share code, notes, and snippets.

@EdVinyard
Created May 13, 2013 20:26
Show Gist options
  • Save EdVinyard/5571213 to your computer and use it in GitHub Desktop.
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
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