chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
try this code:

public abstract class BaseA
{
  private void CallMe()
  {
  }
}

public class DB: BaseA
{
}


and the test code
[Test]
public void RunMe()
{
  var dbnew = new DB();
  Isolate.Invoke.Method(dbnew ,"CallMe"));
}


The test will throw a "There is no method named exception".

I think this is a bug, what do you think?
________
Free Walmart Gift Cards
asked by nsoonhui (59.1k points)

5 Answers

0 votes
Hi Soon,

Not sure it's a bug, since CallMe is private method it is not inherited by DB class event though BaseA is an abstract class.
Maybe it's a missing feature:
call base class private method through derived instance. What do you think?
answered by ohad (35.5k points)
0 votes
Maybe that's a missing feature... but currently is there anyway that allows me to call base private method, given the derived instance?
________
EXTREME Q VAPORIZER
answered by nsoonhui (59.1k points)
0 votes
Hi Soon

Currently there's no way to that through the Isolator but you can easily do that by using some reflection magic :)
Here's an example that will work on instance methods:
private object InvokeBase<T>(T instance, string methodName, params object[] methodParams)
{
    Type baseType = instance.GetType().BaseType;
    Type [] types = GetTypesArray(methodParams);
    MethodInfo method = baseType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic, null, types, null);
    return method.Invoke(instance, methodParams);
}

private Type[] GetTypesArray(object[] objects)
{
    List<Type> types = new List<Type>();
    foreach (var o in objects)
    {
        types.Add(o.GetType());
    }

    return types.ToArray();
}

//usage in test 
[Test]
public void RunMe()
{
    var dbnew = new DB();
    // call to overload without parameters
    InvokeBase(dbnew, "CallMe", new object[0]);
    // call to overload with int and string parameter
    InvokeBase(dbnew, "CallMe", 1, "a");
}


Hope it helps.
answered by ohad (35.5k points)
0 votes
Thanks, that's a good idea.

Maybe you can consider incorporating it into your Isolate.Invoke.Method? You can just add another parameter to take in whether one wants to invoke the base or current.
answered by nsoonhui (59.1k points)
0 votes
Hi Soon,

I'll bring that feature request to the team.
However I'm not sure that adding one parameter to call base will solve it
since users might want to invoke methods more than one level up the hierarchy and we'll want to maintain the test readability.

If you have any ideas please let us know.
answered by ohad (35.5k points)
...