This is a migrated thread and some comments may be shown as answers.

Call Private method with ref parameters

1 Answer 292 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Vikas
Top achievements
Rank 1
Vikas asked on 24 Nov 2014, 07:48 PM
I am using CallMethod to call a private method and it works great, but I need to call a method that has some ref parameters, Is it possible to do that
Also i would like to compare the values in the ref after the method execution finishes, basically instead of returning one value method is manipulating multiple values and i need to Assert all of them

Thanks
vikas

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 27 Nov 2014, 12:10 PM
Hello Vikas,

Here's how you can do it:
public class HasRef
{
    private void Do(ref int a)
    {
        a = a * 2;
    }
}
 
[TestMethod, TestCategory("Elevated"), TestCategory("NonPublic")]
public void PrivateAccessor_RefCall()
{
    var inst = new PrivateAccessor(new HasRef());
    var args = new object[] { 20 };
    inst.CallMethod("Do", args);
    Assert.AreEqual(40, args[0]);
}

Ref and out arguments are passed back into the array of arguments passed to CallMethod.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
James
Top achievements
Rank 1
Iron
commented on 23 Jun 2025, 08:06 PM | edited

Hi,

Is the only way to do this by constructing the array manually and also indexing into the array? The "params" keyword is supposed to be capable of handling parameters like below implicitly adding them into an array.

int testValue = 20;
inst.CallMethod("Do", testValue);
Assert.AreEqual(40, testValue); // testValue is still 20

The above does not work, "testValue" is still 20 even though the "a" variable when debugging the method is set to 40.

Any other instance where I am needing to call private methods I am sending the parameters like above... however it seems for reference parameters I must construct the array myself and I must index into the array when checking the value as it does not update the original variable. 

Even the below does not work, since as stated above the framework doesn't seem to update the original variable.

int testValue = 20;
var args = new object[] { testValue };
inst.CallMethod("Do", args);
Assert.AreEqual(40, testValue); // testValue is still 20

This now means I will have two different paradigms in our unit tests where sometimes we send the parameters to CallMethod individually and other times we construct the array and then index into it to get the value. I can make sure that is clear through comments in the code, but I don't like that we have to do it in a specific way for reference parameters.

Full working code below... however, I don't see why this is necessary. Couldn't the framework update the original variable?

[TestMethod, TestCategory("Elevated"), TestCategory("NonPublic")]
public void PrivateAccessor_RefCall()
{
    var inst = new PrivateAccessor(new HasRef());
    int testValue = 20;
    var args = new object[] { testValue };
    inst.CallMethod("Do", args);
    Assert.AreEqual(40, args[0]); // args[0] does return 40, but testValue is still 20. Why?
}
Ivo
Telerik team
commented on 25 Jun 2025, 03:51 PM

Hello James, the private accessor is a thin wrapper around reflection. It appears that the object returned in the array is different after the call; this clarifies things. Please see the pictures below.

 

Tags
General Discussions
Asked by
Vikas
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or