Skip to content

Instantly share code, notes, and snippets.

@bennage
Created October 1, 2009 18:56
Show Gist options
  • Save bennage/199160 to your computer and use it in GitHub Desktop.
Save bennage/199160 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
public class When_overriding_GetHashCode
{
[Fact]
public void Equals_should_be_overridden()
{
var dlls = new List<Assembly>
{
Assembly.Load("some.assembly"),
Assembly.Load("another.assembly"),
};
var missing = from assembly in dlls
from type in assembly.GetTypes()
let methods = type.GetMethods()
where methods.Any(x => x.Name == "GetHashCode" && x.DeclaringType == type)
&& !methods.Any(x => x.Name == "Equals" && x.DeclaringType == type)
select new {Type = type, Assembly = assembly};
var messages = from item in missing
select
string.Format(
"The type {0} overrides GetHashCode() but not override Equals(), in assembly {1}.",
item.Type.Name,
item.Assembly.GetName());
Assert.True(missing.Count() == 0, string.Join("\n", messages.ToArray()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment