Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active November 7, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsscoder/5271909 to your computer and use it in GitHub Desktop.
Save gsscoder/5271909 to your computer and use it in GitHub Desktop.
Invokes Enumerable::SequenceEqual generic extension method with reflection
static class DynamicComparer
{
public static bool DynamicEquals<T>(this T value, T other)
{
if (object.Equals(value, default(T)))
{
throw new ArgumentNullException("value");
}
if (object.Equals(other, default(T)))
{
return false;
}
if (value is IEnumerable && other is IEnumerable)
{
var t = typeof(Enumerable);
var meth = t.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Single(m => m.Name.Equals("SequenceEqual") && m.GetParameters().Count() == 2);
var gm = meth.MakeGenericMethod(typeof(T).GetGenericArguments().Single());
return (bool)gm.Invoke(null, new object[] { value, other });
}
return value.Equals(other);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment