Why can't I retrieve the value for parameters of type out or ref using Type.InvokeMember?

后端 未结 3 1153
梦如初夏
梦如初夏 2021-01-13 01:13

A long title, but I wanted it to be specific. The title is really the question. Even though the method that InvokeMember is calling has an out pa

3条回答
  •  -上瘾入骨i
    2021-01-13 01:46

    In your first code example, the call to InvokeMember doesn't modify the value of the parameter variable, it just replaces the first item in the parameter array (which now points to a different string instance). Since you didn't keep a reference to this array, you can't retrieve the value of the output parameter.

    In other words: the array initially contains a copy of the parameter variable (i.e. a copy of the reference to an empty string). After the call, parameter and the value in the array refer to 2 different string instances.

提交回复
热议问题