Last active
November 7, 2019 14:11
-
-
Save gsscoder/5271909 to your computer and use it in GitHub Desktop.
Invokes Enumerable::SequenceEqual generic extension method with reflection
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
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