获取Enumerable.DefaultIfEmpty的methodinfo

Tor*_*son 4 c# reflection methodinfo

我正在构建一些Linq Expression并尝试获取MethodInfo IEnumerable.DefaultIfEmpty(http://msdn.microsoft.com/en-us/library/bb360179.aspx).什么似乎是一个简单的任务,但我不知道它为什么不工作.

typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>) });

typeof(Enumerable).GetMethod("DefaultIfEmpty", new[] { typeof(IEnumerable<>).MakeGenericType(typeof(WorkitemListModel)) });
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

说实话,获取通用方法很痛苦.我不知道比使用更好的方法:

var method = typeof(Enumerable).GetMethods()
                               .Where(m => m.Name == "DefaultIfEmpty")
                               .Where(m => m.GetParameters().Length == 1)
                               .Single();
Run Code Online (Sandbox Code Playgroud)

要调用GetMethod,您必须具有完全正确的参数类型,包括参数的正确泛型类型参数.一旦你得到了,一旦你能做到这一点,但在那之前,我认为上面的是所有的可用:(