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()
?
The two you listed are for LINQ.
Any()
returnsbool
- stating that there is at least one.Count()
is used any onIEnumerable<T>
.Length
(the property) will be the fastest becauseGetParameters()
returnsParameterInfo[]
.It does not appear that
MethodInfo
have any other way to access the number of parameters other thanGetParameters()
.If you want to get the count of parameters of a
MethodInfo
, then use the below codeIf 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.