Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to ask a MethodInfo how many parameters it takes?

What is the most efficient way to ask a MethodInfo if it accepts parameters and, if so, how many?

My current solutions would be: methodInfo.GetParameters().Any() and methodInfo.GetParameters().Count().

Is this the most efficient way?

Since I don't actually need any of the ParameterInfo objects, is there a way to do this without a call to GetParameters()?

like image 840
smartcaveman Avatar asked Feb 09 '11 19:02

smartcaveman


2 Answers

The two you listed are for LINQ. Any() returns bool - stating that there is at least one. Count() is used any on IEnumerable<T>.

Length (the property) will be the fastest because GetParameters() returns ParameterInfo[].

It does not appear that MethodInfo have any other way to access the number of parameters other than GetParameters().

like image 62
Daniel A. White Avatar answered Sep 18 '22 20:09

Daniel A. White


If efficiency matters why don't you just cache the result in a Dictionary<MethodInfo,int>? That way you only need to use reflection only once.

like image 29
CodesInChaos Avatar answered Sep 21 '22 20:09

CodesInChaos