2

How can one determine if a ParameterInfo is a Return Parameter?

I wrote the function below, but I'm concerned that I may be missing something:

public bool IsReturnParameter(ParameterInfo parameter){
    var method = parameter.Member as MethodInfo;
    return method != null && parameter.Equals(method.ReturnParameter);
}

I am basing this on a couple assumptions, which may be flawed: (1) Parameters are declared on members that are MethodInfo, ConstructorInfo or PropertyInfo (indexers). (2) ConstructorInfo and PropertyInfo will never have a return parameter.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • You want to know if the method has out/ref parameters? – Jeff Nov 12 '12 at 19:18
  • No. I want to know if it is a return parameter of the method – smartcaveman Nov 12 '12 at 19:19
  • To be clear, you want the ParameterInfo which tells you if the return of the method has any custom attributes? – Jeff Nov 12 '12 at 19:22
  • @JeffN825, Yes. That is what I am trying to test. – smartcaveman Nov 12 '12 at 19:24
  • Maybe I'm not understanding the question. What would usage for this be like? The `ReturnParameter` value doesn't show up in the results of `GetParameters()`, based on my tests - the only way I've found to get at it is to explicitly check the `ReturnParameter` property. And if you're doing that, you already know that it's the return parameter. – Bobson Nov 12 '12 at 19:51
  • @Bobson, I'm crawling the type definition and I don't want to pass my visitor the container method of the parameter. – smartcaveman Nov 12 '12 at 21:56

2 Answers2

7

You could check if the ParameterInfo.Position == -1...but your equality check seems equally as good...though it won't correctly handle overrides or interfaces or generic types in some cases.

Jeff
  • 35,755
  • 15
  • 108
  • 220
-1

Assuming you're referring to out int foo, you want parameter.IsOut.

If you want the return value, try IsRetval, although I've never heard of that before.

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • (1) I am not asking for an out parameter. I am asking for the return value. (2) That is not what `IsRetval` is for – smartcaveman Nov 12 '12 at 19:25
  • http://stackoverflow.com/questions/11734607/when-are-parameterinfo-islcid-or-parameterinfo-isretval-true – smartcaveman Nov 12 '12 at 19:27
  • So `IsRetval` **is** the correct thing to check, except that the compiler doesn't generally set it. "`The [retval] attribute designates the parameter that receives the return value of the member.`" – Bobson Nov 12 '12 at 19:28