public
Created

Invokes Enumerable::SequenceEqual generic extension method with reflection.

  • Download Gist
DynamicComparer.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.