Get methodinfo for Enumerable.DefaultIfEmpty

て烟熏妆下的殇ゞ 提交于 2020-01-14 10:33:12

问题


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)) });

回答1:


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 :(



来源:https://stackoverflow.com/questions/6384031/get-methodinfo-for-enumerable-defaultifempty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!