Ref parameters and reflection

前端 未结 5 1956
北恋
北恋 2020-12-29 02:51

I\'m not sure if I\'m totally missing something here but I can\'t find any way to determine if a parameter is passed by reference or not by using reflection.

Argumen

5条回答
  •  萌比男神i
    2020-12-29 03:27

    ParameterType.IsByRef will return true for both ref and out parameters.

    If you have a ParameterInfo object (e.g. from MethodInfo.GetParameters()), then:

    • The param is out if parameterInfo.ParameterType.IsByRef && parameterInfo.IsOut
    • The param is ref if parameterInfo.ParameterType.IsByRef && parameterInfo.IsOut == false

    If you don't do the IsByRef check for out parameters, then you'll incorrectly get members decorated with the [Out] attribute from System.Runtime.InteropServices but which aren't actually C# out parameters.

提交回复
热议问题