本文介绍了获取MethodInfo的为Enumerable.DefaultIfEmpty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一些LINQ表达式,并试图获得一个MethodInfo的搁置 IEnumerable.DefaultIfEmpty ()。什么似乎是一件容易的事,但我无能,为什么它不工作。

  typeof运算(可枚举).GetMethod( DefaultIfEmpty,新的[] {typeof运算(IEnumerable的<>)}); 

typeof运算(可枚举).GetMethod(DefaultIfEmpty,新的[] {typeof运算(IEnumerable的<方式>)MakeGenericType(typeof运算(WorkitemListModel))});


解决方案

获取通用的方法,是一种痛苦,是诚实的。我不知道一个更好的方式,而不是使用方法:

  VAR方法= typeof运算(可枚举).GetMethods()
。凡(M = GT; m.Name ==DefaultIfEmpty)
。凡(M = GT; m.GetParameters()长度== 1)
。单();

要叫 GetMethod ,你必须有确切的正确的参数的类型,包括用于该参数的合适的一般类型参数。一旦你的一次的你能做到这一点,但在那之前,我认为上面的是所有可用:(


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)) });
解决方案

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

这篇关于获取MethodInfo的为Enumerable.DefaultIfEmpty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 18:42