Redim array parameter when using reflection

F

Fredrik Strandberg

I have not been able to find the solution of this problem anywhere:

I am building a class PrivateHelper that provides methods to access
private members and invoke private methods, to be used for testing.

In a particular test case, an array is passed to the Method Under Test,
and then that MUT performs a ReDim on the array. The test code then
needs to check the array, but the effect of the ReDim statement does
not propagate to the test code. As long as no ReDim is done, there are
no problems and all changes to the array are visible for the test code.

This is a simplified sketch of how I have done (The PrivateHelper class
is left out):

Sub testCase()
Dim testArray = New Byte() {1, 1}
'setup methodInfo for method "changeArray" in class myObject
methodInfo.Invoke(myObject, New Object() {testArray})
'check if testArray(0) = 2 THIS CHECK FAILS
End Sub

Class myObject
Sub changeArray(ByRef testArray As Byte())
ReDim testArray(3)
testArray(0) = 2
End Sub
End Class

I am not allowed to do any changes in the myObject class. Is there a
way around this problem?
 
A

Armin Zingler

Fredrik Strandberg said:
I have not been able to find the solution of this problem anywhere:

I am building a class PrivateHelper that provides methods to access
private members and invoke private methods, to be used for testing.

In a particular test case, an array is passed to the Method Under
Test, and then that MUT performs a ReDim on the array. The test code
then needs to check the array, but the effect of the ReDim statement
does not propagate to the test code. As long as no ReDim is done,
there are no problems and all changes to the array are visible for
the test code.

This is a simplified sketch of how I have done (The PrivateHelper
class is left out):

Sub testCase()
Dim testArray = New Byte() {1, 1}
'setup methodInfo for method "changeArray" in class myObject
methodInfo.Invoke(myObject, New Object() {testArray})
'check if testArray(0) = 2 THIS CHECK FAILS
End Sub

Class myObject
Sub changeArray(ByRef testArray As Byte())
ReDim testArray(3)
testArray(0) = 2
End Sub
End Class

I am not allowed to do any changes in the myObject class. Is there a
way around this problem?



Sub testCase()
Dim testArray = New Byte() {1, 1}
'setup methodInfo for method "changeArray" in class myObject

dim params as object()
params = new object(){testarray}
methodInfo.Invoke(myObject, New Object() {testArray})
testarray = directcast(params(0), byte())
End Sub


Before, the new Array has been returned to the object array which is the
container for the parameters. The problem was that you didn't store a
reference to the object array, thus you weren't able to access the new
values (the reference to the new array created by Redim) in the
object array after Invoke returned.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top