询问MethodInfo需要多少参数的最有效方法是什么?
如果MethodInfo接受参数,那么询问MethodInfo的最有效方法是什么?如果接受,有多少?
我目前的解决方案是: methodInfo.GetParameters().Any()
和methodInfo.GetParameters().Count()
。
这是最有效的方式吗?
由于我实际上并不需要任何ParameterInfo
对象,有没有办法在不调用GetParameters()
情况下执行此操作?
你列出的两个是LINQ。 Any()
返回bool
– 声明至少有一个。 在IEnumerable
上使用Count()
。
Length
(属性)将是最快的,因为GetParameters()
返回ParameterInfo[]
。
似乎MethodInfo
没有任何其他方法来访问GetParameters()
以外的参数数量。
如果效率很重要,为什么不将结果缓存在Dictionary
? 这样你只需要使用一次reflection。
如果要获取MethodInfo
的参数计数,请使用以下代码
int intLength = mi.GetParameters().Length; // where mi is a variable of type MethodInfo