Get methodinfo for Enumerable.DefaultIfEmpty
-
28-10-2019 - |
Question
I'm building some Linq Expression and trying to get hold of MethodInfo for IEnumerable.DefaultIfEmpty
(http://msdn.microsoft.com/en-us/library/bb360179.aspx). What seemed to be an easy task but I'm clueless to why it's not working.
typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>) });
typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>).MakeGenericType(typeof(WorkitemListModel)) });
Solution
Getting generic methods is a pain, to be honest. I don't know of a better way than to use:
var method = typeof(Enumerable).GetMethods()
.Where(m => m.Name == "DefaultIfEmpty")
.Where(m => m.GetParameters().Length == 1)
.Single();
To call GetMethod
, you'd have to have the exact correct parameter type, including the right generic type parameter for the parameter. Once you've got that once you could do it, but until then I think the above is all that's available :(
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow