What is the most efficient way to set the MethodInfo number of parameters?

What is the most efficient way to set MethodInfo if it takes parameters and, if so, how many?

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

Is this the most effective way?

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

+6
performance reflection c #
source share
3 answers

Two listed by you for LINQ. Any() returns bool - indicating that at least one exists. Count() used by anyone on an IEnumerable<T> .

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

It looks like MethodInfo has a different way of accessing a number of parameters other than GetParameters() .

+10
source share

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

+5
source share

If you want to get a MethodInfo parameter MethodInfo , use the following code

 int intLength = mi.GetParameters().Length; // where mi is a variable of type MethodInfo 
0
source share

All Articles