D
D
Dmitry Korolev2019-07-17 19:31:45
C++ / C#
Dmitry Korolev, 2019-07-17 19:31:45

How to pass a private method to a thread in a test?

I have private bool Load(int i){}to pass it in the test to a thread or task, but how do I do it?
I'm creating methodinfo as a private method. I can pass a method to the task without return and parameters like this

task t = new Task((action)methodinfo.createdelegate(typeof(action)))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Korolev, 2019-07-18
@adressmoeistranici

Type t = typeof(..);
MethodInfo mi = t.GetMethod("***", BindingFlags.Instance | BindingFlags.NonPublic);
ParametrizedThreadStart parametrizedThreadStart = mew ParametrizedThreadStart ((object o)=>{
mi.Invoke((..)o, new object[] {0});
});

Thread t = new Thread(parametrizedThreadStart);
string str;
using (StringWriter wr = new StringWriter())
{
TextWriter s= Console.Out;
Console. SetOut(wr);
t.Start(..);
t.Join();
str = wr.ToString();
Console.Setout(s);
}
Assert.AreEquals(regex.ismatch(str, @****), true);

P
Peter, 2019-07-17
@petermzg

https://habr.com/en/post/224733/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question